-2

The two lines in this code:

p ["9", "6", "4"]
p %w(9 6 4)

return exactly the same array ["9", "6", "4"]. But the first line with map:

p ["9", "6", "4"].map(&:to_i)

works fine, and the second one:

p %w(9 6 4).map{&:to_i}

gives:

syntax error, unexpected &
p %w(9 6 4).map{&:to_i}

I also tried to wrap it in bracers (%w(9 6 4)).map(&:to_i), but had no luck. What is the problem with this code? %w(...) is a shortcut for array of strings. Why is it not working in the same way?


UPD

Ok, I got it. That's a stupid question, but I can't delete this question, since it has answers already. I voted to close it..

skywinder
  • 21,291
  • 15
  • 93
  • 123

1 Answers1

2

Your second example is passing a raw block instead of a proc symbol.

Change

p %w(9 6 4).map{&:to_i}

to

p %w(9 6 4).map(&:to_i)

or

p %w(9 6 4).map {|n| n.to_i}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 1
    thanks. just a stupid mistake. I vote to close my question, since it doesn't make no sense for others. Thanks anyway! – skywinder Jun 06 '15 at 00:06