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

  • Thread starter Iván Vega Rivera
  • Start date
I

Iván Vega Rivera

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.
 
A

ara.t.howard

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.

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

--=20
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
 
I

Iván Vega Rivera

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.
 
W

Wilson Bilkovich

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.

%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, 'somethi=
ng')}

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.
 
A

ara.t.howard

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" =3D> {"x" =3D> x}
end

def b x
y "b" =3D> {"x" =3D> 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

--=20
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama
 
I

Iván Vega Rivera

Oh I understand now!

Thanks!

Ivan V.

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.

%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, 'somet= hing')}

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.
 
I

Iván Vega Rivera

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.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top