define_method, proc with block ?

  • Thread starter trans. (T. Onoma)
  • Start date
T

trans. (T. Onoma)

A little help here. I'm trying to dynamically create a method that passes
through to another method but adds in a fixed argument. Unfortunately I can't
pass a block via a proc. How does one handle this? I ended up having to
create two methods for each wrapped method. Yuk. Here's the code snip:

# NOTE: wrap_advice is the method/proc used to wrap meth
# meth is defined in a subclass or singleton, c, to achieve wrapping

# define the interceptor
c.class_eval {
define_method("advice___#{meth}",wrap_advice)
}
c.class_eval %Q{
def #{meth}(*args,&blk)
target = proc{super} #proc{|*args,&blk| super(*args,&blk)}
advice___#{meth}(target,*args,&blk)
end
}

Notice also I can't pass the block via the proc'd super either. I want to just
do this:

# define the interceptor
c.class_eval {
define_method(meth) {|*args,&blk|
target = proc{|*args,&blk| super(*args,&blk)}
wrap_advice.call(target,*args,&blk)
end
}

Any ideas?

Thanks,
T.
 
M

Markus

A little help here. I'm trying to dynamically create a method that passes
through to another method but adds in a fixed argument. Unfortunately I can't
pass a block via a proc.

Why not? Or more lucidly (asfter this post a swear I'm going for
tea): what problems are you having? It seems like either of the methods
that Mauricio

http://www.talkaboutprogramming.com/group/comp.lang.ruby/messages/107894.html

or I (about half way down, search for "$observers")

http://www.talkaboutprogramming.com/group/comp.lang.ruby/messages/107721.html,
suggested on this thread should do the trick.

-- MarkusQ
 
T

trans. (T. Onoma)

Why not? Or more lucidly (asfter this post a swear I'm going for
tea): what problems are you having? It seems like either of the methods
that Mauricio

http://www.talkaboutprogramming.com/group/comp.lang.ruby/messages/107894.ht
ml

Ah, thank you. Thank you. Yes, this first example is essentially what I'm
doing. I see from it that block parameters in Procs are taken care of in
1.9+, I just need to update.
or I (about half way down, search for "$observers")

http://www.talkaboutprogramming.com/group/comp.lang.ruby/messages/107721.ht
ml, suggested on this thread should do the trick.

No aliasing for me thanks. But I am curious, is Symbol.uniq new to 1.9?

Thanks Again,
T.
 
M

Markus

Ah, thank you. Thank you. Yes, this first example is essentially what I'm
doing. I see from it that block parameters in Procs are taken care of in
1.9+, I just need to update.

Actually, if you &-ize it on the way in you should be fine in 1.8
(at least, it works transparently for me).
No aliasing for me thanks. But I am curious, is Symbol.uniq new to 1.9?

Oops. Sorry, it's from my personal bag o'tricks (another ported
lisp-ism):

class Symbol
@@unique_sym_counter = 0
def Symbol.unique(prefix="gloop")
prefix =
prefix.to_s.
gsub(/\+/,'_plus_').
gsub(/\*/,'_times_').
gsub(/-/,'_minus_').
gsub(/\//,'_div_').
gsub(/\!/,'_bang_')
return ("#{prefix.to_s}__#{@@unique_sym_counter += 1}").intern
end
end

So, I'm curious right back: why the aversion to aliasing?

-- MarkusQ
 
M

Markus

Simple: method namespace clutter.

*smile* I used to have that problem with my web browser cache
directory--it kept getting cluttered up with all these files with
cryptic names. It was annoying the tar out of me. Then I decided to
try a trick I'd developed with my mail spool directory: I stopped
looking in there.

That cleared the problem right up.

-- MarkusQ
 
T

trans. (T. Onoma)

*smile* I used to have that problem with my web browser cache
directory--it kept getting cluttered up with all these files with
cryptic names. It was annoying the tar out of me. Then I decided to
try a trick I'd developed with my mail spool directory: I stopped
looking in there.

That cleared the problem right up.

LOL! :) Yea, well, there is that.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top