Ruby 在代码块中,两条垂直线和super.select是什么意思?
下面这句话到底是什么意思Ruby 在代码块中,两条垂直线和super.select是什么意思?,ruby,Ruby,下面这句话到底是什么意思 def method super.select { |a| a.meets_condition? || true } end 在这种情况下,我特别挣扎于| |。用搜索引擎很难找到这类问题的答案 super.select做什么 如果删除了| | true部分,该方法会做什么?此方法从父类调用具有相同名称的方法,然后为返回值调用select方法。 例如: 这将打印: 嗯 不好 方法Class2method调用方法ClassMethod ClassMethod打印“O
def method
super.select { |a| a.meets_condition? || true }
end
在这种情况下,我特别挣扎于| |。用搜索引擎很难找到这类问题的答案
super.select做什么
如果删除了| | true部分,该方法会做什么?此方法从父类调用具有相同名称的方法,然后为返回值调用select方法。 例如: 这将打印: 嗯 不好 方法Class2method调用方法ClassMethod ClassMethod打印“OK”并返回“NOT OK”,然后downcases将返回的字符串“NOT OK”转换为“NOT OK”并打印它 在您的示例中,用于选择“布尔或”的块中的两条垂直线以及整个表达式a.满足_条件?||true可用于两个原因: 它可以被评估为调用a的结果是否满足条件?如果此结果不为真或为零,或为真,则为。 如果a.满足条件,它可以抛出异常?抛出异常。此异常可以在调用堆栈的上层捕获和处理。
这是一段奇怪的代码,但它就是这样做的: 您正在定义一个名为method的方法 特殊方法super意味着对父超类调用相同的命名方法 您正在获取super的结果,它必须是一个可枚举对象,例如数组或哈希,并对该对象调用select select遍历可枚举对象,让其假设为数组,并使用每个元素调用块。通常用于从阵列中筛选或选择某些对象。每次调用块时,它都返回一个truthy或falsy值。如果为truthy,则该元素将保留在结果数组中。如果falsy被扔掉了 好的,所以这个数组的每个元素都将在其上执行: a、 符合条件真的 这很奇怪,因为这将要做的是调用满足条件?在数组的元素上,如果它返回一个truthy值,那么该元素a将保留在数组中 但如果a.符合条件怎么办?法尔西在吗 然后,我们继续下一部分的或双管道,并做到这一点 这是真的 基本上,这个表达式将返回传入的数组的副本 让我们把这个例子变成一个实际的工作例子:
class RandomDigit
# gives you a object containing a random digit between 0 and 9
def initialize
@n = rand(10)
end
def meets_condition? # returns true if @n is even
@n % 2 == 0
end
end
class TheParentClass
def method
# returns array of 4 random digits (between 0 and 9)
[RandomDigit.new, RandomDigit.new, RandomDigit.new, RandomDigit.new]
end
end
class TheChildClass < TheParentClass
def method
# super means we are calling TheParentClass.method
# select will try each element of the the array
# and builds a new array, with elements that returned true
# but the trouble is || true means its always going to return true
super.select { |a| a.meets_condition? || true }
end
end
puts TheChildClass.new.method # -> returns 4 random digits
您可以单击此链接来运行代码并查看其工作情况
只是想澄清一下,这里唯一没有真正意义的是真实的部分
否则,您将定义一个新类,并稍微更改方法的行为,使其工作方式类似于原始方法,但会过滤掉元素
通常你可能会看到类似于问题1的东西a、 问题2
哪一个是问题1?如果它返回真值,那么我们就完蛋了
如果没有返回true,请尝试问题2
这是因为| |是一个控制操作流。。。如果第一部分已经为true,则不会执行第二部分
&&相反,因为除非第一部分为真,否则不会执行第二部分。超级on上有相关信息。它调用超类中的当前方法是一个布尔或。也许只是我,但我认为这个例子没有多大意义。返回条件为true或true的所有条目?换句话说,总是返回从super返回的所有条目?那么这个方法的意义是什么呢?@orde不太清楚| |,a | | b是一个表达式,如果a是真的,b不是真的,那么表达式的值不一定是真的或假的。@mu太短了:thx用于澄清。这是在文档中定义的还是仅仅是隐式的?@orde应该被记录下来,但我不知道有什么关于Ruby的最新文档。这是一种相当常见的行为,| |在JavaScript、Perl、。。。
class RandomDigit
# gives you a object containing a random digit between 0 and 9
def initialize
@n = rand(10)
end
def meets_condition? # returns true if @n is even
@n % 2 == 0
end
end
class TheParentClass
def method
# returns array of 4 random digits (between 0 and 9)
[RandomDigit.new, RandomDigit.new, RandomDigit.new, RandomDigit.new]
end
end
class TheChildClass < TheParentClass
def method
# super means we are calling TheParentClass.method
# select will try each element of the the array
# and builds a new array, with elements that returned true
# but the trouble is || true means its always going to return true
super.select { |a| a.meets_condition? || true }
end
end
puts TheChildClass.new.method # -> returns 4 random digits