%w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.
Using new class method A Ruby array is constructed by calling ::new method with zero, one or more than one arguments. Syntax: arrayName = Array. new.
str_array = int_array.map(&:to_s)
str_array = int_array.collect{|i| i.to_s}
array.map(&:to_s) => array of integers into an array of strings
array.map(&:to_i) => array of strings into an array of integers
map and collect functions will work the same here.
int_array = [1, 2, 3]
str_array = int_array.map { |i| i.to_s }
=> str_array = ['1', '2', '3']
You can acheive this with one line:
array = [1, 2, 3]
array.map! { |i| i.to_s }
and you can use a really cool shortcut for proc: (https://stackoverflow.com/a/1961118/2257912)
array = [1, 2, 3]
array.map!(&:to_s)
Start up irb
irb(main):001:0> int_array = [11,12]
=> [11, 12]
irb(main):002:0> str_array = int_array.collect{|i| i.to_s}
=> ["11", "12"]
Your problem is probably somewhere else. Perhaps a scope confusion?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With