&:

&:

The ampersand-colon combo turns out to be quite useful in saving keystrokes. What I had been doing was creating a new array of numbers. To do so quickly, I needed to use %w, which makes an array of strings.

ary = %w(23 45 68 120 345 760 1220)

The old way I converted to a number was the following:

ary.map! { |x| x.to_i }

(Naturally, I prefer map over collect, which does the same thing, not just because of the keystroke, but because I used Scheme before.) I discovered a shorter way:

ary.map!(&:to_i)

Actually, instead of doing it separately, why not combine the whole thing?

ary = %w(23 45 68 120 345 760 1220).map(&:to_i)

It saves using exclamation point and feels less dangerous.

Advertisement

About AJ

In the future, he hopes that this blogsite will be humorous, informative, and enjoyable for the visitors.
This entry was posted in Uncategorized. Bookmark the permalink.