Object#send and blocks/procs

  • Thread starter Mehr, Assaph (Assaph)
  • Start date
M

Mehr, Assaph (Assaph)

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
 
G

Gavin Sinclair

Assaph said:
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
 
G

George Ogata

Mehr said:
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)
 

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