about_classes.rb ruby​​ 中的检查和 self

发布于 2024-11-16 16:19:50 字数 1164 浏览 9 评论 0原文

我目前正在开发 about_classes.rb。我对检查的概念及其与自我的关系感到困惑。调用对象是否会自动返回该对象的 inspect 方法?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

皇甫轩 2024-11-23 16:19:50

selfinspect 没有特殊的关系——恰好您正在使用的“Ruby koans”教程同时教您这两者。

self 是一个关键字,在方法内部,其计算结果为您调用该方法的对象。

inspect 是所有对象都实现的方法,它返回对象的字符串表示形式。 to_s 还返回对象的字符串表示形式。

不同之处在于,如果可能的话,inspect 返回的字符串通常会模仿可以创建等效对象的 Ruby 语法。 inspect 一般用于调试。 to_s 返回的字符串通常更适合向最终用户显示。

“#{表达式}”语法隐式调用 to_s 对象,该对象是通过计算 表达式 得到的结果。

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.

昔梦 2024-11-23 16:19:50

在 Ruby 中,您不能“调用对象”。 Inspect 类似于 Python 中的 repr() - 它显示对象的官方、可调试字符串表示形式。应该注意的是,puts foo 调用foo.to_s,而p foo 调用foo.inspect

为了完整起见,我将在这里补充一点,您不能在 Ruby 中调用对象 - 没有与 Python 的神奇 call 方法等效的方法。

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.

寄意 2024-11-23 16:19:50

为了回答你的问题,当你在 irb 中“调用”一个对象时,类似于在该对象的检查方法的返回值上调用 put 。这可能是也可能不是实际发生的情况,但这是您能够模仿它的唯一方法。尝试以下示例。

"Hello"
=> "Hello"

puts "Hello"
=> Hello

class MyClass
  def inspect
    "Hello"
  end
end

a = MyClass.new
=> Hello

a.inspect
=> "Hello"

puts a
=> Hello

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文