.reverse! Taking Over?


#1

I have defined "mom" (user's input), as well as "mom2" which is coded to be the user's input for "mom," spelled backwards. However, the program is printing the name backwards every-time, even when the code calls for "mom," spelled normally....

print "What's your mother's first name:"
mom = (gets.chomp)
mom.capitalize!
mom2 = mom.reverse!.capitalize!

My intention is to use #{mom} when spelling the name normally, and use #{mom2} to spell the name backwards (when needed)... But no matter which code I use, the name is spelled backwards!

What am I doing wrong???

Thanks in advanced!

Your humble newb

Aaron


print "What's your mother's first name:"
mom = (gets.chomp)
mom.capitalize!
mom2 = mom.reverse!.capitalize!

print "What's your favorite number:"
fav_number= Integer(gets.chomp)

print "How old are you:"
your_age = Integer(gets.chomp)

print "How old is your mom:"
mom_age = Integer(gets.chomp)

if (fav_number < mom_age) && (fav_number > your_age)
  puts "#{mom} is a sweety-pie!"

elsif (fav_number > mom_age) || (fav_number == your_age)
  puts "When I get angry, I call your mom #{mom2}!"
  
else
  puts "Glory! The number of the day is: #{fav_number}!"
 
end


#2

Something about ! so we're clear.

This operator prevents Ruby from creating a transient object. The operation takes place on the present object, in place. If this operation is assigned to a variable, that variable will get one of two values.

  1. The object with the effect having taken place; or,
  2. nil if there is no effect on the current object.

#3

I went back and read the documents at the end of the first module -http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#2.-variables (I skipped them like a dummy), fixed the problem, so I am okay with that now. However, I would like to understand what you are telling me. I'm not sure what you mean by a "transient object." I have noticed that without the '!' the effect will only occur in that instance, whereas adding the '!' will apply that effect every time the object is used? Am I understanding correctly? I'm still reading up, so I don't yet understand most of the technical terms. I thought if I created a new object (mom2) .reverse! would only apply to mom2, and not the (mom) object.

I honestly don't yet understand half your advice, but I do appreciate the feedback, and I will continue reading/learning until everything you have said to me makes sense! Lol

Anyway here is the updated/working code:

print "What's your mother's first name:"
mom = (gets.chomp)
mom.capitalize!

print "What's your favorite number:"
fav_number= Integer(gets.chomp)

print "How old are you:"
your_age = Integer(gets.chomp)

print "How old is your mom:"
mom_age = Integer(gets.chomp)

if (fav_number < mom_age) && (fav_number > your_age)
puts mom + " is a sweety-pie!"

elsif (fav_number > mom_age) || (fav_number == your_age)
puts "When I get angry, I call your mom "+ mom.reverse.capitalize + "!"

else
puts "Glory! The number of the day is: #{fav_number}!"

end


#4

My point exactly. The current object is unchanged when we do not use !.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.