我的seeds.rb文件变得非常大.重构文件中数据的最佳方法是什么?
我可以将数据放入文件require中的各种文件中seeds.rb吗?
a = {
1 => ["walmart", "walmart.com", 300.0],
2 => ["amazon", "amazon.com", 350.0],
...
}
Run Code Online (Sandbox Code Playgroud)
如何在数组中找到浮点值最小的元素?
我想周期@a从0通过2:0, 1, 2, 0, 1, 2.
def set_a
if @a == 2
@a = 0
else
@a = @a + 1
end
end
Run Code Online (Sandbox Code Playgroud)
也许有更好的方法?
我收到此错误:未定义方法`stringify_keys':environ_gross_score:当我尝试创建新评级时的符号.
class Rating < ActiveRecord::Base
belongs_to :city
after_save :calculate_rating
def calculate_rating
@env = self.environ
self.city.environ_vote_count += 1
@c = self.city.environ_gross_score
@gross = @c += @env
self.city.update_attributes(:environ_gross_score, @gross )
@hold = self.city.environ_gross_score / self.city.environ_vote_count
self.city.update_attributes(:environ_rating, @hold)
end
end
Run Code Online (Sandbox Code Playgroud) 我想要一个正则表达式,它将提取快乐和良好的词,既不贪婪又不区分大小写.
@a = [" I am very HAppy!!", "sad today..", "happy. to hear about this..", "the day is good", "sad one", "sad story"]
Run Code Online (Sandbox Code Playgroud)
看起来这个用一个词:
@z = @a.join.scan(/\bhappy\b/i)
Run Code Online (Sandbox Code Playgroud)
但是,当我加入它时它不会像我期望的那样起作用.
@z = @a.join.scan(/\bhappy|good\b/i)
Run Code Online (Sandbox Code Playgroud)
期待(快乐2x和好1x):
@z.size => 3
Run Code Online (Sandbox Code Playgroud)
结果它给了我:
@z.size => 2
Run Code Online (Sandbox Code Playgroud) 我想跑一个街区60秒.到目前为止,我所提出的并没有按照需要突破障碍.
@start_time = Time.now
stream_some_data do |x|
# .. do something ...
break if Time.now == @start_time + 60
end
Run Code Online (Sandbox Code Playgroud) 我想在生产中运行一个脚本.此脚本生成记录并将其插入生产数据库.我的问题是脚本试图将记录插入开发数据库.
run_report.rb
ENV['RAILS_ENV'] = 'production'
require 'rubygems'
require 'daemons'
Daemons.run('report.rb')
Run Code Online (Sandbox Code Playgroud)
report.rb
ENV['RAILS_ENV'] = 'production'
...
@r = Report.new(:info => @info)
@r.save
Run Code Online (Sandbox Code Playgroud)
我试过了:script/rails runner run_report.rb start
script/rails runner run_report.rb开始生产
ArrayList<Rectangle> list = new ArrayList<Rectangle>();
for (int i=0; i < 10; i++)
{
list.add(new Rectangle(10,20));
}
for (int i=0; i < list.size(); i++ )
{
Rectangle rec = list.get(i);
System.out.print("Element " + i +" ");
System.out.println("x=" + rec.getX()+" y=" + rec.getY());
}
Run Code Online (Sandbox Code Playgroud)
此输出给我:
Element 0 x=0.0 y=0.0
Element 1 x=0.0 y=0.0
Element 2 x=0.0 y=0.0
Element 3 x=0.0 y=0.0
Element 4 x=0.0 y=0.0
Element 5 x=0.0 y=0.0
Element 6 x=0.0 y=0.0
Element 7 x=0.0 y=0.0
Element 8 x=0.0 y=0.0
Element …Run Code Online (Sandbox Code Playgroud)