Forum: Ruby Please illuminate how stupid I am re: initialization blocks.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b1b2c4661dd593b46ecd24492bc243d?d=identicon&s=25 Michael Judge (bluetrust)
on 2007-02-21 23:23
(Received via mailing list)
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
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2007-02-21 23:36
(Received via mailing list)
On 2/21/07, Michael Judge <mjudge@surveycomplete.com> 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).
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-02-21 23:39
(Received via mailing list)
Hi --

On Thu, 22 Feb 2007, Michael Judge 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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-02-21 23:42
(Received via mailing list)
On 2/21/07, Michael Judge <mjudge@surveycomplete.com> 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 Judge
>
>
Cheers
Robert
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-02-21 23:47
(Received via mailing list)
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-...

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


David
617b624fa01db9f6440439f3740b35e9?d=identicon&s=25 Marcello Barnaba (Guest)
on 2007-02-22 08:00
(Received via mailing list)
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-...

I've also a potentially interesting URI to paste:
http://blog.caboo.se/articles/2006/12/26/dsl-style-migration
This topic is locked and can not be replied to.