考虑存储在哈希中的"人".两个例子是:
fred = {:person => {:name => "Fred", :spouse => "Wilma", :children => {:child => {:name => "Pebbles"}}}}
slate = {:person => {:name => "Mr. Slate", :spouse => "Mrs. Slate"}}
Run Code Online (Sandbox Code Playgroud)
如果"人"没有任何子女,则"儿童"元素不存在.所以,对于Slate先生,我们可以检查他是否有父母:
slate_has_children = !slate[:person][:children].nil?
Run Code Online (Sandbox Code Playgroud)
那么,如果我们不知道"slate"是一个"人"哈希呢?考虑:
dino = {:pet => {:name => "Dino"}}
Run Code Online (Sandbox Code Playgroud)
我们不能再轻易检查孩子了:
dino_has_children = !dino[:person][:children].nil?
NoMethodError: undefined method `[]' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
那么,你如何检查哈希的结构,特别是如果它被深深嵌套(甚至比这里提供的例子更深)?也许更好的问题是:这样做的"红宝石方式"是什么?
obj = SomeObject.new
def obj.new_method
"do some things"
end
puts obj.new_method
> "do some things"
Run Code Online (Sandbox Code Playgroud)
这没问题.但是,我需要在现有方法中做同样的事情:
def some_random_method
def obj.new_method
"do some things"
end
end
Run Code Online (Sandbox Code Playgroud)
也可以正常工作,但在方法中使用方法看起来非常可怕.问题是,有没有其他方法可以添加这样的方法?
我有简单的动作节目
def show
@field = Field.find_by(params[:id])
end
Run Code Online (Sandbox Code Playgroud)
我想要写它的规格
require 'spec_helper'
RSpec.describe FieldsController, type: :controller do
let(:field) { create(:field) }
it 'should show field' do
get :show, id: field
expect(response.status).to eq(200)
end
end
Run Code Online (Sandbox Code Playgroud)
但我有一个错误
Failure/Error: get :show, id: field
ArgumentError:
unknown keyword: id
Run Code Online (Sandbox Code Playgroud)
怎么解决?
Ruby/Rails在谈到基本用品的糖时会做很多很酷的事情,我认为有一种非常常见的情况,我想知道是否有人做过帮手或类似的东西.
a = Array.new(5, 1)
a.each_with_index do |x, i|
if i == 0
print x+1
elsif i == (a.length - 1)
print x*10
else
print x
end
end
Run Code Online (Sandbox Code Playgroud)
原谅丑陋,但这可以达到人们想要的......是否有一种红宝石的方式来对循环的第一个和最后一个做一些事情?
[编辑]我认为理想情况下这将是带有参数的数组的扩展(数组实例,所有元素函数,第一个元素函数,最后元素函数)......但我对其他想法持开放态度.
如何在不更改ruby中的类的情况下向异常消息添加信息?
我目前使用的方法是
strings.each_with_index do |string, i|
begin
do_risky_operation(string)
rescue
raise $!.class, "Problem with string number #{i}: #{$!}"
end
end
Run Code Online (Sandbox Code Playgroud)
理想情况下,我还想保留回溯.
有没有更好的办法?
a = b = c = d = 5
puts (a) >> 5
puts (b) >> 5
puts (b) >> 5
puts (b) >> 5
a= a+1
puts (a) >> 6
puts (b) >> 5
Run Code Online (Sandbox Code Playgroud)
我发现分配这样的值没有问题.我的问题是,应该像上面给出的那样分配还是像这样?
a , b, c, d = 5, 5, 5, 5
Run Code Online (Sandbox Code Playgroud) 我正在运行一些Ruby代码,每次日期更改时都会破坏Ruby文件.在文件中,我有不断的定义,比如
Tau = 2 * Pi
Run Code Online (Sandbox Code Playgroud)
当然,它们使解释器每次都显示不需要的"已初始化的常量"警告,所以,我想要具备以下功能:
def_if_not_defined(:Tau, 2 * Pi)
redef_without_warning(:Tau, 2 * Pi)
Run Code Online (Sandbox Code Playgroud)
通过编写我的所有常量定义,我可以避免警告:
Tau = 2 * Pi unless defined?(Tau)
Run Code Online (Sandbox Code Playgroud)
但它不够优雅,有点潮湿(不是干).
有更好的方法def_if_not_defined吗?怎么样redef_without_warning?
-
谢谢Steve的解决方案:
class Object
def def_if_not_defined(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.const_set(const, value) unless mod.const_defined?(const)
end
def redef_without_warning(const, value)
mod = self.is_a?(Module) ? self : self.class
mod.send(:remove_const, const) if mod.const_defined?(const)
mod.const_set(const, value)
end
end
A = 1
redef_without_warning :A, 2
fail 'unit test' unless A == …Run Code Online (Sandbox Code Playgroud) 我有一些宝石,包括ruby-debug在一个名为:development的捆绑组中.当我运行bundle命令时,这些gem被忽略,它只安装不在任何组中的gem.如何确保bundler不会忽略:development组中的gem?
编辑:这是我的Gemfile的样子.
source 'http://rubygems.org'
gem 'rails', '3.0.1'
# Auth gems
gem "devise", "1.1.3"
gem "omniauth"
# Bundle Mongoid gems
gem "mongoid", "2.0.0.beta.19"
gem "bson_ext"
# Asset gems
gem 'jquery-rails'
gem "jammit"
# Controller gems
gem 'inherited_resources', '1.1.2'
# View gems
gem 'haml'
gem 'formtastic', '~> 1.1.0'
# Nokogiri
gem "mechanize"
gem "json"
group :development do
gem "ruby-debug"
gem 'compass'
gem 'compass-colors'
gem 'pickler'
gem 'haml-rails'
gem 'rails3-generators'
gem "hpricot"
gem "ruby_parser"
gem 'fog'
end
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Rails 4.0.8中运行新创建的项目,但我收到并错误:
rails s
=> Booting WEBrick
=> Rails 4.0.8 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:121: warning: constant ::Fixnum is deprecated
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:121: warning: constant ::Bignum is deprecated
Exiting
/usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:124:in `block (2 levels) in <class:Numeric>': stack level too deep (SystemStackError)
from /usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
from /usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
from /usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
from /usr/local/lib/ruby/gems/2.4.0/gems/activesupport-4.0.8/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) …Run Code Online (Sandbox Code Playgroud) ruby ×10
bundler ×1
coding-style ×1
constants ×1
date ×1
exception ×1
hash ×1
redefine ×1
redefinition ×1
rspec ×1
rspec-rails ×1
rubygems ×1
string ×1