# I have some questions about this modified String class

**URL:** <https://www.ruby-forum.com/t/i-have-some-questions-about-this-modified-string-class/191551>\
**Category:** Ruby\
**Created:** [July 14, 2010, 11:56am UTC](https://www.ruby-forum.com/t/i-have-some-questions-about-this-modified-string-class/191551 "2010-07-14T11:56:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 11:56am UTC](https://www.ruby-forum.com/t/i-have-some-questions-about-this-modified-string-class/191551/1 "2010-07-14T11:56:09Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/i-have-some-questions-about-this-modified-string-class/191551/2 "2010-07-14T12:53:44Z")

</div>

On Wed, Jul 14, 2010 at 3:26 PM, Abder-Rahman A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 1:59pm UTC](https://www.ruby-forum.com/t/i-have-some-questions-about-this-modified-string-class/191551/3 "2010-07-14T13:59:23Z")

</div>

Thanks a lot Martin. That makes it more clear.
