Forum: Ruby Passing the same argument(s) to several methods at once

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.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-03-08 02:12
(Received via mailing list)
Hi,

I'm a Ruby beginner, but I remember I read somewhere something
related... Only at the time I didn't realize it's usefulness until
now.

Maybe I'm mistaken, but is it possible to pass the same argument(s) to
several functions at once?

Like:

function_a 'something'
function_b 'something'

Instead something like:

'something'.pass do |arg|
  function_a arg
  function_b arg
end

Does that make sense?

Thanks in advance!

Ivan V.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-08 02:30
(Received via mailing list)
On Wed, 8 Mar 2006, [ISO-8859-1] Iván Vega Rivera wrote:

>
> Does that make sense?
>
> Thanks in advance!
>
> Ivan V.

the general case is:

   %w( function_a function_b ).each{|m| receiver.send(m, *args, &block)}

works for any number of args, any receiver, and any block or lack of.

hth.

-a
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-03-08 02:51
(Received via mailing list)
Thanks but could you explain me a little bit more how that works? Ruby
says 'block' is undefined. So I'm missing something.

The problem is I don't quite understand how that code works :-)

Ivan V.
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-03-08 03:03
(Received via mailing list)
On 3/7/06, Iván Vega Rivera <ivanvega@gmail.com> wrote:
> >
> >   %w( function_a function_b ).each{|m| receiver.send(m, *args, &block)}
> >
> > works for any number of args, any receiver, and any block or lack of.
> >

%w(function_a function_b) creates an Array that looks like this:
['function_a', 'function_b']

'each' is an instance method of Array (via the Enumerable module)
that, when given a block, 'passes' each entry of the array to the
block one by one.
In this case, the block variable is 'm', and so 'm' will be the string
'function_a', and then the string 'function_b'.

Let's modify your original example a little to show what object
'function_a' and 'function_b' are methods on.

someobject.function_a('something')
someobject.function_b('something')

..and let's rename that block variable to make what is going on a
little clearer:
%w(function_a function_b).each {|message| someobject.send(message,
'something')}

By 'general case', Ara means that someobject.send(m, *args, &block)
will handy any kind of method signature that 'm' might happen to have,
no matter how many parameters it expects.
Methods can choose to explicitly accepts blocks as parameters in Ruby,
though it's a little uncommon in typical code.
'send' invokes a method by name, which is handy for this kind of thing.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-08 03:03
(Received via mailing list)
On Wed, 8 Mar 2006, [ISO-8859-1] Iván Vega Rivera wrote:
>>
>> -a

> Thanks but could you explain me a little bit more how that works? Ruby
> says 'block' is undefined. So I'm missing something.
>
> The problem is I don't quite understand how that code works :-)
>
> Ivan V.

a method that takes a block will/can have this signature

   def m(*args, &block)
   end


here is the simplest possible example:

     harp:~ > cat a.rb
     require 'yaml'

     def a x
       y "a" => {"x" => x}
     end

     def b x
       y "b" => {"x" => x}
     end

     %w( a b ).each{|m| send m, 42}


     harp:~ > ruby a.rb
     ---
     a:
       x: 42
     ---
     b:
       x: 42


get a copy of 'the ruby way', the 'pickaxe', or google this group to
learn
about blocks.

kind regards.


-a
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-03-08 03:27
(Received via mailing list)
Oh I understand now!

Thanks!

Ivan V.
C5380b957ff8ce5cb14cf2282ffeb720?d=identicon&s=25 Iván Vega Rivera (Guest)
on 2006-03-08 03:30
(Received via mailing list)
That's kind of what I remember reading, and it was indeed on 'the ruby
way'. Unfortunately, no matter how much I read, I still can't get into
my thick head how blocks work... I wish I just could erase my current
knowledge with other languages... it just gets in the way!

So back to burning the midnight oil it is...

Kind regards,
Ivan V.
This topic is locked and can not be replied to.