# Calling a Method

**URL:** <https://www.ruby-forum.com/t/calling-a-method/148853>\
**Category:** Ruby\
**Created:** [September 17, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/calling-a-method/148853 "2008-09-17T19:17:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 17, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/calling-a-method/148853/1 "2008-09-17T19:17:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 17, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/calling-a-method/148853/2 "2008-09-17T19:17:18Z")

</div>

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…

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

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 17, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/calling-a-method/148853/3 "2008-09-17T19:18:29Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 18, 2008, 12:56am UTC](https://www.ruby-forum.com/t/calling-a-method/148853/4 "2008-09-18T00:56:04Z")

</div>

Thanks Nathan.

---

<div class="post-metadata">

**Author:** ![rgreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@rgreen](https://www.ruby-forum.com/u/rgreen)\
**Post date:** [September 17, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/calling-a-method/148853/5 "2008-09-17T19:23:47Z")

</div>

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