# Please illuminate how stupid I am re: initialization blocks

**URL:** <https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391>\
**Category:** Ruby\
**Created:** [February 21, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391 "2007-02-21T23:23:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 21, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/1 "2007-02-21T23:23:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 21, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/2 "2007-02-21T23:36:01Z")

</div>

On 2/21/07, Michael J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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).

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 21, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/3 "2007-02-21T23:39:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 21, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/4 "2007-02-21T23:47:02Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 22, 2007, 8:00am UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/5 "2007-02-22T08:00:41Z")

</div>

Hi,

On Wednesday 21 February 2007 23:46, removed\_email\_address@domain.invalid wrote:

> It occurs to me that you might find this interesting:  
> [http://dablog.rubypal.com/articles/2007/02/18/the-magic-pens-of-ruby](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](http://blog.caboo.se/articles/2006/12/26/dsl-style-migration)

---

<div class="post-metadata">

**Author:** ![bluetrust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8e7dd6/32.png) [@bluetrust](https://www.ruby-forum.com/u/bluetrust)\
**Post date:** [February 21, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/please-illuminate-how-stupid-i-am-re-initialization-blocks/86391/6 "2007-02-21T23:42:01Z")

</div>

On 2/21/07, Michael J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 😉  
> 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 😉  
> Hopefully others will point out more issues.
> 
> Thanks for your help!
> 
> - Michael J.

Cheers  
Robert
