From: sto.mar@... Date: 2012-08-18T03:02:16+09:00 Subject: Re: Default parameters? + (Noob) Guidance Am 17.08.2012 19:37, schrieb incag neato: >> 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" 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." --