Method objects?


(Tyler Spivey) #1

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.


(Christian Szegedy) #2

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


(Nobuyoshi Nakada) #3

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


(Holden Glova) #4

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.


(Guaracy Monteiro) #5

(…)

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

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

HP: www.guaracy.cjb.net


(Joel VanderWerf) #6

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


(Wilkes Joiner) #7

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()
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


Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup