super.inspect returning escaped control chars? Why?

G

Gene Angelo

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.
 
B

Brian Candler

Please can you turn this into a standalone program which demonstrates
the problem?
 
B

Brian Candler

super.inspect doesn't do what you expect. "super" calls the method of
the same name in the ancestor class (ie 'inspect'). You are then calling
inspect on the value returned.
 
G

Gene Angelo

Brian said:
super.inspect doesn't do what you expect. "super" calls the method of
the same name in the ancestor class (ie 'inspect'). You are then calling
inspect on the value returned.

Thank you, that was the problem. I am user to C#, C++, etc. where you
would call the base class' method explicitly. So, I was essentially
calling super.inspect.inspect.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top