calling random

B

Boris Schmid

Apologies, I have been out of using ruby for a while, and ran into
trouble when I tried to do the below. What is the normal way to call
apply a list of arguments to a list of functions?

def f1(n)
puts "f1: #{n}"
end

def f2(n)
puts "f2: #{n}"
end

irb(main):008:0> [f1,f2].each {|fun| [1,2,3,4].each {|val| fun(val)}}
 
B

Boris Schmid

Sorry for the mismatched title, a more appropriate one would be: "how to
combine a list of functions with a list of arguments?"
 
A

Alex Fenton

Boris said:
Apologies, I have been out of using ruby for a while, and ran into
trouble when I tried to do the below. What is the normal way to call
apply a list of arguments to a list of functions?

def f1(n)
puts "f1: #{n}"
end

def f2(n)
puts "f2: #{n}"
end

[:f1, :f2].each do | fun |
[1, 2, 3, 4].each { | x | Object.send(fun, x) }
end

Ruby doesn't have functions, just methods. What look like functions
above (a def outside a class body) creates methods in the Object class.

"send" is the generic way to call any object's method using a name. It
accepts a string or symbol (hence :f1 above) followed by the arguments.

a
 
D

David A. Black

Hi --

Apologies, I have been out of using ruby for a while, and ran into
trouble when I tried to do the below. What is the normal way to call
apply a list of arguments to a list of functions?

def f1(n)
puts "f1: #{n}"
end

def f2(n)
puts "f2: #{n}"
end

irb(main):008:0> [f1,f2].each {|fun| [1,2,3,4].each {|val| fun(val)}}

If f1 and f2 contain actual method objects, you can use call:

f1,f2 = method:)f1), method:)f2)
[f1,f2].each {|m| [1,2,3,4].each {|e| m.call(e) } }

Mixing f(unction) and m(ethod) terminology a bit... but that's OK,
because it will also work if f1 and f2 are Proc objects (which are
basically anonymous functions.

If you have strings or symbols, you can use them to get the method
objects (see above), or you can use send:

["f1", "f2"].each {|m| [1,2,3,4].each {|e| send(m,e)} }


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
B

Boris Schmid

Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times.. $pop is
about 5000 hosts large.

10000.times {
# Events to happen to every host this 0.1 year:
["birth","death","infect","evolve"].shuffle.each {|event|

next if event == "birth" and EVENTS_BIRTH > rand
next if event == "death" and EVENTS_DEATH > rand
next if event == "infect" and EVENTS_INFECTION > rand
next if event == "evolve" and EVENTS_EVOLUTION > rand
$pop.each {|host|
next if host == nil
send(event,host)
}
}
}

What I could do is make birth,death,infect and evolve methods of the
host class, and perhaps use call then. Does that make sense?

Hi --

end

irb(main):008:0> [f1,f2].each {|fun| [1,2,3,4].each {|val| fun(val)}}

If f1 and f2 contain actual method objects, you can use call:

f1,f2 = method:)f1), method:)f2)
[f1,f2].each {|m| [1,2,3,4].each {|e| m.call(e) } }

Mixing f(unction) and m(ethod) terminology a bit... but that's OK,
because it will also work if f1 and f2 are Proc objects (which are
basically anonymous functions.

If you have strings or symbols, you can use them to get the method
objects (see above), or you can use send:

["f1", "f2"].each {|m| [1,2,3,4].each {|e| send(m,e)} }


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
B

Boris Schmid

Boris said:
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times.. $pop is
about 5000 hosts large.

Ah. It's not that bad. send("fib",2) is about twice as slow as fib(2).
So I lose 2 minutes in 2 hours of simulation, which is fine.

Thanks for the answers!
 
A

Alex Fenton

Boris said:
Am I correct if I remember that using send is quite expensive in
cpu-time? Especially when I am using it millions of times.. $pop is
about 5000 hosts large.

10000.times {
# Events to happen to every host this 0.1 year:
["birth","death","infect","evolve"].shuffle.each {|event|

Using #send with a String argument is slower than using it with a
Symbol. Benchmark:

-------
require 'benchmark'
class Foo
def bar; end
end

a_foo = Foo.new

TIMES = 1_000_000

puts "Direct", Benchmark::measure { TIMES.times { a_foo.bar } }
puts "Symbol", Benchmark::measure { TIMES.times { a_foo.send:)bar) }
puts "String", Benchmark::measure { TIMES.times { a_foo.send("bar") } }
-------

With Ruby 1.8:

Direct
0.290000 0.000000 0.290000 ( 0.294786)
Symbol
0.380000 0.000000 0.380000 ( 0.385319)
String
0.550000 0.000000 0.550000 ( 0.556780)

With Ruby 1.9, the difference between #send(a_symbol) and calling the
method directly is negligible:

Direct
0.210000 0.000000 0.210000 ( 0.209512)
Symbol
0.210000 0.000000 0.210000 ( 0.210357)
String
0.490000 0.000000 0.490000 ( 0.500568)

alex
 
D

David A. Black

Hi --

Boris said:
Am I correct if I remember that using send is quite expensive in cpu-time?
Especially when I am using it millions of times.. $pop is about 5000 hosts
large.

10000.times {
# Events to happen to every host this 0.1 year:
["birth","death","infect","evolve"].shuffle.each {|event|

Using #send with a String argument is slower than using it with a Symbol.
Benchmark:

-------
require 'benchmark'
class Foo
def bar; end
end

a_foo = Foo.new

TIMES = 1_000_000

puts "Direct", Benchmark::measure { TIMES.times { a_foo.bar } }
puts "Symbol", Benchmark::measure { TIMES.times { a_foo.send:)bar) }
puts "String", Benchmark::measure { TIMES.times { a_foo.send("bar") } }
-------

With Ruby 1.8:

Direct
0.290000 0.000000 0.290000 ( 0.294786)
Symbol
0.380000 0.000000 0.380000 ( 0.385319)
String
0.550000 0.000000 0.550000 ( 0.556780)

With Ruby 1.9, the difference between #send(a_symbol) and calling the method
directly is negligible:

Direct
0.210000 0.000000 0.210000 ( 0.209512)
Symbol
0.210000 0.000000 0.210000 ( 0.210357)
String
0.490000 0.000000 0.490000 ( 0.500568)

However, calling #intern/to_sym is (slightly) slower than just sending
a string. This is Ruby 1.9.1:

Direct
0.150000 0.000000 0.150000 ( 0.157290)
Symbol
0.170000 0.000000 0.170000 ( 0.166353)
String
0.470000 0.000000 0.470000 ( 0.476854)
#intern
0.520000 0.020000 0.540000 ( 0.537574)

So if you've got a string already, it's probably not worth doing the
conversion explicitly.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
K

Ken Bloom

Ah. It's not that bad. send("fib",2) is about twice as slow as fib(2).
So I lose 2 minutes in 2 hours of simulation, which is fine.

Thanks for the answers!

It should be a bit faster if you use :fib instead of "fib", because the
ruby runtime has to internally "fib" to :fib every time you call send().

--Ken
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

Sorry for the mismatched title, a more appropriate one would be: "how to
combine a list of functions with a list of arguments?"
procedures as arguments to a function?
if so, then try:
def foo(x, y)
x.call
y.call
end

foo proc { puts 'x' }, proc { puts 'y' }

Anyway i could spend hours trying to understand that question.
 

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
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top