Thank you David,
I stand corrected (actually I’m sitting)
So… I have been re-reading the best darn Ruby/Rails book on the
planet (Ruby for Rails of course! which I already read cover to cover
once) and I am still a bit confused but it’s getting better.
Maybe someone could be so kind as to critique my comments below about
what I think is happening. I have been studying this little bit code
for about 6 … no 8, hours and hopefully, I have most of it correct.
(personally I find it irritating when people ask questions without
putting any personal effort into finding the answers themselves)
module Rails
module Info
mattr_accessor :properties #module level property
assessor (getter and setter)
class << (@@properties = []) #Open up Array class def to
add methods (class << object), object == an Array
def names #define
Array.properties.names method
map {|(name, )| name} #read code block to return
array comprised of first half of [name, value]
end
def value_for(property_name) #define
Array.properties.value_for(“name param”) method
find {|(name, )| name == property_name}.last rescue nil
#return value part of [name, value] for sent name param
end
end #close Array class def
class << self #:nodoc: #open "Info" class def to add
methods
def property(name, value = nil) #define property “setter”
except the yield makes it a “getter”
value ||= yield #if value is nil, yield name
to code block in names and value_for
properties << [name, value] if value #push [name, value]
array into @@properties array
rescue Exception #ignore error?
end
…
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.
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.
Thanks again.