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 Judge

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

···

On 2/21/07, Michael Judge <mjudge@surveycomplete.com> 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 --

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?

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

···

On Thu, 22 Feb 2007, Michael Judge wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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:

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 Judge

Cheers
Robert

···

On 2/21/07, Michael Judge <mjudge@surveycomplete.com> wrote:

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

Hi --

···

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

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hi,

···

On Wednesday 21 February 2007 23:46, dblack@wobblini.net 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

--
pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF