From: "David A. Black" Date: 2008-09-01T17:26:39+09:00 Subject: Re: instance_eval/class_eval including/extending modules HI -- On Mon, 1 Sep 2008, Patrick Li wrote: > Now that I have a firmer grasp on Ruby's object system, is it possible > to combine some of these methods, so that they're easier to understand? > > > Here's some changes that can be made: > > > Let me know what you think > -Patrick > > What it does: > Combines eval/instance_eval/class_eval into one consistent method > Combines include/extend into one consistent method It's not right to say that these things are inconsistent. They're all different from each other, and they all do things that make sense and are needed frequently. include and extend are related, but they're not in conflict; they're complementary, and they both do extremely useful things. > Changes: > > eval(proc) > -eval is now a method of Object. This ALWAYS sets self to the receiver > object, > > and then evaluates proc. > > def myMethod > -def is now a method of Module. This creates a method named myMethod() > that > > can be called by instances of the receiver. > > Consequences: This means that if self is not a subclass of Module, you > cannot > > call def. Instead I propose a metaclass method that returns an objects > > metaclass. Join the club :-) Well, I call it Object#singleton_class, but there's been a request pending for this method for years. > So instead of: > obj.instance_eval do > def myMethod; end > end > > you MUST do: > obj.metaclass.instance_eval do > def myMethod; end > end Why? What's the advantage of taking away the current instance_eval scenario? > include myModule > -becomes a method of Object. This method puts the method definitions in > myModule into the receiver object. I think you're trying to squeeze things together too tightly. Right now they're pretty well-engineered to handle the major use-cases and not to overlap with each other. I'd recommend living with them a little longer, and playing around with them a bit -- I think you'll see that they're all pulling their weight pretty well. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!