From: Jan Svitok Date: 2007-02-22T07:35:27+09:00 Subject: Re: Please illuminate how stupid I am re: initialization blocks. On 2/21/07, Michael Judge wrote: > I seem to use blocks much differently from other people and that > frightens me a little. (Maybe I'm being really dumb and nobody wants > to say anything.) > > Before I get into what I do, let me define how normal people call a > block passed in to a method. They use "yield". And that's cool, > that's what yield is intended for. Their methods tend to look like > this when they're later put into practical use: > > create_table :users do |t| > t.column :first, :string > t.column :last, :string > end > > If I had programmed create_table though, I would have used > instance_eval, and you'd use create_table like this: > > create_table :users do > column :first, :string > column :last, :string > end > > Am I being really dumb? Is there something horrifying that I'm not > grasping? > > Thanks for your help! Hi, the difference is in the scope: with yield you'll have access to variables outside the block, while with instance_eval you won't IIRC. Search the archive for instance_eval scope, e.g. in a thread named 'can there be a "with" construction?' somebody mentioned that 'self' changes when using instance_eval (self is the receiver of instance_eval) and doesn't with yield (stays as was outside the block).