Tag: 循环

如何迭代天

我有一个用于循环days.this的代码来制作树叶。 我希望列登录和注销将在startdate自动开始并在enddate结束。 exp I input : startdate: 2012-11-08 01:30:00 enddate: 2012-11-10 01:30:00 i want output like this: 2012-11-08 01:30:00 2012-11-09 01:30:00 2012-11-010 01:30:00 for i in 0..((@leafe.enddate – @leafe.startdate).to_i) @attendance = Attendance.new @attendance.signin = ‘2012-11-08 01:30:00’ #value must chang automatically @attendance.signout = ‘2012-11-08 10:30:00’#value must chang automatically @attendance.user_id = @leafe.user_id @attendance.save end 谢谢你

如何替换ruby字符串中的文本

我试图在Ruby中编写一个非常简单的方法,它接受一个字符串和一个单词数组,并检查字符串是否包含任何单词,如果是,它将用大写字母替换它们。 我做了一个尝试,但由于我的Ruby技能水平,它并不是很好。 def(my_words,my_sentence) #split the sentence up into an array of words my_sentence_words = my_sentence.split(/\W+/) #nested loop that checks the words array for each brand my_sentence_words.each do |i| my_words.each do |i| #if it finds a brand in the words and sets them to be uppercase if my_words[i] == my_sentence_words[i] my_sentence_words[i] == my_sentence_words[i].up.case end end end […]

我如何certificateRuby`for`循环实际上是使用`each`方法实现的?

在Eloquent Ruby(第21页,第一版,第六版)中 ,作者(Russ Olsen)主张使用each方法而不是for循环,这与我在其他地方读过的所有内容一致。 然而,作者还接着说,这样做的一个原因是for循环实际上调用了each方法,那么为什么不切断中间人并使用each呢? 所以我想知道这实际上是如何工作的。 为了调查我确实搜索了github上的Ruby repo,但发现很难确定我在哪里/如何看到这个。 重申一下这个问题: 如何显示Ruby for循环实际上是使用each方法实现的?

如何在Ruby on Rails中循环几个月

我需要在Ruby on Rails应用程序中循环几个月。 对于每个月,我需要找到该月的第一天和最后一天。 然后在单独的查询中使用这些日期来查找在这些日期期间发生的事件并对它们运行计算。 最初,我尝试过类似的东西: (11.months.ago.to_date.month..Date.today.month).each do |m| start_date = ’01-#{m}-#{Date.today.year}’.to_date.beginning_of_month end_date = ’01-#{m}-#{Date.today.year}’.to_date.end_of_month end 当然,如果11个月前涉及回到上一年,那么这一年的情况不会更新。 并且,我不认为这种类型的for / each循环有效。 我也尝试将数字映射到数组并使用相同的方法,但收到错误。 完成这样的事情的最佳方法是什么?

查找可变数量的Ruby数组的产品

我希望从可变数量的数组中找到单个项目的所有组合。 我如何在Ruby中执行此操作? 给定两个数组,我可以像这样使用Array.product: groups = [] groups[0] = [“hello”, “goodbye”] groups[1] = [“world”, “everyone”] combinations = groups[0].product(groups[1]) puts combinations.inspect # [[“hello”, “world”], [“hello”, “everyone”], [“goodbye”, “world”], [“goodbye”, “everyone”]] 当组包含可变数量的数组时,此代码如何工作?

在Ruby中迭代时删除?

我正在迭代一大堆字符串,它们遍历一小组字符串。 由于大小,这种方法需要一段时间才能完成,所以为了加快速度,我试图从较小的集合中删除字符串,不再需要随之使用。 以下是我目前的代码: Ms::Fasta.foreach(@database) do |entry| all.each do |set| if entry.header[1..40].include? set[1] + “|” startVal = entry.sequence.scan_i(set[0])[0] if startVal != nil @locations << [set[0], set[1], startVal, startVal + set[1].length] all.delete(set) end end end end 我面临的问题是,简单的方法, array.delete(string) ,有效地将一个break语句添加到内部循环中,这会混淆结果。 我知道如何解决这个问题的唯一方法是这样做: Ms::Fasta.foreach(@database) do |entry| i = 0 while i < all.length set = all[i] if entry.header[1..40].include? set[1] + […]

如何在Ruby中循环延迟?

例如,如果我想制作一个计时器,我如何在循环中进行延迟,以便以秒为单位计算,而不是仅仅在几毫秒内完成循环?

循环遍历数组

我想看一下数组中的每个第n个元素。 在C ++中,我会这样做: for(int x = 0; x<cx; x+=n){ value_i_care_about = array[x]; //do something with the value I care about. } 我想在Ruby中做同样的事情,但找不到“步”的方法。 一个while循环可以完成这项工作,但我发现使用它已知大小令人厌恶,并期望有更好的(更多Ruby)方式。

循环遍历整数中的位,ruby

我正在编写一个程序,其中一个问题是我需要对某些整数中的位模式进行一些分析。 因此,我希望能够做到这样的事情: #Does **NOT** work: num.each_bit do |i| #do something with i end 通过这样做,我能够创造出有效的东西: num.to_s(2).each_char do |c| #do something with c as a char end 然而,这没有我想要的性能 。 我发现你可以这样做: 0.upto(num/2) do |i| #do something with n[i] end 这比each_char方法的性能更差 这个循环将被执行数百万次或更多次,所以我希望它尽可能快。 作为参考,这是整个function @@aHashMap = Hash.new(-1) #The method finds the length of the longes continuous chain of ones, minus […]