- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试调整此帖子 (Method chaining and lazy evaluation in Ruby) 中引用的方法链接示例,以与实现 Enumerable
的对象一起使用类 ( Implement a custom Enumerable collection class in Ruby )
咖啡类:
class Coffee
attr_accessor :name
attr_accessor :strength
def initialize(name, strength)
@name = name
@strength = strength
end
def <=>(other_coffee)
self.strength <=> other_coffee.strength
end
def to_s
"<name: #{name}, strength: #{strength}>"
end
end
条件类:
class Criteria
def initialize(klass)
@klass = klass
end
def criteria
@criteria ||= {:conditions => {}}
end
# only show coffee w/ this strength
def strength(strength)
criteria[:strength] = strength
self
end
# if there are multiple coffees, choose the first n=limit
def limit(limit)
criteria[:limit] = limit
self
end
# allow collection enumeration
def each(&block)
@klass.collection.select { |c| c[:strength] == criteria[:strength] }.each(&block)
end
end
CoffeeShop 类:
class CoffeeShop
include Enumerable
def self.collection
@collection=[]
@collection << Coffee.new("Laos", 10)
@collection << Coffee.new("Angkor", 7)
@collection << Coffee.new("Nescafe", 1)
end
def self.limit(*args)
Criteria.new(self).limit(*args)
end
def self.strength(*args)
Criteria.new(self).strength(*args)
end
end
当我运行这段代码时:
CoffeeShop.strength(10).each { |c| puts c.inspect }
我得到一个错误:
criteria.rb:32:in block in each': undefined method '[]' for #<Coffee:0x007fd25c8ec520 @name="Laos", @strength=10>
我确定我没有定义 Criteria.each
方法正确,但我不确定如何更正它。我该如何纠正这个问题?
此外,each
方法不支持 limit
如目前所写。是否有更好的方法来过滤数组,以便更容易同时支持 strength
和 limit
?
感谢其他编码建议。
最佳答案
您的Coffee
类定义了name
和strength
的方法访问器。对于单个 coffee
对象,您可以通过
coffee.name
# => "Laos"
coffee.strength
# => 10
在您的 Criteria#each
方法中,您尝试使用下标运算符访问属性,即 c[:strength]
(使用 c
在本例中是 Coffee
的一个实例)。现在,在您的 Coffee 类中,您还没有实现下标访问器,这导致了您在那里看到的 NoMethodError
。
因此,您可以按如下方式调整您的 Criteria#each
方法:
def each(&block)
@klass.collection.select { |c| c.strength == criteria[:strength] }.each(&block)
end
或者您可以在您的 Coffee
类上实现下标运算符:
class Coffee
attr_accessor :name
attr_accessor :strength
# ...
def [](key)
public_send(key)
end
def []=(key, value)
public_send(:"#{key}=", value)
end
end
不,作为附录,您可能希望在任何情况下扩展您的 each
方法。一个常见的(通常是隐式预期的)模式是,如果没有给出 block ,像 each
这样的方法返回一个 Enumerator。这允许像 CoffeeShop.strength(10).each.group_by(&:strength)
这样的模式。
您可以在您的方法中实现一个简单的在线程序:
def each(&block)
return enum_for(__method__) unless block_given?
@klass.collection.select { |c| c.strength == criteria[:strength] }.each(&block)
end
关于与 Enumerable 类链接的 Ruby 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48471052/
挖掘这个,这是一个很酷的 Enumerator(惰性序列)从 1 到(Ruby 可以表示的最大 Float): 1.9.3-p327 :014 > e = (1..Float::INFINITY).e
这个问题在这里已经有了答案: Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequenc
我不确定这是否特定于 ML.NET,但它确实发生在 ML.NET 的上下文中。 我正在使用 ML.NET 对一些图像进行分类。我意识到,无论我是否对结果 IEnumerable 调用 .ToArray
在Collections Framework中,我们有Iterator和ListIterator接口(interface),它们用于迭代数据结构。它们提供了完整的迭代功能。但我想知道为什么 JDK 在
我正在使用 playframework 的异步 I/O 库,它使用 Iteratees 和 Enumerators。我现在有一个 Iterator[T] 作为数据接收器(为简单起见,说它是一个 Ite
更新: private final java.util.Properties tilesPropertyMap = new Properties(); private class Delegati
所以,我有一个像这样的字符串 \begin{enumerate} \item My first item \item My second item \end{enumerate} 并且需要使用正则表达
我有一个数组a = [1, 2, 3, 4, 5] 如果我在数组中有一个元素,我可以通过a[0].next 有没有一种方法可以用来找到前一个元素a[1].previous 或 a[1].before
是否Enumerable#group_by保留每个值内的原始顺序?当我得到这个时: [1, 2, 3, 4, 5].group_by{|i| i % 2} # => {1=>[1, 3, 5], 0=
我已经基于现有的NativeHashMap.Enumerator和UnSafeHashMap.Enumerator结构编写了枚举数,它们以猝发方式工作,但在尝试为嵌套的非托管结构组合枚举数时遇到了麻烦
在 C# 中使用迭代器创建 Enumerable 或 Enumerator 有什么区别?我知道迭代器用于创建实现 Enumerable 或 Enumerator 的私有(private)类... 哪种
自 C++11 过渡以来,GCC 输出警告“条件表达式中的枚举和非枚举类型”。我想了解此警告背后的原因。比较枚举常量有什么危险? 很明显我们可以通过以下方式摆脱这个警告 -Wno-enum-compa
我在 LINQ 查询的性能方面遇到问题,因此我创建了一个简化的小示例来演示下面的问题。该代码采用一个随机的小整数列表,并返回分成几个较小列表的列表,每个列表总计 10 个或更少。 问题是(正如我所写的
我一直对 Enumerable#all? 和 Enumerable#each 的用例感到困惑。例如 ['.txt', '-hello.txt'].all? do |suffix| pu
Enumerable documentation没有明确说明 each 是 each_entry 的别名,但是 each_entry 的描述与我对 each 1 [1, 2] nil Foo.new.
在这段代码中,我创建了一个字符串数组“1”到“10000”: array_of_strings = (1..10000).collect {|i| String(i)} Ruby Core API 是
我正在为我的代码寻求一些帮助,如下所示: for file in file_name : if os.path.isfile(file): for line_number, l
刚刚下载了 Xcode 7 Beta,此错误出现在 enumerate 关键字上。 for (index, string) in enumerate(mySwiftStringArray) { } 谁
刚刚下载了 Xcode 7 Beta,这个错误出现在 enumerate 关键字上。 for (index, string) in enumerate(mySwiftStringArray) { }
为了清楚起见,假设我们有学生和类(class),这是多对多的关系。 我有一个字典,其中键是学生 ID,而 Enumerable 是一个类的集合(假设我们只有 id ),我想将其恢复为 classId、
我是一名优秀的程序员,十分优秀!