Tag: 范围

在方法定义中使用$ 1,$ 2等全局变量

给出以下两段代码: def hello(z) “hello”.gsub(/(o)/, &z) end z = proc {|m| p $1} hello(z) # prints: nil def hello z = proc {|m| p $1} “hello”.gsub(/(o)/, &z) end hello # prints: “o” 为什么这两段代码的输出不同? 有没有办法从方法定义外部将块传递给gsub ,以便以与在方法定义中给出块的方式相同的方式计算变量$1 , $2 ?

Sinatra具有持久变量

我的sinatra应用程序必须解析一个~60MB的XML文件。 这个文件几乎没有变化:在夜间的cron工作中,它被另一个人覆盖。 是否存在将解析后的文件作为变量保存在内存中的技巧或方法,以便我可以在传入请求中读取它,但不必为每个传入请求一遍又一遍地解析它? 一些Pseudocode来说明我的问题。 get ‘/projects/:id’ return @nokigiri_object.search(“//projects/project[@id=#{params[:id]}]/name/text()”) end post ‘/projects/update’ if params[:token] == “s3cr3t” @nokogiri_object = reparse_the_xml_file end end 我需要知道的是,如何创建这样一个@nokogiri_object,以便在Sinatra运行时它仍然存在。 这有可能吗? 或者我需要一些存储空间?

@instance_variable在ruby块内不可用?

使用以下代码: def index @q = “” @q = params[:search][:q] if params[:search] q = @q @search = Sunspot.search(User) do keywords q end @users = @search.results end 如果使用@q而不是q,则搜索始终返回空查询(“”)的结果。 为什么是这样? @q变量是否对do … end块不可用?

如何查找范围数组中是否包含范围?

例 business_hours[‘monday’] = [800..1200, 1300..1700] business_hours[‘tuesday’] = [900..1100, 1300..1700] … 然后我有一堆事件占据了这些间隔中的一些,例如 event = { start_at: somedatetime, end_at: somedatetime } 迭代从某个日期到某个日期的事件,我创建了另一个数组 busy_hours[‘monday’] = [800..830, 1400..1415] … 现在我的挑战是 创建包含business_hours减去busy_hours的available_hours数组 available_hours = business_hours – busy_hours 如果持续时间为30分钟,请在available_hours中找到可用的时间段。 在上面的例子中,这种方法将返回 available_slots[‘monday’] = [830..900, 845..915, 900..930, and so on] 并非它为指定持续时间的插槽以15分钟为增量检查available_hours。 谢谢您的帮助!

如何将整数数组汇总为范围数组?

我想接受以下输入: [1,2,4,5,6,7,9,13] 把它变成如下的东西: [[1,2],[4,7],[9,9],[13,13]] 每个子数组代表一个整数范围。

范围数组的索引数组

ruby的范围非常酷。 我最终得到这样的数组: geneRanges = [(234..25), (500..510), (1640..1653)] 然后必须删除它们的一部分。 为此我: genePositions = geneRanges.collect {|range| range.entries }.flatten => [500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 1640, 1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652, 1653] 它们被操纵,因此一些数字被排除在外,其他数字可能被添加。 我最终可能会这样: [505, 506, 507, 600, 601, 602, 603, 1643, 1644, 1645, 1646, 1647, […]

Ruby:两个范围之间的交集

在ruby中,给定两个日期范围,我想要表示两个日期范围的交集的范围,或者如果没有交叉则为nil。 例如: (Date.new(2011,1,1)..Date.new(2011,1,15)) & (Date.new(2011,1,10)..Date.new(2011,2,15)) => Mon, 10 Jan 2011..Sat, 15 Jan 2011 编辑:应该说我希望它也适用于DateTime,所以间隔可以缩短到分钟和秒: (DateTime.new(2011,1,1,22,45)..Date.new(2011,2,15)) & (Date.new(2011,1,1)..Date.new(2011,2,15)) => Sat, 01 Jan 2011 22:45:00 +0000..Tue, 15 Feb 2011

Rails3:将范围与OR结合

我需要将名称范围与运算符组合……类似于: class Product < ActiveRecord::Base belongs_to :client scope :name_a, where("products.name = 'a'") scope :client_b, joins(:client).where("clients.name = 'b'") scope :name_a_or_b, name_a.or(client_b) end 谢谢

控制器的所有操作的相同实例变量

我有一个rails控制器,定义了两个动作: index和show 。 我有一个在index action中定义的实例变量。 代码如下所示: def index @some_instance_variable = foo end def show # some code end 如何访问show.html.erb模板中的show.html.erb ?

我如何对我的默认范围进行rspec测试

我的模型有default_scope(:order =>’created_at’)我的测试(rspec,factory girl,shoulda等)是: require ‘spec/spec_helper.rb’ describe CatMembership do context “is valid” do subject { Factory.build(:cat_membership) } it { should be_valid } it { should belong_to :cat} it { should belong_to :cat_group} it { should have_db_column(:start_date)} it { should have_db_column(:end_date)} end end