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?
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?
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.
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
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
···
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
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: