Calling a Method

R

Rong

If I have a file that contains a method that takes one parameter. and
I want to call the method from the command line, how do you do that?
I know how to run a script from the command line but not how to call a
method.
 
N

Nathan Powell

If I have a file that contains a method that takes one parameter. and
I want to call the method from the command line, how do you do that?
I know how to run a script from the command line but not how to call a
method.

I'll be interested to see if there is a better way to do this...

npowell@delilah ~ $ cat test.rb
class Foo
def bar
puts "Oh Hai!"
end
end
npowell@delilah ~ $ ruby -e 'require "test"; f = Foo.new; f.bar;'
Oh Hai!

--
nathan
nathan_at_nathanpowell_dot_org

Trust no thought arrived at sitting down.
~ George Sheehan
------------------------------------
 
H

Henning Bekel

Rong said:
If I have a file that contains a method that takes one parameter. and
I want to call the method from the command line, how do you do that?
I know how to run a script from the command line but not how to call a
method.

Is this what you mean?

henning@box:~$ cat test.rb
def bar(p=ARGV[0])
puts "bar called with parameter '#{p}'"
end
henning@box:~$ ruby -rtest -e 'bar' Hello
bar called with parameter 'Hello'
 
N

Nathan Powell

henning@box:~$ cat test.rb
def bar(p=ARGV[0])
puts "bar called with parameter '#{p}'"
end
henning@box:~$ ruby -rtest -e 'bar' Hello
bar called with parameter 'Hello'

Nice. I should have looked at the Ruby man page. -r makes sense.

Though I would have done this:

npowell@delilah ~ $ ruby -r test -e 'bar("Oh Hai")'
Oh Hai

Just to keep from having to add the ARGV[0] as the default value.
--
nathan
nathan_at_nathanpowell_dot_org

Now I know what a statesman is; he's a dead politician. We need more statesmen.
~ Bob Edwards
------------------------------------
 
R

Rong

Thanks Nathan.

henning@box:~$ cat test.rb
def bar(p=3DARGV[0])
=A0 puts "bar called with parameter '#{p}'"
end
henning@box:~$ ruby -rtest -e 'bar' Hello
bar called with parameter 'Hello'

Nice. I should have looked at the Ruby man page. =A0-r makes sense.

Though I would have done this:

npowell@delilah ~ $ ruby -r test -e 'bar("Oh Hai")'
Oh Hai

Just to keep from having to add the ARGV[0] as the default value.
--
nathan
nathan_at_nathanpowell_dot_org

Now I know what a statesman is; he's a dead politician. We need more stat= esmen.
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0~ Bob Edwards
------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top