Hi -- On Mon, 14 Nov 2005, Christian Leskowsky wrote: > Thanks for the reply guys! > > This last one looks much better to my eyes, but I'm still having problems. > > I like the code I write to tell as much to the reader about what's going on > as it possibly can-right there (without having to jump to other methods, in > other classes, in other modules). > > I think I'd probably have a hard time figuring out what: > > C.new do |c| > c.x = 1 > end > > did, without having: > > class C > attr_accessor :x > def initialize > yield self > end > end > > being directly above it (on the same screen somewhere). It might take a few > more keystrokes to write: > > c = C.new > c.x = 1 > > in general, but to me, it's much more readable. Actually I just showed the construct without assignment. In full you'd want to assign it: mc = MyClass.new do |m| m.x = 1 end (Easier to choose names with a multi-letter class name :-) Or if c existed already: c = nil C.new do |c| etc. because the block params pick up any existing variables and use those. (This may change circa 2.0.) Also, most of the time you see this, there's more than one assignment going on in the block, so it has a nice encapsulating feel. Mind you, all of this would have to go hand-in-hand with documentation. You probably wouldn't have the method code visible, but you'd have to know that MyClass.new yielded the new instance. David > > The other thing that bugs me (and it's probably just an unfortunate > example), is that it looks like I'm passing out a reference to myself, > before I'm completely initialized. ugh! > > Strange stuff... I think I'll just stay about as for away from it as I can > :). > > > -- > - > > 'There was an owl lived in an oak. > The more he heard, the less he spoke. > The less he spoke, the more he heard.' > > Christian Leskowsky > christian.leskowsky / gmail.com > > > > > On 11/13/05, David A. Black <dblack / wobblini.net> wrote: >> >> Hi -- >> >> On Sun, 13 Nov 2005, Christian Leskowsky wrote: >> >>> Hey guys, >>> >>> I'm very new to ruby. >>> >>> This is some freaky shite! Could somebody give me a use case as to when >> I'd >>> want to do something like this? >> >> Somewhat rarely :-) I actually prefer to know what 'self' is when I >> write a block. I'm uneasy with the idea that here: >> >> some_method { another_method } >> >> I can't tell by looking who's receiving the another_method message. >> >> A more common idiom is to provide the object so that you can see >> exactly who's doing what: >> >> class C >> attr_accessor :x >> def initialize >> yield self >> end >> end >> >> C.new do |c| >> c.x = 1 >> end >> >> and things like that. >> >> >> David >> >> -- >> David A. Black >> dblack / wobblini.net >> >> > -- David A. Black dblack / wobblini.net