Ruby 根据我的_时间定义我的_

Ruby 根据我的_时间定义我的_,ruby,iterator,codeblocks,control-flow,ruby-1.9.1,Ruby,Iterator,Codeblocks,Control Flow,Ruby 1.9.1,我正在读一本基础扎实的红宝石书,遇到了一个额外的信用挑战,对此没有答案 class Array def my_each c = 0 until c == size yield(self[c]) c += 1 end self end end 给出了使用myu时间创建myu-each的示例 class Array def my_each size.my_times do |i| yield self[i]

我正在读一本基础扎实的红宝石书,遇到了一个额外的信用挑战,对此没有答案

class Array
  def my_each
    c = 0
    until c == size
      yield(self[c])
      c += 1
    end
    self
  end
end
给出了使用
myu时间创建
myu-each
的示例

class Array
  def my_each
    size.my_times do |i|
      yield self[i]
    end
    self
  end
end
Ruby的许多迭代器都是在
的基础上构建的,而不是相反

鉴于上述
my_each
,我如何在
my_times
的实现中使用它

为了说明这一点,前面给出了
my_times
实现的示例:

class Integer
  def my_times
    c = 0
    until c == self
      yield(c)
      c += 1
    end
    self
  end
end

5.my_times { |e| puts "The block just got handed #{e}." }

因此,问题似乎最明确地意味着在
my\u times
的实现中使用
my\u each
要实现
my\u times
使用
my\u each
,只需在一个看起来像
[0,1,…(x-1)]
的数组上调用
my\u each
,其中
x
self
(整数):


另外,如果您在可枚举而不是数组上定义了
my\u each
(如“real”
each
),您可以从上面的第三行中删除
到a
,直接在范围上迭代,而不是首先将范围转换为数组。

要使用
my\u each
实现
my\u次
,您只需在一个看起来像
[0,1,…(x-1)]
的数组上调用
my\u each
,其中
x
self
(整数):

另外,如果您在可枚举而不是数组上定义了
my_each
(如“real”
each
),您可以从上面第三行中删除
到a
,并直接在该范围内迭代,而不是先将范围转换为数组。

编辑:我注意到使用了
而不是
生成正确的输出;有关范围差异的更多详细信息,请参见此。我在下面更新了我的答案

我的账户太新了,我无法评论的解决方案;我看到这是大约一年前发布的,但我目前正在通过一位有良好基础的红宝石作家阅读,并想对解决方案发表评论

我曾以与乔丹相似的方式处理过它,但发现与乔丹相比,它的输出是差的;基于良好基础的Rubyist实现的
my_times
,可产生:

puts 5.my_times { |i| puts "I'm on iteration # {i}!" }
I'm on iteration 0!
I'm on iteration 1!
I'm on iteration 2!
I'm on iteration 3!
I'm on iteration 4!
约旦的解决方案产出:

puts 5.my_times { |i| puts "I'm on iteration # {i}!" }
I'm on iteration 0!
I'm on iteration 1!
I'm on iteration 2!
I'm on iteration 3!
I'm on iteration 4!
I'm on iteration 5!

我使用了一个幻数来匹配基础良好的Rubyist输出[参见Jordan的解决方案,使用
而不是
这样就不需要幻数了]

class Integer
  def my_times
    (0..(self-1)).to_a.my_each do |n|
      yield n
    end
    self
  end
end
编辑:我刚刚注意到使用了
而不是
生成正确的输出;有关范围差异的更多详细信息,请参见此。我在下面更新了我的答案

我的账户太新了,我无法评论的解决方案;我看到这是大约一年前发布的,但我目前正在通过一位有良好基础的红宝石作家阅读,并想对解决方案发表评论

我曾以与乔丹相似的方式处理过它,但发现与乔丹相比,它的输出是差的;基于良好基础的Rubyist实现的
my_times
,可产生:

puts 5.my_times { |i| puts "I'm on iteration # {i}!" }
I'm on iteration 0!
I'm on iteration 1!
I'm on iteration 2!
I'm on iteration 3!
I'm on iteration 4!
约旦的解决方案产出:

puts 5.my_times { |i| puts "I'm on iteration # {i}!" }
I'm on iteration 0!
I'm on iteration 1!
I'm on iteration 2!
I'm on iteration 3!
I'm on iteration 4!
I'm on iteration 5!

我使用了一个幻数来匹配基础良好的Rubyist输出[参见Jordan的解决方案,使用
而不是
这样就不需要幻数了]

class Integer
  def my_times
    (0..(self-1)).to_a.my_each do |n|
      yield n
    end
    self
  end
end

为了实现my_times,我们需要一个数组将每条消息发送到my_。在书中的这一点上,我认为没有涵盖范围,所以我在实现时没有使用范围。以下是解决方案:

require_relative "my_each"
class Integer
  def my_times
    array = Array.new(self)
    c = 0
    array.my_each do
      array[c] = c
      yield(c)
      c += 1
    end
    self
  end
end

为了实现my_times,我们需要一个数组将每条消息发送到my_。在书中的这一点上,我认为没有涵盖范围,所以我在实现时没有使用范围。以下是解决方案:

require_relative "my_each"
class Integer
  def my_times
    array = Array.new(self)
    c = 0
    array.my_each do
      array[c] = c
      yield(c)
      c += 1
    end
    self
  end
end

好的老靶场。我明白你关于可枚举的意思了。尽管这个问题含蓄地禁止它。我会检查你的解决方案。如果有更有趣的解决方案,我会犹豫是否立即接受答案。很好的老范围。我明白你关于可枚举的意思了。尽管这个问题含蓄地禁止它。我会检查你的解决方案。如果有更有趣的解决方案,我不敢马上接受答案。