- 博客(25)
- 资源 (1)
- 问答 (1)
- 收藏
- 关注
转载 Check if array element appears on line
I'm going through a file line by line and I want to check if that line contains any element from an array. for instance if I have:myArray = ["cat", "dog", "fish"]and the current line said:
2014-09-28 20:22:32 329
转载 Obtain Key from a Hash of Arrays using array value
I have a Hash as follows:{'a' => [title: 'ab', path: 'cd'], 'b' => [title: 'ef', path: 'gh']}Now lets say I have one title and wish to get the key from it...i.e. if I have 'ef' and want
2014-09-27 11:38:07 362
转载 Ruby converting array of hashes to array of arrays
I have a ruby array of hashesarr1 = [:a => {:name=>"Bob",:age=>"10",:city=>"NY"}, :b => {:name=>"Mike",:age=>"20",:city=>"FL"}]What is the best way to convert this toarr2 = [["Bob",10]
2014-09-26 09:00:13 333
转载 List the maximum or minimum of the elements at each position of two arrays
[a1, a2].transpose.map &:minmaxGiven a larger pair of arrays, it stores the minmax at each index.a1 = [ 10, 30, 8, 15 ]a2 = [ 17, 23, 5, 35 ]p [a1, a2].transpose.map &:minmax # => [[10, 1
2014-09-26 08:53:06 313
转载 How to group anagrams in a string into an array
I want to group anagrams in ruby from a given string and the result should be an array containing arrays of anagram groups likeGiven String: "scream cars for four scar creams"here cars and scar ar
2014-09-25 15:05:56 349
转载 Ruby, unique hashes in array based on multiple fields
I'd like to get back an array of hashes based on sport and type combinationI've got the following array:[ { sport: "football", type: 11, other_key: 5 }, { sport: "football", type: 12, o
2014-09-25 15:03:55 360
转载 Distinguish between index of a decimal number and integer inside an array in Ruby?
Since Ruby does type conversion, how do I get the index correctly?I would like this to return 1[1,2.0,2,3].index(2.0)#=> 1I would like this to return 2[1,2.0,2,3].index(2)#=> 1
2014-09-25 14:56:32 319
转载 Ruby: Multidimensional array to multidimensional hash
I have an array:a = [[1,[2,3]], [2,[3,4]], [3,[4,5]]]I want to convert it to:h = {1 => {2 => 3}, 2 => {3 => 4}, 3 => {4 => 5}}I m new to Ruby, can anybody help me to solve this?
2014-09-18 16:12:52 359
转载 get coordinates of value in 2D array
array = [["foo", "bar", "lobster"], ["camel", "trombone", "foo"], ["goat", "car", "hog"], ["foo", "harp", "foo"]]array.each_with_index.with_object([]) { |(a,i),b| a.each_with_index { |s,
2014-09-14 09:17:28 311
转载 关于字符串转换日期的问题
大家好:最近遇到一个需求必须要把字符串’20120514144424‘这个样子的转换为日期如:2012-05-14 14:44:24
2014-09-11 08:47:53 336
转载 Ruby Group Parts of An Array
I start out with a following simple array, as such:a = ["a", "b", "c", "d", "e", "f"]I want the below. How do I get this?a = ["ab", "cd", "ef"] a.each_slice(2).map(&:join)
2014-09-06 16:26:30 387
转载 Sorting a 2d array in ruby
arr = [[1,2,8], [3,8,4], [9,2,10], [0,1,3]]arr.sort! { |a, b| a.min b.min }
2014-09-06 16:21:34 247
转载 Sorting a mixed array of hashes and strings
What is the syntax for sorting an array alphabetically by the key of a hash or by a string? I would like to sort the following:['bob', 'apple', 'sandwich', {'lasagne' => 'munch'}, 'tangoed']to g
2014-09-06 16:10:44 346
转载 Does Ruby have a method similar to Emacs align-regexp?
Looking for a Ruby method to operate on an array of strings and align them according to a regex given as as an argument. Emacs has a defun called align-regexp that does this interactively when operati
2014-09-05 10:43:41 470
转载 Ruby hash combinations
input = { color: [ "blue", "grey" ], size: [ "s", "m", "l" ], wt: [:light, :heavy] }keys = input.keys #=> [:color, :size, :wt]values = input.values #=> [["blue", "grey"]
2014-09-05 09:35:56 493
转载 Change Hash Array keys to strings
I have this Hash:{["word"]=>1, ["cat"]=>2, ["tree"]=>1, ["dog"]=>1}But I want to have this Hash:{"word"=>1, "cat"=>2, "tree"=>1, "dog"=>1}
2014-09-05 09:26:47 302
转载 Sorting array elements by date
I have the following array.dates = ["6/23/2014", "8/5/2014", "8/19/2014", "6/26/2014", "8/19/2014", "8/19/2014","7/8/2014", "6/3/2014", "7/30/2014", "7/3/2014", "6/3/2014", "6/26/2014"]Our arr
2014-09-05 08:41:59 310
转载 How to get array from string contained identical symbols in Ruby?
I have string like this: "1112222355". How can I get array like that ["111","2222","3","55"] using Ruby?Assuming you want to group only consecutive elements, use Enumerable#chunk:> "1112222355
2014-09-03 11:21:20 335
转载 Rails mapping array of hashes onto single hash
I have an array of hashes like so: [{"testPARAM1"=>"testVAL1"}, {"testPARAM2"=>"testVAL2"}]And I'm trying to map this onto single hash like this:{"testPARAM2"=>"testVAL2", "testPARAM1"=>"testV
2014-09-03 11:20:09 322
转载 Split Ruby array into sub array with slash
I have got an Array:a = [[676, "/"], [679, "/", 680, "/"], [4, "3.7", "111.55", "/"], [668, "/"], [681, "/", 682, "/"]]Want to have an output like:a = [[676], [679], [680], [4, "3.7", "111.55"
2014-09-03 09:10:04 532
转载 Convert array of key value object to object of the key values (ruby)
I have a list of objects that have key attribute and value attribute.I would like to convert it to an object that contains attributes named as keys with the values.Example will make it clearer
2014-09-02 16:20:21 361
转载 Wobble Hypothesis - Combination or Permutation of Array of strings in ruby
I have a ruby array with 4 stringsnucleotides = ['A', 'G', 'C', 'T']I need 4^3 combinations of strings.new_array = ['AGC', 'AGT' .. 'TTT']This is the wobble hypothesis in genetics. Could s
2014-09-02 11:18:53 386
转载 How to sort an array of hashes into hashes with multiple values for a key?
am working on Ruby on Rails project using rails4.Scenario:I have an array of hashes. An array contains hashes where keys are the same. a = [{132=>[1000.0]}, {132=>[4000.0]}, {175=>[1000.0]},
2014-09-02 10:22:57 687
转载 Access nested array, where depth is unknown until runtime in Ruby
Example:array = [ [ [ [ ["foo"] ] ] ] ]array[0][0][0][0][0] == "foo" # => true
2014-09-01 11:31:21 317
转载 ruby find entry in array
I have some array[0,1,0,1,0][0,0,1,1,0][1,1,0,1,1]and some mask[1,0][0,1]What approach I should use to find sequence by mask?Sample array can be any size and mask can be any size tooI n
2014-09-01 11:16:12 279
pycharm2021社区版怎么删除项目?怎么让建立的所有项目在左侧的导航栏显示出来
2021-08-12
TA创建的收藏夹 TA关注的收藏夹
TA关注的人