2

I'm working my way through Ruby's Codecademy course and I'm up to here https://www.codecademy.com/en/courses/ruby-beginner-en-L3ZCI/1/6?curriculum_id=5059f8619189a5000201fbcb#

The correct code (that I don't understand) to convert the array of numbers into an array of strings is posted below.

    numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    strings_array = numbers_array.map(&:to_s)

I've been looking through the forums and googling and at this point I'm still confused what the heck is going on in this code.

I'm confused by :to_s because to_s means to convert to strings and : stands for symbols. Does :to_s convert stuff into symbols or strings?

I'm confused by & too. I get that the & sign makes the block that follows it into a proc, but here it's the colon for symbol and the method to convert to a string rather than a block that follows the & symbol.

I'd really appreciate any help!

Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
Gwater17
  • 2,018
  • 2
  • 19
  • 38
  • This comes up quite a bit (http://symbolhound.com/?q=ruby+%26%3A), which of those qualifies as a duplicate? – mu is too short Apr 29 '16 at 02:21
  • [Understanding \[ClassOne, ClassTwo\].each\(&:my_method\)](http://StackOverflow.Com/q/99318/2988), [What does `map(&:name)` mean in Ruby?](http://StackOverflow.Com/q/1217088/2988), [What exactly is `&:capitalize` in Ruby?](http://StackOverflow.Com/q/1792683/2988), [Ruby/Ruby on Rails ampersand colon shortcut](http://StackOverflow.Com/q/1961030/2988), [Ruby : `&:symbol` syntax](http://StackOverflow.Com/q/2096975/2988), [What is this `&:last` Ruby Construct Called?](http://StackOverflow.Com/q/2211751/2988), [What do you call the `&:` operator in Ruby?](http://StackOverflow.Com/q/2259775/2988), – Jörg W Mittag Apr 29 '16 at 08:40
  • [What does `map(&:name)` do in this Ruby code?](http://StackOverflow.Com/q/2388337/2988), [What are `:+` and `&+` in ruby?](http://StackOverflow.Com/q/2697024/2988), [`&:views_count` in `Post.published.collect(&:views_count)`](http://StackOverflow.Com/q/3888044/2988), [Ruby `Proc` Syntax](http://StackOverflow.Com/q/4512587/2988), [ruby on rails block sytax using `&:`](http://StackOverflow.Com/q/4942285/2988), [How does “`(1..4).inject(&:+)`” work in Ruby](http://StackOverflow.Com/q/5003257/2988), [Ruby map method syntax question](http://StackOverflow.Com/q/5231919/2988), – Jörg W Mittag Apr 29 '16 at 08:40
  • [What does following statement `&:property` ?](http://StackOverflow.Com/q/5620411/2988), [What does the `&` mean in the following ruby syntax?](http://StackOverflow.Com/q/5952175/2988), [Why would one use the unary operator on a property in ruby? i.e `&:first`](http://StackOverflow.Com/q/6289084/2988), [how does `Array#map` have parameter to do something like this?](http://StackOverflow.Com/q/6716629/2988), [What is the underlying code for `TableA.create(TableB.all.map(&:attributes))`?](http://StackOverflow.Com/q/7708087/2988), – Jörg W Mittag Apr 29 '16 at 08:41
  • [what does `&:` mean in ruby, is it a block mixed with a symbol?](http://StackOverflow.Com/q/9188362/2988), [what is the functionality of “`&:`” operator in ruby?](http://StackOverflow.Com/q/9429819/2988), [What does `Post.all.map(&:id)` mean?](http://StackOverflow.Com/q/9468564/2988), [What does “`temps.each(&:valid?)`” mean in Ruby?](http://StackOverflow.Com/q/9926724/2988), [What is the `&:` of `&:aFunction` doing?](http://StackOverflow.Com/q/11847336/2988), [What does this ampersand mean?](http://StackOverflow.Com/q/13383074/2988), – Jörg W Mittag Apr 29 '16 at 08:41
  • [whats does `.map(&:chomp)` do, exactly?](http://StackOverflow.Com/q/14250281/2988), [Ruby trying to grasp a new notation. (`inject(: )` vs `select(&:even?)`; why one has `&`?)](http://StackOverflow.Com/q/16605597/2988), [what does `&:` mean in ruby](http://StackOverflow.Com/q/16786658/2988), [Difference between passing `&:method` and `:method` as function arguments in ruby](http://StackOverflow.Com/q/17206254/2988), [What is the explanation for `&:hex` in ruby?](http://StackOverflow.Com/q/18366649/2988), – Jörg W Mittag Apr 29 '16 at 08:41
  • [What exactly does `(&:id)` do in `Product.all.map(&:id)`](http://StackOverflow.Com/q/18523675/2988), [Name for this type of abbreviation `map{|x| x.foo}` -> `map(&:foo)`](http://StackOverflow.Com/q/22849489/2988), [What does `&:to_s` mean in `b.map(&:to_s)`](http://StackOverflow.Com/q/23041002/2988), [Alternative style to passing a block to a method?](http://StackOverflow.Com/q/23620330/2988), [Understanding the `map(&:age).sum` syntax](http://StackOverflow.Com/q/23856876/2988), [I really do not get the 'symbol'](http://StackOverflow.Com/q/24419460/2988), – Jörg W Mittag Apr 29 '16 at 08:42
  • [Rails special symbol](http://StackOverflow.Com/q/25195698/2988), [Alternate syntax for Ruby `Array#map` using “`&`” character?](http://StackOverflow.Com/q/24637480/2988), [What is the use of the `&` operator in Ruby](http://StackOverflow.Com/q/25265311/2988), [what does `&:upcase` mean in Ruby](http://StackOverflow.Com/q/27054825/2988), [What does “`&:name`” mean in Ruby](http://StackOverflow.Com/q/28444892/2988), [Rails: What does this shorthand '`.map(&:downcase)`' mean?](http://StackOverflow.Com/q/32110352/2988) – Jörg W Mittag Apr 29 '16 at 08:42
  • @muistooshort: "A bit" :-D – Jörg W Mittag Apr 29 '16 at 08:42

1 Answers1

3

What happens is that the syntax &:to_s is another way constructing a block that calls the method that matches the symbol, on every item in the array. In your case, it is basically the same as writing:

strings_array = numbers_array.map { |number| number.to_s }

Here is another example:

strings_array = numbers_array.map(&:to_f)
# Same as
strings_array = numbers_array.map { |number| number.to_f }

For further explanation regarding the & sign, look at how you would define a method that accepts a block as an argument.

def map(&block)
  # ....
end

Now look at the different ways we could invoke this method that all will yield the same result.

# Block using bracket notation
map { |n| n.to_i }

# Block using do ... end notition
map do |n|
  n.to_i
end

# When using ampersand, ruby will try to treat the following as a 
# proc and use to proc as the block for the method. If we pass a proc
# to it, then no problem.
p = proc { |n| n.to_i }
map(&p)

# When using ampersand with a symbol (or any other non-proc objects), 
# ruby will try to call the to_proc method on the object and use that
# as a block. And in the scenario of passing a symbol, the Symbol 
# object will return a proc that calls the method that matches the symbol.
:to_i.to_proc => #<Proc:...>
map(&:to_i)
DanneManne
  • 21,107
  • 5
  • 57
  • 58
  • I understand the code you wrote, but I'm still confused how it translates to &:to_s. What part of what you wrote does the symbol sign replace? Can the & sign turn methods into procs as well as blocks? – Gwater17 Apr 29 '16 at 00:59
  • I see that the & sign can call methods now. I'm still confused by what the symbol sign does. – Gwater17 Apr 29 '16 at 02:49
  • Thinking of the symbol as a sign is probably where you are going wrong. You should be thinking of it as an object, just like a String or an Integer. It is an object that you can call methods on. – DanneManne Apr 29 '16 at 03:11
  • "Can the `&` sign turn methods into procs as well as blocks?" – It doesn't turn anything into a proc in this case. It turns something into a *block*. More precisely, it calls `to_proc` to convert an arbitrary object into a `Proc` and then turns that `Proc` into a block. `Method`s implement `to_proc`, but you aren't dealing with `Method`s here either. You are dealing with a `Symbol`, which, as luck will have it, *also* implements `to_proc`. – Jörg W Mittag Apr 29 '16 at 08:31