简体   繁体   English

在 hash 迭代器中使用 proc 而不是块

[英]using proc instead of block inside hash iterator

I wrote my own iterator for the class that I'm working on which extends hash .我为我正在处理的 class 编写了自己的迭代器,它扩展hash

The iterator that I wrote was achieved using blocks and now im trying to achieve the same exact thing using Proc .我编写的迭代器是使用块实现的,现在我试图使用Proc实现相同的功能。 but I kinda don't know how to use it in this context: :(但我有点不知道如何在这种情况下使用它::(

def each_word
   rank=0
   self.sort_by{|k,v| v}.reverse.to_h.keys.each{ |key,abs, rel | yield rank+=1, 
   key,self[key], self.frequency(key) if block_given? }#iterate with aid block
end

and i would use it like this:我会这样使用它:

puts "Rang - Anzahl - Häufigkeit(%) - Wort"
obj1.each_word do |rank,word,abs ,rel|
  puts "#{rank}         #{abs}       #{rel}%        #{word} "
end

using Proc(not correct code)使用 Proc(不正确的代码)

problem is that i want to have the same code using Proc问题是我想使用Proc拥有相同的代码

def each_letter
  rank=0
  x=self.sort_by{|k,v| v}.reverse.to_h# this wont work but wrote it as helpless try
  my_proc=Proc new { |key,abs, rel | yield rank+=1, key,x[key], x.frequency(key) }
  my_proc.call()
end

I already looked up those pages我已经查过那些页面

so what is the correct way to use the provided block using letters?!那么使用字母使用提供的块的正确方法是什么?!

Edit the Assignment text:编辑作业文本:

each_word iterate over the words found using the method and a block, starting with the most common word to the least common word. each_word遍历使用方法和块找到的单词,从最常见的单词开始到最不常见的单词。 The block gets the word, the absolute and relative frequency passed.(which I assume I did correctly)该块得到了这个词,绝对和相对频率通过了。(我假设我做对了)

The letter statics each_letter It behaves similarly to each_word , but within the method, you should use a Proc instead of a block .字母each_letter它的行为类似于each_word ,但在方法中,您应该使用Proc而不是block

What your doing here in your attempt is wrapping yield with a Proc, but in reality you can replace yield with the Proc:你在这里所做的尝试是用 Proc包装yield,但实际上你可以用 Proc替换yield:

def each_word(&proc)
   rank=0
   keys = self.sort_by{|k,v| v}.reverse.to_h.keys
   keys.each do |key, abs, rel|
     if proc
       proc.call(rank+=1, key,self[key], self.frequency(key))
     end
   end
end

What &proc does it take the block that is passed in (whether with do... end or {... } syntax) and converts it to a proc. &proc将传入的(无论是使用do... end还是{... }语法)并将其转换为 proc。

To check whether a block was given, you would simply use if proc (whereas if you were using yield, you'd use block_given? ).要检查是否给出了块,您只需使用if proc (而如果您使用 yield,您将使用block_given? )。 You could alternatively use proc&.call which is the safe navigation operator .您也可以使用proc&.call ,它是安全导航运算符

By the way, you should probably change rank +=1 to rank + 1 , there's no no point reassigning the variable's value here since it's not going to change the value in the hash anyway (numbers are immutable).顺便说一句,您可能应该将rank +=1更改为rank + 1 ,在这里重新分配变量的值是没有意义的,因为它不会改变 hash 中的值(数字是不可变的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM