Calling a Method

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.

On Thu, Sep 18, 2008 at 01:28:15AM +0900, Rong wrote:

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…

[email protected] ~ $ cat test.rb
class Foo
def bar
puts “Oh Hai!”
end
end
[email protected] ~ $ 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

Rong wrote:

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?

[email protected]:~$ cat test.rb
def bar(p=ARGV[0])
puts “bar called with parameter ‘#{p}’”
end
[email protected]:~$ ruby -rtest -e ‘bar’ Hello
bar called with parameter ‘Hello’

Thanks Nathan.

On Thu, Sep 18, 2008 at 01:57:31AM +0900, Henning B. wrote:

[email protected]:~$ cat test.rb
def bar(p=ARGV[0])
puts “bar called with parameter ‘#{p}’”
end
[email protected]:~$ 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:

[email protected] ~ $ 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