Hi guys, would love your help with a few questions:
1) working on the RubyMonk.com primer.
It's pretty good, but it got vague and super challenging all of a
sudden. (or maybe i just got dumb.)
This is where I am stumped:
def say_hello(name)
"Hello, #{name}."
end
The output:
returns "Hello, Master." when say_hello("Master") is called
Error: returns "Hello, Qui-Gon Jinn."
when say_hello is called with no parameters
Error: wrong number of arguments (0 for 1)
The question:
How do I write a default value, to account for when the object is called
with no parameter?? I feel I've tried everything: googling/ testing,
re-reading
rubymonk/ testing, rereading ruby doc/ testing; can't get it!
2) in moving forward, how would you recommend I teach myself?
a. Michael Hartl's tutorials? or perhaps
b. Chris Pine's PragProg book: "Learn to Program"?
Hi guys, would love your help with a few questions:
Welcome to the wonderful world of Ruby!
1) working on the RubyMonk.com primer.
It's pretty good, but it got vague and super challenging all of a
sudden. (or maybe i just got dumb.)
This is where I am stumped:
def say_hello(name)
"Hello, #{name}."
end
The output:
returns "Hello, Master." when say_hello("Master") is called
Error: returns "Hello, Qui-Gon Jinn."
when say_hello is called with no parameters
Error: wrong number of arguments (0 for 1)
The question:
How do I write a default value, to account for when the object is called
with no parameter?? I feel I've tried everything: googling/ testing,
re-reading
Maybe it's because your terminology is wrong in one place: you call
(or invoke) a method - not an object. say_hello is a method.
Default arguments are declared with an assignment like this
def say_hello(name = "nobody")
"Hello, #{name}."
end
2) in moving forward, how would you recommend I teach myself?
a. Michael Hartl's tutorials? or perhaps
b. Chris Pine's PragProg book: "Learn to Program"?
I can't really comment on that - since I used neither to learn Ruby.
Chris Pine's book is recommended quite often so maybe that's a good
choice. Other than that what I find most helpful is to have real
problems or tasks I try to solve / fulfill and then I grab the
knowledge I need for the task at hand. The advantage is that the
motivation to solve things you really want to get solved is certainly
higher as for to tasks in tutorials.
Kind regards
robert
···
On Fri, Aug 17, 2012 at 6:57 PM, incag neato <lists@ruby-forum.com> wrote:
How do I write a default value, to account for when the object is called
with no parameter?? I feel I've tried everything: googling/ testing,
re-reading
rubymonk/ testing, rereading ruby doc/ testing; can't get it!
Perhaps your Google-fu needs some strengthening. What search string
did you use? Googling "ruby default method (parameters OR arguments)"
(sans quotes) ought to get you some good hits.
But to save you all that, there are a couple ways. The easy way, when
it's just a few optional args, is to use = and a value. (Note that
all optional args have to be at the end, and you can't skip some and
supply later ones.) In your case it would be something like:
def say_hello(name = "Qui-Gon Jinn")
There is a more complicated way, that will soak up an unlimited number
of additional args, but that's for another time.
2) in moving forward, how would you recommend I teach myself?
a. Michael Hartl's tutorials? or perhaps
b. Chris Pine's PragProg book: "Learn to Program"?
Both are excellent. It depends whether you're trying to learn
programming (Pine), the Ruby language (Pine), or the Ruby on Rails web
app framework (Hartl). If you're already familiar with programming in
other languages, you MIGHT do OK starting with Hartl and then
referring to sections in Pine (or any other Ruby resources) as needed.
-Dave
···
On Fri, Aug 17, 2012 at 12:57 PM, incag neato <lists@ruby-forum.com> wrote:
Default arguments are declared with an assignment like this
def say_hello(name = "nobody")
"Hello, #{name}."
end
Thanks so much Robert! Gotcha. The issue I'm confused with is accounting
for both:- setting the default value (for when no parameter is passed)
and also accepting an input.
I.e. -->
returns "Hello, Master." when say_hello("Master") is called
Error: returns "Hello, Qui-Gon Jinn."
when say_hello is called with no parameters
Error: wrong number of arguments (0 for 1)
Do I use something like a conditional? Something more like the below?
def say_hello(name)
if say_hello?
"Hello, #{name}."
else
"Hello, Qui-Gon Jinn."
end
end
Dave Aronson wrote in post #1072704:
Perhaps your Google-fu needs some strengthening.
I concur.
def say_hello(name = "Qui-Gon Jinn")
Boom.
There is a more complicated way, that will soak up an unlimited number
of additional args, but that's for another time.
I'll stretch my legs and dig in now that I get it.
2) in moving forward, how would you recommend I teach myself?
a. Michael Hartl's tutorials? or perhaps
b. Chris Pine's PragProg book: "Learn to Program"?
Both are excellent. It depends whether you're trying to learn
programming (Pine), the Ruby language (Pine), or the Ruby on Rails web
app framework (Hartl). If you're already familiar with programming in
other languages, you MIGHT do OK starting with Hartl and then
referring to sections in Pine (or any other Ruby resources) as needed.
Default arguments are declared with an assignment like this
def say_hello(name = "nobody")
"Hello, #{name}."
end
Thanks so much Robert! Gotcha. The issue I'm confused with is accounting
for both:- setting the default value (for when no parameter is passed)
and also accepting an input.
I.e. -->
returns "Hello, Master." when say_hello("Master") is called
Error: returns "Hello, Qui-Gon Jinn."
when say_hello is called with no parameters
Error: wrong number of arguments (0 for 1)
Do I use something like a conditional? Something more like the below?
def say_hello(name)
if say_hello?
"Hello, #{name}."
else
"Hello, Qui-Gon Jinn."
end
end
No, that does not work. Did you try it?
Not sure which approach to take.
Robert already gave you the solution to your problem:
"default arguments are declared with an assignment like this"