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
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?
print "We have " + rooms.join( ", " ) + " rooms available."
or
print "We have " + rooms.join + " rooms available."
with and without your modification.
···
On Mon, Aug 10, 2009 at 10:28 AM, Ca Josephson<cjosephson@ebscohost.com> 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
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?
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
···
On Mon, Aug 10, 2009 at 4:28 PM, Ca Josephson<cjosephson@ebscohost.com> 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
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?
--
Posted via http://www.ruby-forum.com/\.
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
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?
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"
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
···
On Mon, Aug 10, 2009 at 5:00 PM, steve<zyzygy@telstra.com> wrote:
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
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?
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"