3

Using this code example

#!/usr/bin/ruby
paragraph = "champion xylophone excellent"
paragraph = paragraph.gsub("ch","tj")
words = paragraph.split(/ /)
words.each do |word|
   if word[0,1] == "x"
     word[0.1] = "z" 
   end
end
paragraph = words.join(" ")
paragraph.gsub("x","ks")
print paragraph

The output will be 'tjampion zylophone excellent' rather than 'tjampion zylophone ekscellent'

The same applies if the gsub is applied within the each to the individual words. I don't understand why it acts at the beginning but not at the end.

Edit

Second case is a distinct issue from the first:

#!/usr/bin/ruby
paragraph = "champion xylophone excellent"
paragraph = paragraph.gsub("ch","tj")
words = paragraph.split(/ /)
words.each do |word|
   if word[0,1] == "x"
     word[0.1] = "z" 
   end
   word = word.gsub("x","ks")
end
paragraph = words.join(" ")
print paragraph
1
  • See below I updated. One line of code can do all operations.
    – SwiftMango
    Apr 13, 2012 at 14:42

4 Answers 4

5

When you first use gsub you are assigning it to paragrah

paragraph = paragraph.gsub("ch","tj")

The second time you are missing the assignment

change paragraph.gsub("x","ks") to

paragraph = paragraph.gsub("x","ks")
2
  • 5
    or change to 'paragraph.gsub!("x","ks")'
    – megas
    Apr 13, 2012 at 13:39
  • What a plonker, you are right! Any thoughts about the second case as that seems to be different altogether?
    – jaz9090
    Apr 13, 2012 at 13:40
2

Your code is working by accident. The working parts use methods on strings that modify the string; the not working parts use methods that produce a new string (which is discarded). This is confusing; a better way would be to use map if the desired result is an array. It would contain the results of the block, either modified strings or new ones.

paragraph = "champion xylophone excellent"
words = paragraph.split(' ').map do |word|
  word.gsub('ch','tj') #new string
  word[0.1] = "z" if word.start_with?('x') #modified string
  word.gsub!('x','ks') #modified string
end
puts words.join(' ')
2
paragraph.gsub!("x","ks")
puts paragraph

Or use RegExp (one line operation can do all):

paragraph = "champion xylophone excellent"
paragraph = paragraph.gsub("ch","tj").gsub(/([\A\s])x(\w*)/){$1 + 'z'+ $2}.gsub("x","ks")

puts paragraph
2
  • does using the assignment operator create a new 'word' element in my code ?
    – jaz9090
    Apr 13, 2012 at 13:48
  • No. obj.each only pass the "VALUE" of the reference (rather than pass by reference; a bit confusing and worth a bit digging deep). For example, [1,2].each { |x| x += 1} will still give [1,2]
    – SwiftMango
    Apr 13, 2012 at 13:54
0

If you want your gsub to be destructive, i.e. to change the value of the variable on which it was called, use gsub!. so:

paragraph.gsub!('ch', 'tj')

rather than:

paragraph = paragraph.gsub('ch', 'tj')

It's more concise, and Ruby developers recognize bang (!) methods as destructive. And in your case, you need the gsub! method in place of both calls to gsub

2
  • there is a typo in the second example, not sure if it causes the problem though: word[0.1] = "z" shouldn't the . be a ,? Apr 13, 2012 at 13:44
  • Doesn't seem to be the issue, fixing it results in the same output
    – jaz9090
    Apr 13, 2012 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.