From: Abder-Rahman Ali on
I have the following script that I just have only slight changes in the
hash from "Why's poignant guide to Ruby" book:

http://pastie.org/private/ojmcfglofwloqemqmuhra

I have those questions regarding the script:

1- I have inserted this method call after the class:

puts "Abder-Rahman-is-my-name".name_significance


And, got the following message:

sub.rb:7:in `name_significance': undefined method `[]' for nil:NilClass
(NoMethodError)
from sub.rb:6:in `collect'
from sub.rb:6:in `name_significance'
from sub.rb:13

How can I call the name_significance method?

2- How can I use the @@syllables? Or, more specific, what is the use of
@@syllables here while I conclude that the purpose of this modification
to the class String is to remove '-'?

3- Why all that code if the purpose is removing '-', such that we ONLY
need self.split( '-' ) in the body of name_significance here?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Martin DeMello on
On Wed, Jul 14, 2010 at 3:26 PM, Abder-Rahman Ali
<abder.rahman.ali(a)gmail.com> wrote:
> I have the following script that I just have only slight changes in the
> hash from "Why's poignant guide to Ruby" book:
>
> http://pastie.org/private/ojmcfglofwloqemqmuhra

pasting it inline:

class String
@@syllables = [{'P' => 'Px','G' => 'Gx','B' => 'Bx','S' => 'Sx','W'
=> 'Wx','O' => 'Ox'}, {'R' => 'Rx','C' => 'Cx'}]
def name_significance
parts = self.split('-') # 'self' returns the current object. And,
'split' here means splitting based on (-)
syllables = @@syllables.dup
signif = parts.collect do |p|
syllables.shift[p]
end
signif.join( ' ' )
end
end

Your problem is the call to syllables.shift - it removes one element
from syllables and returns it. But it does this once per hyphen, and
syllable only has two elements, so when you say

> puts "Abder-Rahman-is-my-name".name_significance

the first time through the loop, you have:

p = Abder
syllables.shift = {"P"=>"Px", "G"=>"Gx", "B"=>"Bx", "S"=>"Sx",
"W"=>"Wx", "O"=>"Ox"}
syllables.shift[p] =

the second time through you have:

p = Rahman
syllables.shift = {"R"=>"Rx", "C"=>"Cx"}
syllables.shift[p] =

the third time:
p = is
syllables.shift = nil
s.rb:10:in `block in name_significance': undefined method `[]' for
nil:NilClass (NoMethodError)

> 2- How can I use the @@syllables? Or, more specific, what is the use of
> @@syllables here while I conclude that the purpose of this modification
> to the class String is to remove '-'?
>
> 3- Why all that code if the purpose is removing '-', such that we ONLY
> need self.split( '-' ) in the body of name_significance here?

I have no idea what this code is intended to do :) What it does do is
take in a string like "P-C" and return "Px Cx".

Here's the code with print statements:

class String
@@syllables = [{'P' => 'Px','G' => 'Gx','B' => 'Bx','S' => 'Sx','W'
=> 'Wx','O' => 'Ox'}, {'R' => 'Rx','C' => 'Cx'}]
def name_significance
parts = self.split('-') # 'self' returns the current object. And,
'split' here means splitting based on (-)
syllables = @@syllables.dup
signif = parts.collect do |p|
s = syllables.shift
puts "p = #{p}"
puts "syllables.shift = #{s.inspect}"
puts "syllables.shift[p] = #{s[p]}"
s[p]
end
signif.join( ' ' )
end
end

puts "P-C".name_significance

martin

From: Abder-Rahman Ali on
Thanks a lot Martin. That makes it more clear.
--
Posted via http://www.ruby-forum.com/.