# Newbie has trouble understanding a Why example

**URL:** <https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498>\
**Category:** Ruby\
**Created:** [August 10, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498 "2009-08-10T16:28:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjosephson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@cjosephson](https://www.ruby-forum.com/u/cjosephson)\
**Post date:** [August 10, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498/1 "2009-08-10T16:28:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cjosephson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@cjosephson](https://www.ruby-forum.com/u/cjosephson)\
**Post date:** [August 10, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498/2 "2009-08-10T17:02:01Z")

</div>

Ca Josephson wrote:

> ```
> sprintf( format, item )
> 
> ```
> 
> 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?

Its a little confusing. In the method definition, sep= and format=  
indicate the default values for those parameters. The default value for  
sep is the value of the global variable $, and the the default format  
string is “%s”

now $, is the default system output separator and is nil unless  
explicitly set

C:\Documents and Settings\Administrator\>irb  
irb(main):001:0\> puts $,  
nil  
=\> nil  
irb(main):002:0\> %w(a b c d e f g).join  
=\> “abcdefg”  
irb(main):003:0\> $,=":"  
=\> “:”  
irb(main):004:0\> %w(a b c d e f g).join  
=\> “a:b:c:d:e:f:g”

does that make more sense?

Steve

---

<div class="post-metadata">

**Author:** ![cjosephson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@cjosephson](https://www.ruby-forum.com/u/cjosephson)\
**Post date:** [August 10, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498/3 "2009-08-10T17:23:05Z")

</div>

On Mon, Aug 10, 2009 at 4:28 PM, Ca Josephson[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> sprintf( format, item )
> 
> ```
> 
> ## 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?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

This is rather a hint than an answer, if you want a more explicit  
answer do not hesitate to ask again:  
irb(main):004:0\> def a sep=“,”, format=“_"  
irb(main):005:1\> p [sep, format]  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> a 41, 42  
[41, 42]  
=\> [41, 42]  
irb(main):008:0\> a 41  
[41, "_”]  
=\> [41, “_“]  
irb(main):009:0\> a  
[”,", "_”]  
=\> [“,”, “\*”]

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![cjosephson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@cjosephson](https://www.ruby-forum.com/u/cjosephson)\
**Post date:** [August 10, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498/4 "2009-08-10T17:23:41Z")

</div>

On Mon, Aug 10, 2009 at 10:28 AM, Ca Josephson[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> sprintf( format, item )
> 
> ```
> 
> 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/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![cjosephson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@cjosephson](https://www.ruby-forum.com/u/cjosephson)\
**Post date:** [August 10, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/newbie-has-trouble-understanding-a-why-example/173498/5 "2009-08-10T20:15:12Z")

</div>

On Mon, Aug 10, 2009 at 5:00 PM, steve[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def join( sep = $, format = “%s” )
> 
> =\> “:”  
> irb(main):004:0\> %w(a b c d e f g).join  
> =\> “a:b:c:d:e:f:g”
> 
> does that make more sense?
> 
> Steve

No idea, but if the implementation of join looks like this, it would

def join by  
reduce{ |s,e| “#{s}#{by||$,}#{e}” }  
end

HTH  
Robert
