开发者

about_classes.rb inspect and self in ruby

开发者 https://www.devze.com 2023-03-14 13:08 出处:网络
I\'m currently working on about_classes.rb. I开发者_开发百科\'m confused on the concept of inspect and how it relates to self. Does calling an object automatically return the inspect method for that o

I'm currently working on about_classes.rb. I开发者_开发百科'm confused on the concept of inspect and how it relates to self. Does calling an object automatically return the inspect method for that object?

class Dog7
    attr_reader :name

    def initialize(initial_name)
      @name = initial_name
    end

    def get_self
      self
    end

    def to_s
      __
    end

    def inspect
      "<Dog named '#{name}'>"
    end
  end

  def test_inside_a_method_self_refers_to_the_containing_object
    fido = Dog7.new("Fido")

    fidos_self = fido.get_self
    assert_equal __, fidos_self
  end

  def test_to_s_provides_a_string_version_of_the_object
    fido = Dog7.new("Fido")
    assert_equal __, fido.to_s
  end

  def test_to_s_is_used_in_string_interpolation
    fido = Dog7.new("Fido")
    assert_equal __, "My dog is #{fido}"
  end

  def test_inspect_provides_a_more_complete_string_version
    fido = Dog7.new("Fido")
    assert_equal __, fido.inspect
  end

  def test_all_objects_support_to_s_and_inspect
    array = [1,2,3]

    assert_equal __, array.to_s
    assert_equal __, array.inspect

    assert_equal __, "STRING".to_s
    assert_equal __, "STRING".inspect
  end


self and inspect have no special relationship -- it just so happens that the "Ruby koans" tutorial which you are using teaches you about both at the same time.

self is a keyword which, inside a method, evaluates to the object which you called the method on.

inspect is a method implemented by all objects, which returns a string representation of the object. to_s also returns a string representation of an object.

The difference is that the string returned by inspect will generally, if possible, mimic the Ruby syntax which can create an equivalent object. inspect is generally used for debugging. The string returned by to_s is usually more appropriate for displaying to an end user.

The "#{expression}" syntax implicitly calls to_s on the object which results from evaluating expression.


In Ruby, you can't "call an object". Inspect is similar to repr() in Python - it shows the official, debuggable string representation of an object. It should be noted that whilst puts foo calls foo.to_s, p foo calls foo.inspect.

For completeness, I'll add here that you can't call an object in Ruby - there isn't an equivalent of Python's magic call method.


To answer your question, when you "call" an object in irb, it is similiar to puts being called on the return value of the inspect method for that object. This may or may not be what is actually happening, but its the only way you will be able to imitate it. Try the following examples.

"Hello"
=> "Hello"

puts "Hello"
=> Hello

class MyClass
  def inspect
    "Hello"
  end
end

a = MyClass.new
=> Hello

a.inspect
=> "Hello"

puts a
=> Hello
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号