Suppose I have a method that will yield to a block if one is given:
def try(x, y, z)
# ... do some other stuff first ...
yield self if block_given?
end
Now suppose that I create an alias to this method, for the purpose of
overriding how it’s called, e.g.
alias old_try try
def try(hash)
old_try(hash[:x], hash[:y], hash[:z])
end
If someone calls this new and improved version of try() and provides a
block, is there any way for me to somehow pass that block down into
old_try() without actually modifying the method signature?
alias old_try try
def try(hash)
old_try(hash[:x], hash[:y], hash[:z])
end
If someone calls this new and improved version of try() and provides a
block, is there any way for me to somehow pass that block down into
old_try() without actually modifying the method signature?
Do you feel that this:
def try( hash, &block )
old_try( hash[:x],hash[:y],hash[:z],&block )
end
is somehow changing its signature? (You can still choose to pass a
block or not.)
Do you feel that this:
def try( hash, &block )
old_try( hash[:x],hash[:y],hash[:z],&block )
end
is somehow changing its signature?
For the purposes of this discussion, yes. Perhaps a better way to
phrase the question is whether there’s any way to do it without
modifying the original source code for try().
(You can still choose to pass a block or not.)
Yes, I get what you’re saying. I’m just hoping to not have to modify
the original source (if possible).
On Fri, Feb 09, 2007 at 08:00:11AM +0900, Phrogz wrote:
On Feb 8, 3:52 pm, “Lyle J.” [email protected] wrote:
[…]
def try( hash, &block )
old_try( hash[:x],hash[:y],hash[:z],&block )
end
is somehow changing its signature? (You can still choose to pass a
block or not.)
def old_try
yield * 2
end
def try(hash)
old_try(&Proc.new) # probably breaking in 1.9 someday (works right
now)
end
modifying the original source code for try().
To be clear: you only need to modify your new (overriding) try
method with the &block notation. The original try method’s source code
remains unchanged.
(I hope this is what you want. If you can’t modify the source code for
your overriding method, I’m not sure how you would intend to use any
code that would pass the block along.
To be clear: you only need to modify your new (overriding) try
method with the &block notation. The original try method’s source code
remains unchanged.
Huh. I tried that with my “real” code and got an error (i.e. with Ruby
claiming that the number of arguments passed into old_try was
incorrect). But I just wrote up a short example to test it and it
worked as you described.
OK. I need to figure out what’s different about my code. Thanks for
the lead on this!
def try(hash)
old_try(hash[:x], hash[:y], hash[:z], &yield)
end
Ezra, this isn’t working. In this case:
try( :a => ‘foo’) { puts ‘hi’ }
yield is calling the block, which returns nil. Calling old_try with &nil
is the same as calling it without a block, so block_given? is always
false in old_try.
Regards,
Pit
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.