gpt4 book ai didi

ruby - 在 ruby​​ 中,这种类型的定义是什么 : self. class.method

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

我是一个试图理解一些代码的 NOOB。

这个 self.class.current_section 是做什么的?

class MyClass
class << self
def current_section(*section)
if section.empty?
@current_section
else
@current_section = section[0]
end
end
end


def current_section()
self.class.current_section
end

def current_section=(section)
self.class.current_section(section)
end
end

最佳答案

self 返回当前对象。

self.class 返回当前对象的类。

self.class.current_section 调用当前对象类的方法(该方法称为current_section)。

 def current_section()
p self
p self.class
end

current_section()

关于ruby - 在 ruby​​ 中,这种类型的定义是什么 : self. class.method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9110636/

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