with or without arguments - how?

J

Jason Lillywhite

I've used methods before that work with or without arguments. I've tried
to figure it out but can't get to the bottom of it. Could someone give
me a hint how you would go about creating a method that works for either
of these cases?

mymethod(x, y)
x + y
end

Then somehow get this to work without the (x, y) arguments...? What I
want is to have a method that can do something on default if no
arguments are spec'd or I can refine what it does by adding arguments.
 
C

Chris Shea

I've used methods before that work with or without arguments. I've tried
to figure it out but can't get to the bottom of it. Could someone give
me a hint how you would go about creating a method that works for either
of these cases?

mymethod(x, y)
  x + y
end

Then somehow get this to work without the (x, y) arguments...? What I
want is to have a method that can do something on default if no
arguments are spec'd or I can refine what it does by adding arguments.

What methods? Have you looked at the source for those methods? Have
you read Programming Ruby? What about this section (http://www.ruby-
doc.org/docs/ProgrammingRuby/html/tut_methods.html)?

HTH,
Chris
 
V

Victor H. Goff III

[Note: parts of this message were removed to make it a legal post.]

Try this:
my_method(x = 1, y = 1)
x + y
end

Warmest Regards,

Victor H. Goff III


On Fri, Jan 23, 2009 at 11:01 PM, Jason Lillywhite <
 
R

Robert Klemme

OK, sorry this was a dumb question. I missed default values.

.... and probably the splash operator as well which can be used to
similar purpose.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top