当前位置 : 主页 > 编程语言 > ruby >

ruby – 我如何在课堂上学习?

来源:互联网 收集:自由互联 发布时间:2021-06-23
class X def initialize @name = "Bob" end blah blahendputs X.new # I want this to print X:Bobputs [X.new, X.new] # I want this to print [X:Bob, X:Bob] 覆盖类的to_s方法: class X def initialize @name = "Bob" end def to_s "X:#{@name}" e
class X
  def initialize
    @name = "Bob"
  end
  blah blah
end

puts X.new  # I want this to print X:Bob
puts [X.new, X.new] # I want this to print [X:Bob, X:Bob]
覆盖类的to_s方法:

class X
  def initialize
    @name = "Bob"
  end

  def to_s
    "X:#{@name}"
  end
end

puts X.new  # prints X:Bob
puts [X.new, X.new].to_s # prints [X:Bob, X:Bob]
网友评论