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


#1

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.


#2

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 :slight_smile:

Ivan V.


#3

On Wed, 8 Mar 2006, [ISO-8859-1] Iván Vega R. 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


#4

On 3/7/06, Iván Vega R. removed_email_address@domain.invalid 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.


#5

On Wed, 8 Mar 2006, [ISO-8859-1] Iván Vega R. 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 :slight_smile:

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


#6

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.


#7

Oh I understand now!

Thanks!

Ivan V.