Find arguments... is this possible

I want to get the names of the arguments that are passed into a method.

Example:

def my_method(first, second, third)
puts
end

my_method 1, 2, 3

Should output:
first
second
third

Is there any way to do this?

Michael Hale - Software Developer
RoleModel Software Inc. - The XP Software Studio
www.rolemodelsoftware.com

Hi –

I want to get the names of the arguments that are passed into a method.

Example:

def my_method(first, second, third)
puts
end

my_method 1, 2, 3

Should output:
first
second
third

Is there any way to do this?

irb(main):021:0> def my_method(first,second,third)
irb(main):022:1> puts local_variables
irb(main):023:1> end
nil
irb(main):024:0> my_method(1,2,3)
first
second
third

(give or take hidden pitfalls, and of course the question I dare not
ask, which is why one would want to do this :slight_smile:

David

···

On Sat, 23 Nov 2002, Michael Hale wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

“Why would one want to do this?”

I suspect the answer will be, uhmmm, interesting.

···

On Sat, Nov 23, 2002 at 05:08:18AM +0900, dblack@candle.superlink.net wrote:

Hi –

On Sat, 23 Nov 2002, Michael Hale wrote:

I want to get the names of the arguments that are passed into a method.

Example:

def my_method(first, second, third)
puts
end

my_method 1, 2, 3

Should output:
first
second
third

Is there any way to do this?

irb(main):021:0> def my_method(first,second,third)
irb(main):022:1> puts local_variables
irb(main):023:1> end
nil
irb(main):024:0> my_method(1,2,3)
first
second
third

(give or take hidden pitfalls, and of course the question I dare not
ask, which is why one would want to do this :slight_smile:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

“You, sir, are nothing but a pathetically lame salesdroid!
I fart in your general direction!”
– Randseed on #Linux

(give or take hidden pitfalls, and of course the question I dare not
ask, which is why one would want to do this :slight_smile:

pigeon% irb
irb(main):001:0> a_variable_with_a_very_long_name = 12
12
irb(main):002:0> a_variable_with_a_very_long_name
12
irb(main):003:0>
pigeon%

For the second line, the input was

   a_<TAB>

Guy Decoux

I noticed that many times I just hang on to the constuctor variables and
use them later. This code allows me to do that automatically.

class MyClass
def initialize(first, second)
local_variables.each { |each| eval(“@#{each} = #{each}”)
}
end

def a_method
	p instance_variables.collect { |each| eval("#{each}") }
end

end

MyClass.new(‘val1’,‘val2’).a_method

Produces:
[“val1”, “val2”]

···

-----Original Message-----
From: dblack@candle.superlink.net [mailto:dblack@candle.superlink.net]
Sent: Friday, November 22, 2002 3:08 PM
To: ruby-talk ML
Subject: Re: find arguments… is this possible

Hi –

On Sat, 23 Nov 2002, Michael Hale wrote:

I want to get the names of the arguments that are passed into a
method.

Example:

def my_method(first, second, third)
puts
end

my_method 1, 2, 3

Should output:
first
second
third

Is there any way to do this?

irb(main):021:0> def my_method(first,second,third)
irb(main):022:1> puts local_variables
irb(main):023:1> end
nil
irb(main):024:0> my_method(1,2,3)
first
second
third

(give or take hidden pitfalls, and of course the question I dare not
ask, which is why one would want to do this :slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Michael Hale wrote:

I noticed that many times I just hang on to the constuctor variables and
use them later. This code allows me to do that automatically.

class MyClass
def initialize(first, second)
local_variables.each { |each| eval(“@#{each} = #{each}”)
}
end

def a_method
p instance_variables.collect { |each| eval(“#{each}”) }
end
end

MyClass.new(‘val1’,‘val2’).a_method

Produces:
[“val1”, “val2”]

How about something like:

class Class
def init_simple(*syms)
strs = syms.map {|s| s.id2name}
self.module_eval %{def initialize #{strs.join(‘,’)}; #{strs.map {|s|
“@#{s} = #{s};”}.join} end}
end
end

Then you just use this to create constructors:

class C
self.init_simple :a, :b, :c

def sum
@a + @b + @c
end
end

C.new(1, 2, 3).sum # 6

Alternatively, you can piggyback a Struct:

C = Struct.new(‘C’, :a, :b, :c)
class C
def sum
a + b + c
end
end

C.new(1, 2, 3).sum # 6

George Ogata wrote:

How about something like:

class Class
def init_simple(*syms)
strs = syms.map {|s| s.id2name}
self.module_eval %{def initialize #{strs.join(‘,’)}; #{strs.map {|s|

  ^^^^^  Actually, you can leave this 'self.' out.

“@#{s} = #{s};”}.join} end}
end
end

Then you just use this to create constructors:

class C
self.init_simple :a, :b, :c

^^^^^  This one too.
···

def sum
@a + @b + @c
end
end

C.new(1, 2, 3).sum # 6

Alternatively, you can piggyback a Struct:

C = Struct.new(‘C’, :a, :b, :c)
class C
def sum
a + b + c
end
end

C.new(1, 2, 3).sum # 6