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.

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!

···

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.

--
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?

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.

···

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

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'

--
nathan
nathan_at_nathanpowell_dot_org

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

Thanks Nathan.

···

On Sep 17, 12:02 pm, Nathan Powell <nat...@nathanpowell.org> wrote:

On Thu, Sep 18, 2008 at 01:57:31AM +0900, Henning Bekel wrote:
> 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
------------------------------------