This question already has answers here:
Convert array of strings to an array of integers
(2 answers)
Closed 2 months ago.
I am facing an issue converting ruby array ["1", "2"] to [1,2]
we can use ["1", "2"].map{|n| eval n} but the use of eval is a serious security risk.
Any suggestions?
Just use:
["1", "2"].map(&:to_i)
#=> [1, 2]
Which is basically the same as
["1", "2"].map { |number| number.to_i }
See Array#map and String#to_i.
Related
This question already has answers here:
How to merge Ruby hashes
(4 answers)
Closed 6 years ago.
Note: that the two hashes has the with same key and the same values
a = {:ip=>'192.168.2.1',:b=>2}
b = {:ip=>'192.168.2.1',:c=>4}
i want to merge them to a new hash like this
newhash= {:ip=>'192.168.2.1',:b=>2,:c=4}
Use Hash#merge
a = {:ip=>"192.168.2.1", :b=>2}
b = {:ip=>"192.168.2.1", :c=>4}
newhash = a.merge(b)
#=> {:ip=>"192.168.2.1", :b=>2, :c=>4}
This question already has answers here:
How can I check if a Ruby array includes one of several values?
(5 answers)
Closed 8 years ago.
I have an array a = ["1","2","3","6","7"] and another array b = ["2","4","7"]. I want to check if any content of b exists in a or not.
You can do
a = ["1","2","3","6","7"]
b = ["2","4","7"]
b.any? { |e| a.include?(e) }
that is so simple:
(a & b).blank?
actually what does it do is, it takes intersection of two array and returns the result then check if the result is blank/empty.
Use & operator of Ruby, it will return an array with intersection values of two arrays, below is an example.
pry(main)> a = ["1","2","3","6","7"]
=> ["1", "2", "3", "6", "7"]
pry(main)> b = ["2","4","7"]
=> ["2", "4", "7"]
pry(main)> a & b
=> ["2", "7"]
pry(main)> (a & b).empty?
=> false
In Rails, you can also use blank?
pry(main)> (a & b).blank?
=> false
Hope the above example helps
This question already has answers here:
Ruby: filter array by regex?
(6 answers)
Closed 8 years ago.
I can intersect two arrays by doing:
keyphrase_matches = words & city.keywords
How can I achieve the same thing using regular expression? I want to test one array against a regular expression and get a new array with the matches.
You can use the Enumerable#grep method:
%w{a b c 1 2 3}.grep /\d/ # => ["1", "2", "3"]
Use array.grep(regex) to return all elements that match the given regex.
See Enumerable#grep.
As I understand, if arr1 and arr2 are two arrays of strings (though you did not say they contain strings), you want to know if a regular expression could be used to produce arr1 & arr2.
First some test data:
arr1 = "Now is the time for all good Rubyists".split
#=> ["Now", "is", "the", "time", "for", "all", "good", "Rubyists"]
arr2 = "to find time to have the good life".split
#=> ["to", "find", "time", "to", "have", "the", "good", "life"]
The result we want:
arr1 & arr2
#=> ["the", "time", "good"]
I can think of two ways you might use use Enumerable#grep, as suggested by #meagar and #August:
#1
arr1.select { |e| arr2.grep(/#{e}/).any? }
#=> ["the", "time", "good"]
#2
regex = Regexp.new("#{arr2.join('|')}")
#=> /to|find|time|to|have|the|good|life/
arr1.grep(regex)
#=> ["the", "time", "good"]
Of course, Array#& generally would be preferred, especially in Code Golf.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the * operator doing to this string in Ruby
I ran across the following code when looking for an easy way to convert an array to a hash (similar to .Net's ToDictionary method on IEnumerable... I wanted to be able to arbitrarily set the key and the value).
a = [ 1, 2, 3, 4, 5, 6 ]
h = Hash[ *a.collect { |v| [ v, v ] }.flatten ]
My question is, what does the asterisk before a.collect do?
By the way, the code comes from http://justatheory.com/computers/programming/ruby/array_to_hash_one_liner.html
It's the splat-operator if you want to google it. It does transform an array into a list (so you can use an array as arguments to a method). It also does the opposite: it can 'slurp' a list into an array.
require 'date'
*date_stuff = 2012,2,29 # slurp
p date_stuff #=> [2012, 2, 29]
Date.new(*date_stuff) # regurgitate
This question already has answers here:
How to chunk an array in Ruby
(2 answers)
Closed 5 years ago.
I have an array
foo = %w(1 2 3 4 5 6 7 8 9 10)
How can I split or "chunk" this into smaller arrays?
class Array
def chunk(size)
# return array of arrays
end
end
foo.chunk(3)
# => [[1,2,3],[4,5,6],[7,8,9],[10]]
Take a look at Enumerable#each_slice:
foo.each_slice(3).to_a
#=> [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"], ["10"]]
If you're using rails you can also use in_groups_of:
foo.in_groups_of(3)