Forum: Ruby How to "pass" the current binding's block to some other meth

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.
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-08 23:53
(Received via mailing list)
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?
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-02-09 00:00
(Received via mailing list)
On Feb 8, 3:52 pm, "Lyle Johnson" <lyle.john...@gmail.com> wrote:
>     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.)
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-09 00:12
(Received via mailing list)
On 2/8/07, Phrogz <gavin@refinery.com> wrote:

> 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).
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-02-09 00:50
(Received via mailing list)
On Feb 8, 4:11 pm, "Lyle Johnson" <lyle.john...@gmail.com> wrote:
> 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. :)
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 Mauricio Fernandez (Guest)
on 2007-02-09 00:55
(Received via mailing list)
On Fri, Feb 09, 2007 at 08:00:11AM +0900, Phrogz wrote:
> On Feb 8, 3:52 pm, "Lyle Johnson" <lyle.john...@gmail.com> 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

try(:a => 1) { "foo" }                             # => "foofoo"
RUBY_VERSION                                       # => "1.8.5"
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-02-09 01:21
(Received via mailing list)
Lyle Johnson wrote:
> 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).

Does this help?

   class Foo
     def try(x, y, z)
       # ... do some other stuff first ...
       yield self if block_given?
     end

     alias old_try try
     def try(hash)
       old_try(hash[:x], hash[:y], hash[:z]) do
         yield self if block_given?
       end
     end
   end

   Foo.new.try({}) do puts "trying" end
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-09 01:33
(Received via mailing list)
On 2/8/07, Phrogz <gavin@refinery.com> wrote:

> 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!
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-09 01:34
(Received via mailing list)
On 2/8/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:

>        old_try(hash[:x], hash[:y], hash[:z]) do
>          yield self if block_given?
>        end
>      end
>    end
>
>    Foo.new.try({}) do puts "trying" end

Ah, yes, that ought to do the trick too. Thanks!
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-02-09 01:52
(Received via mailing list)
Lyle Johnson wrote:
>>      alias old_try try
>>      def try(hash)
>>        old_try(hash[:x], hash[:y], hash[:z]) do
>>          yield self if block_given?
>>        end
>>      end
>>    end
>>
>>    Foo.new.try({}) do puts "trying" end
>
> Ah, yes, that ought to do the trick too. Thanks!

It *might* be faster, too, if that matters. It doesn't create a Proc
instance.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2007-02-09 07:31
(Received via mailing list)
On Feb 8, 2007, at 4:33 PM, Lyle Johnson wrote:

>>      alias old_try try
>
def try(x, y, z)
   # ... do some other stuff first ...
    yield self if block_given?
end

alias old_try try

  def try(hash)
     old_try(hash[:x], hash[:y], hash[:z], &yield)
   end


try( :a => 'foo') { puts 'hi' }


Cheers-

-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez@engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-02-09 08:41
(Received via mailing list)
Ezra Zygmuntowicz wrote:
>>>        yield self if block_given?
>>>    Foo.new.try({}) do puts "trying" end
> alias old_try try
>
>  def try(hash)
>     old_try(hash[:x], hash[:y], hash[:z], &yield)
>   end
>
>
> try( :a => 'foo') { puts 'hi' }

Cool, &yield is new to me!

But the OP wanted the following to work too, I think:

try( :a => 'foo')

and this will raise a LocalJumpError in this case...
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2007-02-09 09:54
(Received via mailing list)
Ezra Zygmuntowicz schrieb:
>  def old_try(x, y, z)
>    # ... do some other stuff first ...
>    yield self if block_given?
>  end
>
>  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 topic is locked and can not be replied to.