Default parameters? + (Noob) Guidance

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!

The link:

http://rubymonk.com/learning/books/1/chapters/19-ruby-methods/lessons/69-new-lesson

Help!

  1. in moving forward, how would you recommend I teach myself?
    a. Michael H.'s tutorials? or perhaps
    b. Chris P.'s PragProg book: “Learn to Program”?

On Fri, Aug 17, 2012 at 6:57 PM, incag neato [email protected]
wrote:

Hi guys, would love your help with a few questions:

Welcome to the wonderful world of Ruby!

The output:
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

  1. in moving forward, how would you recommend I teach myself?
    a. Michael H.'s tutorials? or perhaps
    b. Chris P.'s PragProg book: “Learn to Program”?

I can’t really comment on that - since I used neither to learn Ruby.
Chris P.'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

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 :heavy_check_mark:
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

Not sure which approach to take.

Am 17.08.2012 19:37, schrieb incag neato:

  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”

1.9.3-p194 :001 > def say_hello(name = “nobody”)
1.9.3-p194 :002?> “Hello, #{name}.”
1.9.3-p194 :003?> end
=> nil
1.9.3-p194 :004 > say_hello
=> “Hello, nobody.”
1.9.3-p194 :005 > say_hello(‘Master’)
=> “Hello, Master.”

unknown wrote in post #1072703:

Am 17.08.2012 19:37, schrieb incag neato:

No, that does not work. Did you try it?

Thanks. Yes, I had tried it. I knew it did not work.

Robert already gave you the solution to your problem:
“default arguments are declared with an assignment like this”

Figured it out!!

I didn’t understand that a method with a default parameter could take
yet another object as well.

Nicee

On Fri, Aug 17, 2012 at 12:57 PM, incag neato [email protected]
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. :slight_smile:

  1. in moving forward, how would you recommend I teach myself?
    a. Michael H.'s tutorials? or perhaps
    b. Chris P.'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

Dave A. 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. :slight_smile:

I’ll stretch my legs and dig in now that I get it.

  1. in moving forward, how would you recommend I teach myself?
    a. Michael H.'s tutorials? or perhaps
    b. Chris P.'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.

Thanks for the breakdown man! Much appreciated.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs