草庐IT

beginning_of_hour

全部标签

子类中的 Ruby : Invoke overridden method of parent class,

假设我有从A派生的B类是否可以像这样调用A的重写方法?classAdefmethod1enddefmethod2endendclassB#与HowdoIcallanoverriddenparentclassmethodfromachildclass?不完全相同,但我们似乎想做同样的事情。 最佳答案 我在这里假设B应该继承自A,而您只是在示例代码中输入了一个错字。如果不是这样,就没有办法做你想做的事情。否则,您可以通过将A的method2实例方法绑定(bind)到您当前的B对象并调用它来使用反射来做您想做的事这个:classAdefm

ruby-on-rails - rails : How to test if an attribute of a class object is required in the model policy?

简单地说,我有一个模型用户,其中包含名称、电子邮件和评论作为属性。validates_presence_of:namevalidates_presence_of:email所以“姓名”和“电子邮件”是必需的,但不是“评论”。my_user=User.new我想找到一种方法来测试像my_user.name.required?或User.name.required?类似的东西。我的目标是创建一个表单,并根据该项目是否设置为“validates_presence_of”,将特定类动态添加到表单项目span或td我试图搜索它,但没有找到任何相关信息。有没有简单的方法可以做到这一点?谢谢

ruby 正则表达式 : capture the path of url

我想从任何URL中提取其路径。例如:网址:https://stackoverflow.com/questions/ask路径:questions/ask应该不难:url[/(?:\w{2,}\/).+/]但我认为我使用了错误的模式来“忽略这个”('?:'-不起作用)。什么是正确的方法? 最佳答案 我建议您不要使用正则表达式来执行此操作,而是使用内置的URI库:require'uri'uri=URI::parse('http://stackoverflow.com/questions/ask')putsuri.path#results

ruby - Ruby 的 BEGIN 是做什么的?

BEGIN在Ruby中是什么意思,如何调用?例如,给出这段代码:puts"Thisissentence1."BEGIN{puts"Thisissentence2."}为什么puts"Thisissentence2."先执行? 最佳答案 BEGIN和END设置在执行任何其他内容之前调用的block,或者在其他所有内容之后,就在解释器退出之前调用。例如,运行这个:END{puts'ENDblock'}puts'foobar'BEGIN{puts'BEGINblock'}输出:BEGINblockfoobarENDblock通常我们会对B

ruby - 导轨 : Advantages of storing session in database?

我只是想知道为什么将session存储在数据库中?将session存储在数据库中有什么好处吗? 最佳答案 数据库或memcached的优势在于session数据无法在客户端被篡改,并且您可以存储比使用cookie(4kB)更大的数据量。如果您的session存储在cookie或数据库中,并且重新启动网络服务,则session数据不会丢失。只有存储在memcached中才可能丢失。如果服务器是负载平衡的,那么session数据将传递到为请求提供服务的Web服务器,因此这不是cookie、数据库或内存缓存session的问题。cooki

Ruby 1.9、YAML 和字符串编码 : how to lead a life of sanity?

在我看来,ruby1.9附带的YAML库是编码失聪的。这意味着在生成YAML时,它将采用任何字节串,并转义任何不输出干净ASCII的字节序列。这很蹩脚,但可以接受。我的问题恰恰相反。从所述YAML转储加载内容时。在下面的示例中,我创建了一个UTF-8字符串,将其转储为!binary类型。当我加载它时,它的编码是ASCII-8BIT。在示例的最后,我尝试将原始字符串和重新加载的字符串与另一个UTF-8字符串连接起来。后者将因Encoding::CompatibilityError而失败。require'yaml's0="Iñtërnâtiônàlizætiøn"y=s0.to_yamls

ruby - Rspec 模拟错误 : wrong number of arguments

我正在尝试使用Rspec对StripeAPI进行stub,但我遇到了一个问题。这是我的代码的样子:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)这是我遇到的错误:Failure/Error:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)ArgumentError:wrongnumberofarguments(0for3..6)

ruby-on-rails - 为什么我不想到处使用 inverse_of?

如这里所述:http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.htmlinverse_of似乎告诉Rails缓存内存关联并最小化数据库查询。他们的例子是:classDungeon:dungeonhas_one:evil_wizard,:inverse_of=>:dungeonendclassTrap:trapsend他们立即跟进:for`belongs_to`associations`has_many`inverseassociationsareignored.所以我有几个问题。has_m

ruby-on-rails - cucumber + capybara : Problem with a scenario that redirects the browser outside of my app

GivenIhavearailsappAndI'musingcucumberAndI'musingcapybaraAndIhaveanactionthatresultsinaredirect_to"http://some.other.domain.com/some_path"WhenItestthisactionThenthein-appportionofthetestworksfineButIseethiserror:Noroutematches"/some_path"with{:method=>:get}(ActionController::RoutingError)所以capyb

ruby-on-rails - rails 3 : validate presence of at least one has many through association item

我有两个模型:Project和ProjectDiscipline:classProject:destroyhas_many:project_disciplines,through::project_disciplinizationsattr_accessible:project_discipline_idsattr_accessible:project_disciplines_attributesaccepts_nested_attributes_for:project_disciplines,:reject_if=>proc{|attributes|attributes['name'