From: Rick DeNatale Date: 2009-08-10T23:49:50+09:00 Subject: Re: Newbie has trouble understanding a Why example On Mon, Aug 10, 2009 at 10:28 AM, Ca Josephson wrote: > I'm just learning ruby, so I am working my way through 'Why's Poignant > Guide to Ruby' > > I'm on the 5th chapter, and I came across this example: > > class ArrayMine < Array >   # Build a string from this array, formatting each entry >   # then joining them together. >   def join( sep = $,, format = "%s" ) >     collect do |item| >       sprintf( format, item ) >     end.join( sep ) >   end >  end > > An example of the method in action: > >  rooms = ArrayMine[3, 4, 6] >  print "We have " + rooms.join( ", ", "%d bed" ) + " rooms available." > > Which prints, “We have 3 bed, 4 bed, 6 bed rooms available.” > > I'm confused by the line "def join( sep = $,, format = "%s" )". What on > earth is going on with the parameters? What do the  sep = $, and format > = "%s" do? If I modify that line to "def join( sep, format)", the same > thing prints out. So why are $, and %s used? What do they do? They provide default values for the parameters. try: print "We have " + rooms.join( ", " ) + " rooms available." or print "We have " + rooms.join + " rooms available." with and without your modification. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale