Robert Klemme
3/26/2008 12:13:00 PM
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