Why prefer def style over block magic for ib_action?

0 views
Skip to first unread message

Nagnag

unread,
Jan 8, 2008, 4:53:54 AM1/8/08
to Rucola
Hi Eloy,

you said in your screencast, that you prefer the use of the def style
over the use of block magic for the declaration of an ib_action.

# def style
def download(sender)
puts 'Huii!'
end
ib_action :download

# block magic
ib_action :download do |sender|
puts 'Huii!'
end

Why? Has this a profound reason?

nag nag,
the Nagnag

Eloy Duran

unread,
Jan 8, 2008, 4:59:54 AM1/8/08
to ruc...@googlegroups.com
Hi Nagnag,

No there's no special reason at all.
It's just that I used this already before #ib_action even existed
and so that's just the way I'm used to doing it.

I also find it to be more intuitive,
since an ib_action is nothing more than a instance method.

Cheers,
Eloy

Nagnag

unread,
Jan 8, 2008, 7:22:50 AM1/8/08
to Rucola
Hi Eloy,

thanks for your hyper fast reply :-)
This question is a result of a mistake I did as I followed your
screencast.

I tried to change the name of the ib_action (using def style). I
changed the name of the def but forgot to change the name for the
ib_action call.
So, as I updated the nib file with rake, the new name for the action
was not available :-(.

With block magic there is only one line to master (DRY). So, as you
say there is no special reason, I am able to decide, to use the block
magic in the furture.

Thx for help,
Nagnag

Eloy Duran

unread,
Jan 8, 2008, 7:39:21 AM1/8/08
to ruc...@googlegroups.com
Ah I see.
Yes it's definitely more DRY to use the block variant.

I just don't like the looks of it :)

You're welcome.
Cheers,
Eloy

Nagnag

unread,
Jan 8, 2008, 8:34:04 AM1/8/08
to Rucola
I agree. The 'def style' looks a little bit better ;-)

Cheers,
Nagnag

Psychs

unread,
Jan 20, 2008, 1:26:36 AM1/20/08
to Rucola
Hi,

With the block style ib_action, you cannot use return in it.
Because it's a block.

It's the reason why I didn't use the block style ib_action.
I prefer the def style even if it's not DRY.

--
Satoshi Nakagawa

eloy.d...@gmail.com

unread,
Jan 20, 2008, 7:00:41 AM1/20/08
to Rucola
Hi Satoshi,

Well, you can't use the "return" statement.
But you can return values with an implicit return.
Because when the block is passed to ib_action
it will actually define a real instance method (something like this):

def ib_action(mname, &block)
define_method(mname, &block)
# more stuff for ib actions
end

So you can do for instance:

ib_action :foo do |sender|
# you can 't use the 'return' statement in a block
# return 'blah'

# but without the return statement it works
'blah'
end

However, I still prefer the non-block way :)

Cheers,
Eloy

Psychs

unread,
Jan 21, 2008, 3:38:18 AM1/21/08
to Rucola
Hi Eloy,

Sure, I know that way.
But sometimes I want to write parameters checks at the head of a
function like:

def func(param)
return nil unless param
...

However with the block style, I can't do that.
It's inconvenient for me.
So I select the def style.

--
Satoshi Nakagawa

On 1月20日, 午後9:00, "eloy.de.en...@gmail.com" <eloy.de.en...@gmail.com>
wrote:

Eloy Duran

unread,
Jan 21, 2008, 3:48:18 AM1/21/08
to ruc...@googlegroups.com
Yes, I agree.
Let's agree we agree :)

Eloy

Reply all
Reply to author
Forward
0 new messages