send de la clase
Object:
MacBookdeCasiano:chapter8ReflectionandMetaprogramming casiano$ cat -n dynamiccall.rb
1 class Tutu
2 def tutu(arg)
3 a = arg*2
4 yield a if block_given?
5 end
6 end
7
8 obj = Tutu.new
9 obj.tutu(4) { |m| puts "In block: #{m}" }
10 print "Action?: "
11 n = gets.chomp!.to_sym # Leer desde stdin, suprimir '\n' y convertirº
12 obj.send(n, 5) { |m| puts "In block: #{m}" }
Ejecución:
MacBookdeCasiano:chapter8ReflectionandMetaprogramming casiano$ ruby dynamiccall.rb In block: 8 Action?: tutu In block: 10
~/rubytesting$ cat -n benchcall.rb
1 require 'benchmark'
2 include Benchmark
3 test = "Comparing Calls"
4 m = test.method(:length)
5 n = 1000000
6 bm(12) {|x|
7 x.report("call") { n.times { m.call } }
8 x.report("send") { n.times { test.send(:length) } }
9 x.report("eval") { n.times { eval "test.length" } }
10 }
~/rubytesting$ ruby benchcall.rb
user system total real
call 0.210000 0.000000 0.210000 ( 0.209029)
send 0.240000 0.000000 0.240000 ( 0.247114)
eval 1.950000 0.000000 1.950000 ( 1.951107)