- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
undefined method 简而言之,class 为给定对象打开一个实例类,允许为给定实例定义额外的实例方法,这些方-6ren">
为什么我要使用 class << Class
向类中添加任何内容?语法?
class Fun
def much_fun
# some code here
end
end
class << Fun # the difference is here!
def much_more_fun
# some code here
end
end
而不是使用 monkey patching/duck punching 方法:
class Fun
def much_fun
# some code here
end
end
class Fun # the difference is here!
def much_more_fun
# some code here
end
end
阅读时Why's Poignant Guide to Ruby我遇到了:
为什么要定义一个类 LotteryDraw
:
class LotteryDraw
# some code here
def LotteryDraw.buy( customer, *tickets )
# some code here as well
end
end
并在一段时间后向 LotteryDraw
添加了一个方法类:
class << LotteryDraw
def play
# some code here
end
end
说:
When you see
class << obj
, believe in your heart, I’m adding directly to the definition ofobj
.
这个语法的目的是什么?为什么为什么决定这样做而不是使用猴子修补方法?
这些是一些相关的问题和网站:
class << Class
有了一点了解。语法。最佳答案
需要多解释一下。在 ruby 中,几乎每个对象都可以创建一个奇怪的东西,称为实例类。这个东西就像一个普通的类,主要区别在于它只有一个实例,并且该实例是在该类之前创建的。
简而言之,有了 A 类,您可以:
a = A.new
b = A.new
两者,a
和 b
现在是类 A 的实例,并且可以访问此类中定义的所有实例方法。现在让我们说,我们想添加一个只能由 a
访问的额外方法, 但不是 b
(这在某些情况下可能很有用,但尽可能避免使用)。所有的方法都在类中定义,所以我们似乎需要将它添加到类 A,但这样它就可以通过 b
访问。以及。在这种情况下,我们需要为 a
创建一个实例类目的。为此,我们使用 class << <object>
语法:
class << a
def foo
end
end
a.foo #=> nil
b.foo #=> undefined method
简而言之,class << <object>
为给定对象打开一个实例类,允许为给定实例定义额外的实例方法,这些方法在任何其他对象上都不可用。
现在,话虽这么说:在 Ruby 中,类只是类 Class 的实例。这:
class A
end
几乎等同于(这里的区别不重要)
A = Class.new
所以如果类是对象,它们可以创建它们的实例类。
class << A
def foo
end
end
A.foo #=> nil
Class.new.foo #=> undefined method
通常用于给给定的类添加所谓的类方法,但关键是实际上您是在类的实例类上创建实例方法。
关于ruby - "class << Class"(尖括号)语法的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33453063/
我遇到过如下代码: my $string = "fo2345obar"; $string =~ sg; 我不明白尖括号在这种情况下是什么意思;我之前看到的所有替换都是以下形式: $string =~
我正在尝试使用 VBA 计算特定单元格值在 Excel 电子表格中出现的次数。单元格值是一个 XML 标记,尖括号被函数解释为小于/大于。这些字符如何转义? 微软说: "您可以使用通配符、问号 (?)
这个问题在这里已经有了答案: What is the difference between parentheses, brackets and asterisks in Angular2? (3 个回
我正在阅读githab Flux Dispatcher 的源代码并找出尖括号语法: class Dispatcher { .... } 这是该语言的新功能还是像 typescript 一样的子集?这里
在类声明中,<> 尖括号和 swift 中声明的参数有什么用?就像: public class ClassName { } 最佳答案 它使类变得通用。 Swift 标准库没有很多泛型类的示例,但它有一
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: printing “” using html 如何在不调用 html 的情况下将 < 符号直接放入 html
为什么我要使用 class 语法: class nil b.foo #=> undefined method 简而言之,class 为给定对象打开一个实例类,允许为给定实例定义额外的实例方法,这些方
在类声明中,<> 尖括号和在 swift 中声明的参数有什么用?像这样: public class ClassName { } 最佳答案 它使类通用。 Swift 标准库没有很多泛型类的例子,但是它有
我正在学习Java,最近被尖括号(<>)难住了。它们到底是什么意思? public class Pool{ public interface PoolFactory{ publ
我正在学习Java,最近被尖括号(<>)难住了。它们到底是什么意思? public class Pool{ public interface PoolFactory{ publ
在“我的查询”中,其他开发人员使用 <> (angle brackets)这是什么意思? sb.append(" AND nvl(VoidFlag, 'N') <> 'Y' "); 最佳答案 <> 运
我正在使用 (Win 10) 捷克语键盘布局 (CES/CS),通常我能够通过右 alt (AltGR + 的组合来编写尖括号“>”。 ) 但现在 Visual Studio 2015 Communi
这个问题在这里已经有了答案: 13年前关闭。 Duplicate: ASP.NET "special" tags 我使用 PHP,它使用 或 作为服务器标签,但是ASP.NET的中的含义和用法是什么?
我是一名优秀的程序员,十分优秀!