I've been reading lots of Ruby tutorials trying to figure out
metaclassing - the one that got me closest was Why?s poignant guide to
Ruby, but I'm still struggling over some of the Ruby-specific syntax.
I attached a class I've been writing that needs to be highly extensible,
and it's loaded with comments I wrote to try to understand each part of
it. If any part of the comments are wrong, it would be nice to have my
misconceptions cleared up. I've only been using Ruby for a day and, wow.
Here's the part I'm having the hardest time figuring out, and Why?s
guide kind of skipped over it:
metaclass.instance_eval do
define_method( a ) do |val|
@attribs ||= {}
@attribs[a] = val
end
end
So, I understand that the block inside the instance_eval will be
executed in the instance of the object(meaning when I create a new
object, right?)
However, I don't understand the next part. define_method is a method
call to the method that is defined further down, right? But what is the
" do |val|" after the method call, and what is the point of the two
lines after that?
If anyone could explain this to me, I would be greatly indebted.
Attachments:
http://www.ruby-forum.com/attachment/776/user_cleaned.rb
···
--
Posted via http://www.ruby-forum.com/.