Please illuminate how stupid I am re: initialization blocks

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!

  • Michael J.

On 2/21/07, Michael J. [email protected] wrote:

 t.column :first, :string

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).

Hi –

On Thu, 22 Feb 2007, Michael J. wrote:

t.column :first, :string

Am I being really dumb? Is there something horrifying that I’m not
grasping?

People do it that way too. I personally don’t like it, because it’s
weird to me to have ‘self’ change in a way that I can’t see:

@x = :first
create_table :users do
column @x, :string # different @x!
end

I think instance_eval works best as a big clunky method name that
draws attention to itself, and not as something folded away invisibly.

David

Hi –

On Thu, 22 Feb 2007, Michael J. 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.)

It occurs to me that you might find this interesting:

http://dablog.rubypal.com/articles/2007/02/18/the-magic-pens-of-ruby

It’s not a pro/con discussion but is a discussion and explication of
the idiom of yielding one object to a block.

David

Hi,

On Wednesday 21 February 2007 23:46, [email protected] wrote:

It occurs to me that you might find this interesting:
http://dablog.rubypal.com/articles/2007/02/18/the-magic-pens-of-ruby

I’ve also a potentially interesting URI to paste:
http://blog.caboo.se/articles/2006/12/26/dsl-style-migration

On 2/21/07, Michael J. [email protected] wrote:

 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:
amongst many other things instance eval would expose private methods,
so there is for sure some reason why create_table calls the block with
an object instead of instance_evalling it

create_table :users do
column :first, :string
column :last, :string
end

Now of course instance_eval is not necessarily a bad idea, especially
if you prepare the object well in which instance_eval is called. -
which might be much work though.
It is a well known DSL technique.

Am I being really dumb?
I let you be the Judge of that statement :wink:
Is there something horrifying that I’m not
grasping?
As I said there are some dangers you might not have thought of, but I
would not be as extreme :wink:
Hopefully others will point out more issues.

Thanks for your help!

  • Michael J.

Cheers
Robert