is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
end
or is that just not possible? if it isn’t that’s not very good for te project i’m trying to hack up.
Tyler Spivey wrote:
is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
end
or is that just not possible? if it isn’t that’s not very good for te project i’m trying to hack up.
Try this:
def a
puts ‘a’
end
def b
puts ‘b’
end
def c
[:a,:b].each { |x| send x }
end
c
Hi,
is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
end
or is that just not possible? if it isn’t that’s not very good for te project i’m trying to hack up.
With Method object:
a = [method(:func1), method(:func2), method(:func3)]
a.each do |f|
f.call()
end
Sending Symbols:
a = [:func1, :func2, :func3]
a.each do |f|
send(f)
end
···
At Mon, 1 Jul 2002 03:22:12 +0900, Tyler Spivey wrote:
–
Nobu Nakada
If those 3 methods are to be executed in order, then they sound pretty related
to each. Perhaps they belong better in a class? Anyhow, what you want to do
is possible because methods are objects, make sure you call() the method you
want to run.
Hope this helps somewhat.
Signed,
Holden Glova
···
On Mon, 01 Jul 2002 06:22, Tyler Spivey wrote:
is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
end
or is that just not possible? if it isn’t that’s not very good for te
project i’m trying to hack up.
(…)
With Method object:
a = [method(:func1), method(:func2), method(:func3)]
a.each do |f|
f.call()
endSending Symbols:
a = [:func1, :func2, :func3]
a.each do |f|
send(f)
end
with an array of strings
a = [‘func1’, ‘func2’, ‘func3’]
a.each {|fn|
method(fn).call
}
Guaracy
···
----- Original Message -----
From: nobu.nokada@softhome.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Tyler Spivey wrote:
is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
end
Or, if you just want your func1, func2, etc. to be functions without a self:
func1 = proc { puts “do something” }
#…
a = [func1, …]
a.each do |f|
f.call # or f
end
or
def func1
puts “do something”
end
a = [:func1, …]
a.each do |f|
method(f).call
end
···
— Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote:
Tyler Spivey wrote:
is it possible to write a function liike:
a=[func1,func2,func3]
a.each do |f|
f()
endOr, if you just want your func1, func2, etc. to be functions without a self:
func1 = proc { puts “do something” }
#…a = [func1, …]
a.each do |f|
f.call # or f
end
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com