Super.inspect returning escaped control chars? Why?

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 S.s:"
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

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

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

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.

Brian C. wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs