[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing params to Map method

Ruby Freak

3/25/2008 3:23:00 AM

Hi and thanks in advance for your help.
I have been pounding every Ruby and Rails book I can find, desperately
attempting to move beyond newbie status.My latest foray has taken me
into the native 2.0.2 Rails::Info module. I am reading what I think is
native ruby code but I can't seem to figure out what is being passed
to map and find. Could someone please explain (name, ) in the
following code?

Thanks again.

module Rails
module Info
mattr_accessor :properties

class << (@@properties = [])
def names
map {|(name, )| name}
end

def value_for(property_name)
find {|(name, )| name == property_name}.last rescue nil
end
end
3 Answers

David A. Black

3/25/2008 3:57:00 AM

0

Hi --

On Tue, 25 Mar 2008, Ruby Freak wrote:

> Hi and thanks in advance for your help.
> I have been pounding every Ruby and Rails book I can find, desperately
> attempting to move beyond newbie status.My latest foray has taken me
> into the native 2.0.2 Rails::Info module. I am reading what I think is
> native ruby code but I can't seem to figure out what is being passed
> to map and find. Could someone please explain (name, ) in the
> following code?
>
> Thanks again.
>
> module Rails
> module Info
> mattr_accessor :properties
>
> class << (@@properties = [])
> def names
> map {|(name, )| name}
> end
>
> def value_for(property_name)
> find {|(name, )| name == property_name}.last rescue nil
> end
> end

find and map are not being passed any arguments; rather, they're being
given code blocks, and those blocks are being passed arguments.
(name,) is block parameter syntax for grabbing one element of an array
and putting it in name.


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
CORE RAILS June 24-27 London (Skills Matter)
See http://www.r... for more info!

Robert Klemme

3/26/2008 12:13:00 PM

0

2008/3/26, Ruby Freak <twscannell@gmail.com>:

> What I think is happening is the new "Info.properties" accessor is
> cleverly leveraging self.property as a method to both set a new
> property into @@properties and to return (yield) just the name when
> the parameter passed to the code block is just the first half of the
> array (name, ). Just understanding that after 8 months and thousands
> of hours and reading thousands of pages of books is really exciting
> for me.

Yep. Methods #names and #values_for are defined for the one instance
of Array only that is referenced by @@properties. So these are not
general methods. But they rely on methods defined in Array and
Enumerable to do their job. Basically they seem to rely on the fact
that @@properties contains arrays with exactly two elements - the
first one used as property name and the second one as property value.
During iteration (i.e. map, find) on the array only the first is
assigned a block parameter (small optimization because the second one
is not needed). That's it basically. This is the usual pattern
matching that Ruby applies to multiple assignments:

irb(main):001:0> (a,(b,c),d)=1,2,3,4,5
=> [1, 2, 3, 4, 5]
irb(main):002:0> a
=> 1
irb(main):003:0> b
=> 2
irb(main):004:0> c
=> nil
irb(main):005:0> d
=> 3
irb(main):006:0> (a,(b,c),d)=1,[2,3],4,5
=> [1, [2, 3], 4, 5]
irb(main):007:0> a
=> 1
irb(main):008:0> b
=> 2
irb(main):009:0> c
=> 3
irb(main):010:0> d
=> 4

What makes this a bit hard to read is that assignment to @@properties
and the class<<@@properties are lumped into one statement.

> I am a little iffy on the "class << (@@properties = [])" opening up
> the Array object, but it fits the rule.(class << object)
>
> if that is true, it is pretty cool code. (it's pretty cool even if I
> am wrong) Ruby has quite a learning curve, especially for a VB guy.

Yeah, it seems VB is a bad place to start learning to program. :-)

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Brian Adkins

3/27/2008 1:15:00 PM

0

On Mar 24, 11:57 pm, "David A. Black" <dbl...@rubypal.com> wrote:
> ...
> find and map are not being passed any arguments; rather, they're being
> given code blocks, and those blocks are being passed arguments.
> (name,) is block parameter syntax for grabbing one element of an array
> and putting it in name.

From your description "block parameter syntax", someone might conclude
that this is something specific to blocks, but it's more generally the
syntax for destructuring an array. For example:

(x,) = [7,8,9] # x => 7
(x,y,z) = [7,8,9]

or even

x, = [7,8,9]

In the OP's example, I think map {|a| a[0] } is clearer, but if the
parameter is used multiple times, e.g. map {|a,| "#{a}-#{a}" }, then
assigning to 'a' initially is more concise.