我无法在Rails文档中找到这个,但似乎'mattr_accessor'是普通Ruby 类中'attr_accessor'(getter&setter)的模块推论.
例如.在课堂上
class User
attr_accessor :name
def set_fullname
@name = "#{self.first_name} #{self.last_name}"
end
end
Run Code Online (Sandbox Code Playgroud)
例如.在一个模块中
module Authentication
mattr_accessor :current_user
def login
@current_user = session[:user_id] || nil
end
end
Run Code Online (Sandbox Code Playgroud)
这个帮助方法由ActiveSupport提供.
我正在寻找一种转储对象结构的方法,类似于PHP函数print_r和var_dump调试原因.
我一直在试图找出记录堆栈跟踪的正确方法.我遇到了这个链接,声明logger.error $!,$ !. backtrace是要走的路,但这对我来说不起作用log_error.根据文档,我没有看到如何将第二个参数传递给error方法仍然可行,因为rails使用的ruby logger只接受一个参数.
奇怪的(或可能不是)第二个论点在没有任何翻译投诉的情况下被接受.但是,我传递给它的任何内容都会被忽略.
谁能解释我错过的东西?任何洞察错误的第二个论点是什么以及吃什么?
我正在尝试找到在Rails中为对象设置默认值的最佳方法.
我能想到的最好的new方法是在控制器中设置方法的默认值.
如果可以接受或者有更好的方法,是否有人有任何意见?
我正在尝试运行gem install json并得到以下错误
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby extconf.rb
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling generator.c
linking shared-object json/ext/generator.bundle
clang: error: unknown argument: '-multiply_definedsuppress' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
make: *** [generator.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /opt/boxen/repo/.bundle/ruby/2.0.0/gems/json-1.8.0 for inspection.
Results logged to /opt/boxen/repo/.bundle/ruby/2.0.0/extensions/universal-darwin-13/2.0.0/json-1.8.0/gem_make.out
Run Code Online (Sandbox Code Playgroud)
我正在使用:
Os X:10.9.2
Xcode:5.1构建版本5B130a
命令行工具(CLT):5.1.0.0.1.1393561416
Ruby:ruby 2.0.0p247(2013-06-27修订版41674)[universal.x86_64-darwin13]
Ruby Gem:2.2 …
我想重用一些Cucumber步骤,但似乎找不到正确的方法.
我想写一个像这样的步骤:
Given /^I login with (.*) credentials$/ |type|
# do stuff with type being one of "invalid" or "valid"
end
Run Code Online (Sandbox Code Playgroud)
但是又有另一个步骤:
Given /^I login successfully$
# call "Given I login with valid credentials"
end
Run Code Online (Sandbox Code Playgroud)
因此在测试用户身份验证时我可以使用前者,但大多数其他地方,我可以使用后者,而不是实际上必须重新编码.
有没有办法调用其他步骤,或者我只是将逻辑放在辅助方法中,并从每个任务调用所述方法(基本上是一个方法提取重构,在阅读我的问题后让我相信这实际上是最好的方法无论如何)?
是否有可能从方法返回多个值?像这样的东西:
def someMethod()
return ["a", 10, SomeObject.new]
end
[a, b, c] = someMethod
Run Code Online (Sandbox Code Playgroud) 以上不是第一次工作,第二次工作.
尝试将ruby版本设置为2.0.0以用于任何新的shell窗口.
干
$ rvm use 2.0.0 --default
Run Code Online (Sandbox Code Playgroud)
给
Warning! PATH is not properly set up, '/home/durrantm/.rvm/gems/ruby-1.9.3-p125/
bin' is not at first place,
usually this is caused by shell initialization files - check them for '
PATH=...' entries,
it might also help to re-add RVM to your dotfiles: 'rvm get stable --au
to-dotfiles',
to fix temporarily in this shell session run: 'rvm use ruby-1.9.3-p125'
.
Using /home/durrantm/.rvm/gems/ruby-2.0.0-p247
Run Code Online (Sandbox Code Playgroud)
然后做同样的事情
$ rvm use 2.0.0 --default
Run Code Online (Sandbox Code Playgroud)
现在没有错误,即
$ rvm use 2.0.0 --default
Using …Run Code Online (Sandbox Code Playgroud) 在编写RSpec测试时,我发现自己编写了大量看起来像这样的代码,以确保在执行测试期间调用一个方法(为了论证,我们只能说我不能真正地查询状态调用后的对象,因为该方法执行的操作不容易看到效果).
describe "#foo"
it "should call 'bar' with appropriate arguments" do
called_bar = false
subject.stub(:bar).with("an argument I want") { called_bar = true }
subject.foo
expect(called_bar).to be_true
end
end
Run Code Online (Sandbox Code Playgroud)
我想知道的是:有比这更好的语法吗?我是否缺少一些时髦的RSpec非常棒,可以将上面的代码减少到几行?should_receive听起来它应该这样做但是进一步阅读它听起来并不完全是它的作用.