Forum: Ruby calling random

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.
409bbe5af67a049f87e019ac0275dbc1?d=identicon&s=25 Boris Schmid (bor_)
on 2009-02-08 17:15
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)}}
409bbe5af67a049f87e019ac0275dbc1?d=identicon&s=25 Boris Schmid (bor_)
on 2009-02-08 17:17
Sorry for the mismatched title, a more appropriate one would be: "how to
combine a list of functions with a list of arguments?"
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 Alex Fenton (Guest)
on 2009-02-08 17:32
(Received via mailing list)
Boris Schmid wrote:
> 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-08 17:39
(Received via mailing list)
Hi --

On Mon, 9 Feb 2009, Boris Schmid wrote:

> 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!
409bbe5af67a049f87e019ac0275dbc1?d=identicon&s=25 Boris Schmid (bor_)
on 2009-02-08 19:58
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?


David A. Black wrote:
> Hi --
>
> On Mon, 9 Feb 2009, Boris Schmid wrote:
>
>> 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!
409bbe5af67a049f87e019ac0275dbc1?d=identicon&s=25 Boris Schmid (bor_)
on 2009-02-08 21:57
Boris Schmid wrote:
> 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!
669b7046f02e5dfc4bda4421f1069731?d=identicon&s=25 Alex Fenton (Guest)
on 2009-02-08 23:10
(Received via mailing list)
Boris Schmid wrote:
> 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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-08 23:17
(Received via mailing list)
Hi --

On Mon, 9 Feb 2009, Alex Fenton wrote:

> Benchmark:
>
>  0.380000   0.000000   0.380000 (  0.385319)
> 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!
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-09 05:27
(Received via mailing list)
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-02-09 06:10
(Received via mailing list)
On Sun, 08 Feb 2009 15:56:04 -0500, Boris Schmid wrote:

> Boris Schmid wrote:
>> 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!

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
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-02-09 09:00
(Received via mailing list)
On Mon, Feb 9, 2009 at 2:16 AM, Boris Schmid <borisschmid@gmail.com>
wrote:

> Sorry for the mismatched title, a more appropriate one would be: "how to
> combine a list of functions with a list of arguments?"
> --
> Posted via http://www.ruby-forum.com/.
>
> I'm taking a stab in the dark here but are you trying to pass more than one
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.
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2009-02-12 20:38
(Received via mailing list)
On Mon, Feb 09, 2009 at 07:15:35AM +0900, David A. Black wrote:
> #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.

It's probably worth it if you can organize your code to do explicity
conversion once vs. doing implict conversion through many iterations,
though.
This topic is locked and can not be replied to.