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

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

On Wed, May 30, 2012 at 6:26 PM, n/a n/a [email protected] wrote:

def fonction1
arg.each {|param|
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 ?!

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
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 [email protected] wrote:

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

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.

On Wed, May 30, 2012 at 7:22 PM, n1 n0x [email protected] 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.

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

Kind regards

robert