草庐IT

参数化

全部标签

ruby-on-rails - 工厂女孩在构建/创建时将参数传递给模型定义

模型/message.rbclassMessageattr_reader:bundle_id,:order_id,:order_number,:eventdefinitialize(message)hash=message@bundle_id=hash[:payload][:bundle_id]@order_id=hash[:payload][:order_id]@order_number=hash[:payload][:order_number]@event=hash[:concern]endend规范/模型/message_spec.rbrequire'spec_helper'de

ruby - Sidekiq 如何将参数传递给 perform 方法?

我有这个Sidekiqworker:classDealNoteWorkerincludeSidekiq::Workersidekiq_optionsqueue::emaildefperform(options={})ifoptions[:type]=="deal_watch_mailer"deal_watchers=DealWatcher.where("deal_id=?",options[:deal_id])deal_note=DealNote.find(options[:deal_note_id])current_user=User.find(options[:current_us

ruby - 如何将参数从父任务传递到 Rake 中的子任务?

我正在编写一个Rake脚本,其中包含带参数的任务。我弄清楚了如何传递参数以及如何使任务依赖于其他任务。task:parent,[:parent_argument1,:parent_argument2,:parent_argument3]=>[:child1,:child2]do#PerformParentTaskFunctionalitiesendtask:child1,[:child1_argument1,:child1_argument2]do|t,args|#PerformChild1TaskFunctionalitiesendtask:child2,[:child2_argum

ruby - 如何在 rake 任务的字符串参数中使用逗号?

我有以下Rakefile:task:test_commas,:arg1do|t,args|putsargs[:arg1]end并希望使用包含逗号的单个字符串参数来调用它。这是我得到的:%rake'test_commas[foo,bar]'foo%rake'test_commas["foo,bar"]'"foo%rake"test_commas['foo,bar']"'foo%rake"test_commas['foo,bar']"'foo%rake"test_commas[foo\,bar]"foo\我目前正在使用此pullrequesttorake中提出的解决方法,但有没有办法在不修

Ruby,如何将一个参数添加到一个你不知道它是否已经有任何其他参数的 URL

我必须向不确定的URL添加一个新参数,假设param=value。如果实际的URL已经有这样的参数http://url.com?p1=v1&p2=v2我应该将URL转换为另一个:http://url.com?p1=v1&p2=v2¶m=value但是如果URL还没有任何参数,像这样:http://url.com我应该将URL转换为另一个:http://url.com?param=value我担心用Regex解决这个问题,因为我不确定寻找&是否就足够了。我在想也许我应该将URL转换为URI对象,然后添加参数并再次将其转换为字符串。正在寻求已经处于这种情况的人的任何建议。更新为了帮

Ruby - 结构和命名参数继承

这个问题完全是关于Struct行为的,所以请不要问“为什么在广泛的体育世界中你要那样做?”此代码不正确,但它应该说明我试图了解的有关RubyStructs的内容:classPerson如您所见,尝试使用Structs定义类继承。但是,当然,当您尝试初始化ReligiousPerson或PoliticalPerson时,Ruby会变得脾气暴躁。因此,鉴于此说明性代码,如何使用这种使用Structs的类继承来继承命名参数? 最佳答案 您可以定义基于Person的新结构:classPerson结果:##在发布我的答案后我立即有了一个想法:

ruby - 在初始化时使用 attr_accessor 设置 ruby​​ 2.0 关键字参数

我怎样才能动态设置而不用写相同的代码。现在的代码是这样的:definitialize(keywords:keywords,title:title,url:url,adsetting:adsetting)self.keywords=keywordsself.title=titleself.url=urlself.adsetting=adsettingend如果列表变长,这很快就会失控。在ruby​​1.9中,我只需将哈希传递给该方法。像这样:definitialize(args)args.eachdo|k,v|instance_variable_set("@#{k}",v)unlessv

ruby - 具有可选查询参数的 Sinatra

我想创建一个带有可选查询参数的SinatraAPI路由。我可以按如下方式添加查询参数%r{^/mysql/data/(?)/start_time=(?\w*)/?}但是上面route对应的route是像"/mysql/data/:name/start_time=:start_time"我需要查询参数作为可选参数并以URL格式声明。例如:/mysql/data/:name?start_time=:start_time&end_time=:end_time在Sinatra中有什么方法可以做到这一点吗? 最佳答案 引自Sinatra文档:

ruby-on-rails - 将参数括起来以确保该 block 将与方法调用相关联

classUserscope:active,->{where(active:true)}end运行rubocop我收到以下警告:Parenthesizetheparam->{where(active:true)}tomakesurethattheblockwillbeassociatedwiththe->methodcall.我完全不知道我的scope定义与这个警告有什么关系。你呢?除了关闭检查之外,我该如何修复警告,因为它目前没有意义? 最佳答案 它要你这样做:scope:active,(->{where(active:true)

ruby - 如何多次执行带参数的 Rake 任务?

不可能从循环内调用相同的rake任务morethanonce.但是,我希望能够调用rakefirst并循环遍历数组并在每次迭代时使用不同的参数调用second。由于invoke只在第一次执行,我尝试使用execute,但是Rake::Task#execute不使用splat(*)运算符,只接受一个参数。desc"firsttask"task:firstdoother_arg="bar"[1,2,3,4].each_with_indexdo|n,i|ifi==0Rake::Task["foo:second"].invoke(n,other_arg)else#thisdoesn'twork