同时迭代两个文件的最简洁方法是什么,以便如果 file_1 == 'x' 中的第 57 行则以这种方式处理 file_2 中的第 57 行?
我下面的代码不起作用,但我知道有一个简单的解决方案......
@blue = []
@red = []
file_1 = File.open('index.txt', 'r')
file_2 = File.open('index2.txt', 'r')
for item, line in file_1, file_2
@blue << line if item == "blue"
@red << line if item == "red"
end
file_1.close
file_2.close
Run Code Online (Sandbox Code Playgroud) 我想使用从另一个源访问的属性名称(在真实数据中,有很多)来更新 AR 对象。
class DailyScore < ActiveRecord::Base
attr_accessor :mail, :date, :sun, :express, :average
end
array = ['mail','date','sun']
thing = DailyScore.new
Run Code Online (Sandbox Code Playgroud)
如何将数组中的每个项目转换为符号,并相应地更新记录?
理想情况下,我想要这样的东西:
array.each do |field|
thing.field = 1.0 #Float
end
Run Code Online (Sandbox Code Playgroud)
更新(使用更完整的代码示例):
def sort_and_deliver_scores(dates)
# dates example: ["2016-01-01","2016-01-12","2016-01-05"]
dates.each do |dateString|
params = {}
params[:date] = dateString
so_far = 0.0
["mail","express","sun"].each do |paper|
# get all records that correspond to this newspaper
@dailyscores = MyModel
.pluck(:paper,:score, :date)
.select{|b| b[0]==paper}
# add up the scores in all records for each paper that …Run Code Online (Sandbox Code Playgroud) 我正在构建一个下载句子并解析它们以进行文字游戏的应用程序.我事先并不知道文本将包含哪些标点符号.
我希望能够将句子分开,检查它们的词性标记,如果找到了正确的标记,请将其替换为" ",并按顺序重新加入它们.
text = "some string, with punctuation- for example: things I don't know about, that may or may not have whitespaces and random characters % !!"
Run Code Online (Sandbox Code Playgroud)
我怎样才能将它拆分成一个数组,以便我可以将解析器传递给每个单词,并按顺序重新加入它们,同时要记住我string.split(//)似乎需要知道我在寻找什么标点符号?
刚刚安装了宝石https://github.com/javan/whenever来运行我的rake任务,这是nokogiri/feedzilla依赖的抓取任务.
例如我的任务叫做grab_bbc,grab_guardian等
我的问题 - 当我更新我的网站时,我会不断向scheduler.rake添加更多任务.
我应该在config/schedule.rb中编写什么来使所有rake任务运行,无论它们被称为什么?
会这样的吗?
every 12.hours do
rake:task.each do |task|
runner task
end
end
Run Code Online (Sandbox Code Playgroud)
使用RoR 4是Cron的新手.
在循环文本行时,最好的方法(大多数"Ruby")做一个if else语句(或类似的)以检查字符串是否是单个单词是什么?
def check_if_single_word(string)
# code here
end
s1 = "two words"
s2 = "hello"
check_if_single_word(s1) -> false
check_if_single_word(s2) -> true
Run Code Online (Sandbox Code Playgroud)