# Newbie question: command line to execute a method from .rb file

**URL:** <https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559>\
**Category:** ruby-talk\
**Created:** [30 May 2012 16:26 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559 "2012-05-30T16:26:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [30 May 2012 16:26 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559/1 "2012-05-30T16:26:18Z")

</div>

Hi there -

So, I'm sorry for this very basic question which makes me feel like a  
real dummy... But honestly I couldn't find the answer from many internet  
tutorials, nor from the book I am reading, called "Ruby: the  
foundations...".

So, pretty basic, I have a test.rb file which contains:

def fonction1  
&nbsp;&nbsp;puts "Here we are - func1"  
end

def fonction2  
&nbsp;&nbsp;puts "Here we are - func2"  
end

def test(\*arg)  
&nbsp;&nbsp;countArgs = 0;  
&nbsp;&nbsp;arg.each {|param|  
&nbsp;&nbsp;&nbsp;&nbsp;t[countArgs] = param  
&nbsp;&nbsp;&nbsp;&nbsp;countArgs = countArgs+1  
&nbsp;&nbsp;}  
&nbsp;&nbsp;countArgs = countArgs+1  
&nbsp;&nbsp;puts "You called me with #{count-args} argument(s) as parameters"

&nbsp;&nbsp;return t  
end

Now, I'm still trying to figure out the COMMAND LINE to eventually  
execute my test() function of test.rb.  
I want to try on different cases, test(a,b,c), test("yoyo", "yaya"), ...

I tried ruby test.rb test() , but of course, this isn't it.

Let me know if I'm totally out of the concept here ?!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [30 May 2012 16:43 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559/2 "2012-05-30T16:43:38Z")

</div>

In the program you wrote, you are just defining the functions, but you  
are not calling them  
You need a section at the end that actually calls the test method, you  
cannot do it from the outside (well, you can, but it's not probably  
what you mean). If you want to pass to the test method any arguments  
you write on the command line you can do this:

test(\*ARGV)

and call it like this:

ruby test.rb 1 2 3 4

Little example:

def test \*args  
&nbsp;&nbsp;p args  
end

test(\*ARGV)

$ ruby test.rb 1 2 3 4  
["1", "2", "3", "4"]

Hope this helps,

Jesus.

PS: if you don't really want to call the test method inside your  
script, you can do:

ruby -I. -rtest -e "test(1,2,3,4)"

but I'm guessing this is not what you really want.

> **···**
>
> On Wed, May 30, 2012 at 6:26 PM, n/a n/a \<lists@ruby-forum.com\> wrote:
> 
> > Hi there -
> > 
> > So, I'm sorry for this very basic question which makes me feel like a  
> > real dummy... But honestly I couldn't find the answer from many internet  
> > tutorials, nor from the book I am reading, called "Ruby: the  
> > foundations...".
> > 
> > So, pretty basic, I have a test.rb file which contains:
> > 
> > def fonction1  
> > puts "Here we are - func1"  
> > end
> > 
> > def fonction2  
> > puts "Here we are - func2"  
> > end
> > 
> > def test(\*arg)  
> > countArgs = 0;  
> > arg.each {|param|  
> > t[countArgs] = param  
> > countArgs = countArgs+1  
> > }  
> > countArgs = countArgs+1  
> > puts "You called me with #{count-args} argument(s) as parameters"
> > 
> > return t  
> > end
> > 
> > Now, I'm still trying to figure out the COMMAND LINE to eventually  
> > execute my test() function of test.rb.  
> > I want to try on different cases, test(a,b,c), test("yoyo", "yaya"), ...
> > 
> > I tried ruby test.rb test() , but of course, this isn't it.
> > 
> > Let me know if I'm totally out of the concept here ?!

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [30 May 2012 16:47 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559/3 "2012-05-30T16:47:01Z")

</div>

> Hi there -
> 
> So, I'm sorry for this very basic question which makes me feel like a  
> real dummy... But honestly I couldn't find the answer from many internet  
> tutorials, nor from the book I am reading, called "Ruby: the  
> foundations...".
> 
> So, pretty basic, I have a test.rb file which contains:
> 
> def fonction1  
> puts "Here we are - func1"  
> end
> 
> def fonction2  
> puts "Here we are - func2"  
> end

Not sure what those functions are used or intended for - I cannot see  
any invocation of them.

> def test(\*arg)  
> countArgs = 0;  
> arg.each {|param|  
> t[countArgs] = param  
> countArgs = countArgs+1  
> }  
> countArgs = countArgs+1

This increment is too much. But you do not need to count manually  
because you can get the count directly via args.size.

> puts "You called me with #{count-args} argument(s) as parameters"
> 
> return t  
> end
> 
> Now, I'm still trying to figure out the COMMAND LINE to eventually  
> execute my test() function of test.rb.  
> I want to try on different cases, test(a,b,c), test("yoyo", "yaya"), ...

Well, if you want to do that just include those calls in the script:

test(a,b,c)  
test("yoyo", "yaya")

> I tried ruby test.rb test() , but of course, this isn't it.

If you want to find out the arguments to the program, then this is easiest:

puts "You called me with #{ARGV.size} argument(s) as parameters"

Then just invoke it with

$ ruby your-script.rb arg1 arg2

If you want to use your method #test to evaluate program arguments you can do

test(\*ARGV)

But I am not 100% sure I understood what you're after.

Kind regards

robert

> **···**
>
> On Wed, May 30, 2012 at 6:26 PM, n/a n/a \<lists@ruby-forum.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [30 May 2012 17:22 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559/4 "2012-05-30T17:22:04Z")

</div>

Ahhhh!

Thank you guys. Now I see clearer.  
Jesus got it right indeed; I wanted to call the function from the  
outside - on the command line.

What I didn't understand in Ruby is that the call of the functions were  
indeed to be written as part of the script !

OKAY.... Makes sense.

TY very much.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [30 May 2012 17:56 UTC](https://rubytalk.org/t/newbie-question-command-line-to-execute-a-method-from-rb-file/65559/5 "2012-05-30T17:56:37Z")

</div>

Well, you can also use the whole script as "function". Arguments are  
then in ARGV (see my previous posting).

Kind regards

robert

> **···**
>
> On Wed, May 30, 2012 at 7:22 PM, n1 n0x \<lists@ruby-forum.com\> wrote:
> 
> > Ahhhh!
> > 
> > Thank you guys. Now I see clearer.  
> > Jesus got it right indeed; I wanted to call the function from the  
> > outside - on the command line.
> > 
> > What I didn't understand in Ruby is that the call of the functions were  
> > indeed to be written as part of the script !
> > 
> > OKAY.... Makes sense.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
