gpt4 book ai didi

ruby - 我如何获得在类里面工作的机会?

转载 作者:数据小太阳 更新时间:2023-10-29 07:48:50 26 4
gpt4 key购买 nike

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]

关于ruby - 我如何获得在类里面工作的机会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com