Forum: Ruby Confusion with Array#map

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-03-25 19:30
Hi could anyone help me how does the below concept helped to produce the
output?

%w{ david black }.map(&:capitalize)
#=> ["David", "Black"]
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-03-25 19:34
map iterates through the given array and collects the results with a
block executed on them, in this case interpreting the argument symbol
:capitalize into a proc { |s| s.capitalize }
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-03-25 19:37
Joel Pearson wrote in post #1103136:
> map iterates through the given array and collects the results with a
> block executed on them, in this case interpreting the argument symbol
> :capitalize into a proc { |s| s.capitalize }


How `proc` object is getting called by `call` to produce the output? Who
is calling that?
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse mailing lists (Guest)
on 2013-03-25 19:42
(Received via mailing list)
On Mon, Mar 25, 2013 at 1:37 PM, Love U Ruby <lists@ruby-forum.com>
wrote:
> Joel Pearson wrote in post #1103136:
>> map iterates through the given array and collects the results with a
>> block executed on them, in this case interpreting the argument symbol
>> :capitalize into a proc { |s| s.capitalize }
>
>
> How `proc` object is getting called by `call` to produce the output? Who
> is calling that?

The operator "&" is the block operator, which is turning the symbol
:capitalize which refers to the method capitalize in that context.
Since at that point, map is sending in strings, it says to call the
capitalize method on that string.
https://en.wikibooks.org/wiki/Ruby_Programming/Syn...
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-03-25 20:03
The below code giving empty array.

[2,3][-1,0]
#=> []

But why does the code below giving nil value?

[2,3][0,-1]
#=> nil
60b83bb4579cd684731a1709f85ee849?d=identicon&s=25 John W Higgins (Guest)
on 2013-03-25 20:14
(Received via mailing list)
[start, size]

Think about the size -1 -

What exactly would an array with -1 members look like?

What would an array with 0 members look like?

John
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-03-25 22:59
(Received via mailing list)
On Mon, Mar 25, 2013 at 7:41 PM, tamouse mailing lists
<tamouse.lists@gmail.com> wrote:
> On Mon, Mar 25, 2013 at 1:37 PM, Love U Ruby <lists@ruby-forum.com> wrote:

>> How `proc` object is getting called by `call` to produce the output? Who
>> is calling that?
>
> The operator "&" is the block operator, which is turning the symbol
> :capitalize which refers to the method capitalize in that context.
> Since at that point, map is sending in strings, it says to call the
> capitalize method on that string.
>
https://en.wikibooks.org/wiki/Ruby_Programming/Syn...

The crucial bit here is that Symbol#to_proc exists.  Actually anything
can be used in that way:

irb(main):001:0> o = Object.new
=> #<Object:0x802bdf10>
irb(main):002:0> def o.to_proc; lambda {|x| "<#{x.inspect}>"}
irb(main):003:1> end
=> nil
irb(main):004:0> (1..5).map(&o)
=> ["<1>", "<2>", "<3>", "<4>", "<5>"]

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.