Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Object#send and blocks/procs

Reply
Thread Tools

Object#send and blocks/procs

 
 
Mehr, Assaph (Assaph)
Guest
Posts: n/a
 
      09-27-2004

Hi Guys,

I started to write this email as a question, but figured the answer in
the middle...
So now I want to know if there's any problem with the code below, if
there is any performance hit or if there's a simpler way to turn Procs
back into blocks.

The question is how do I #send a method to an object with an attached
proc (not block)? The simplest way to reproduce is:

prc = lambda{ |a| a.upcase }
puts "aaa".send(:gsub, /./) # how do I pass prc as the block to
gsub?

Solution:

puts "aaa".send(:gsub, /./) { |x| prc[x] }

i.e. I have wrapped my Proc object within a block. Some interspersing of
'*' may be required for multiple params, as Markus pointed out in
another thread.


Cheers,
Assaph




 
Reply With Quote
 
 
 
 
Gavin Sinclair
Guest
Posts: n/a
 
      09-27-2004
Assaph wrote:

> prc = lambda{ |a| a.upcase }
> puts "aaa".send(:gsub, /./) # how do I pass prc as the block to gsub?


Try this:

puts "aaa".send(:gsub, /./, &prc)

Cheers,
Gavin




 
Reply With Quote
 
 
 
 
George Ogata
Guest
Posts: n/a
 
      09-27-2004
"Mehr, Assaph (Assaph)" <(E-Mail Removed)> writes:

>
> Hi Guys,
>
> I started to write this email as a question, but figured the answer in
> the middle...
> So now I want to know if there's any problem with the code below, if
> there is any performance hit or if there's a simpler way to turn Procs
> back into blocks.
>
> The question is how do I #send a method to an object with an attached
> proc (not block)? The simplest way to reproduce is:
>
> prc = lambda{ |a| a.upcase }
> puts "aaa".send(:gsub, /./) # how do I pass prc as the block to
> gsub?
>
> Solution:
>
> puts "aaa".send(:gsub, /./) { |x| prc[x] }


puts 'aaa'.send(:gsub, /./, &prc)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments