Hello,
I am new to Ruby please help...
Two questions...
1) I do not know why Contact#inspect returns an escaped string (e.g. \n
changed to \\n, etc.). Can someone help?
I have a Classes Contact and Person...
class Contact
#...
def inspect
result = ""
# Print our email addresses...
# result << "\n"
result << "Email Addresses:"
email_addresses.each do |e|
result << "\n\t#{e.type.to_s.capitalize}: " << e.to_s
end unless email_addresses.nil?
# Print our phone numbers...
result << "\n"
result << "Phone Numbers:"
phone_numbers.each do |e|
result << "\n\t#{e.type.to_s.capitalize}: " << e.to_s
end unless phone_numbers.nil?
# Print our web sites...
result << "\n"
result << "Web Sites:"
web_sites.each do |e|
result << "\n\t#{e.type.to_s.capitalize}: " << e.to_s
end unless web_sites.nil?
# This prints an unescaped string, if uncommented...
#puts result
# But result returns an escaped string! Why?
result
end
#...
end
class Person < Contact
#...
def inspect
result =
"Name: #{@last}, #{@first}" \
<< "\nPrefix: #{@prefix}" \
<< "\nMiddle: #{@middle_initial}" \
<< "\nSuffix: #{@suffix}"
result << "\n"
# Everything above this returns an unescaped string...
# super.inspect returns an escaped string! Why?
result << super.inspect
end
#...
end
2) Is there any built-in method/class method to unescape an escaped
string (e.g. convert "\\n", "\\t", etc. to "\n", "\t", etc.?
Thank you much.
···
--
Posted via http://www.ruby-forum.com/.