请考虑以下代码:
$ irb
> s = "asd"
> s.object_id # prints 2171223360
> s[0] = ?z # s is now "zsd"
> s.object_id # prints 2171223360 (same as before)
> s += "hello" # s is now "zsdhello"
> s.object_id # prints 2171224560 (now it's different)
Run Code Online (Sandbox Code Playgroud)
似乎可以在不创建新字符串的情况下更改单个字符.但是,附加到字符串显然会创建一个新字符串.
Ruby中的字符串是否可变?
我在使用Rails form_for帮助程序时遇到(我认为)路由错误.我一直在寻找这个问题,但复数形式的"static_event"是"static_events",所以我很茫然.任何帮助都会得到赞赏.这是细节....
ActionView::Template::Error (undefined method `static_events_path' for #<#<Class:0x007f9fcc48a918>:0x007f9fcc46fa78>):
Run Code Online (Sandbox Code Playgroud)
我的型号:
class StaticEvent < ActiveRecord::Base
attr_accessible :content, :title, :discount, :location, :day_of_week, :start_time
Run Code Online (Sandbox Code Playgroud)
我的控制器:
class StaticEventsController < ApplicationController
before_filter :authenticate, :only => [:create, :destroy]
before_filter :authorized_user, :only => [:destroy]
def new
@title = "Share An Event"
@static_event = StaticEvent.new
end
def create
@static_event = current_user.static_events.build(params[:event])
if @static_event.save
flash[:success] = "Event Shared"
redirect_to @static_event #this was the old version
else
render :new
end
end
Run Code Online (Sandbox Code Playgroud)
路线:
match '/static-events/new', :to => 'static_events#new'
match '/static-events/', …Run Code Online (Sandbox Code Playgroud) 如何在Windows 7上安装RVM?
它说要安装RVM,并使用以下脚本:
user$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么做.如果我将其键入cmd,则会显示一条错误消息,说它"此时意外".
如何安装RVM?
我想在数组中添加一个元素,但实际上没有更改该数组,而是返回一个新数组.换句话说,我想避免:
arr = [1,2]
arr << 3
Run Code Online (Sandbox Code Playgroud)
哪个会回归:
[1,2,3]
Run Code Online (Sandbox Code Playgroud)
改变arr本身.如何避免这种情况并创建一个新阵列?
任何人都可以向我展示一种检查哈希中是否存在密钥的红宝石方法,如果没有,则为其提供默认值.我假设有一个衬垫使用,除非这样做,但我不知道该使用什么.
我正在使用Rails API应用程序中的reset_password方法.当该端点被命中时,ActiveJob将排队,该命令将触发对Mandrill(我们的交易电子邮件客户端)的请求.我正在尝试编写测试以确保在控制器端点被命中时ActiveJob正确排队.
def reset_password
@user = User.find_by(email: params[:user][:email])
@user.send_reset_password_instructions
end
Run Code Online (Sandbox Code Playgroud)
send_reset_password_instructions在创建ActiveJob之前创建了一些url等,其代码如下:
class SendEmailJob < ActiveJob::Base
queue_as :default
def perform(message)
mandrill = Mandrill::API.new
mandrill.messages.send_template "reset-password", [], message
rescue Mandrill::Error => e
puts "A mandrill error occurred: #{e.class} - #{e.message}"
raise
end
end
Run Code Online (Sandbox Code Playgroud)
目前我们没有为ActiveJob使用任何适配器,所以我只想检查Rspec ActiveJob是否排队.
目前我的测试看起来像这样(我正在使用工厂女孩来创建用户):
require 'active_job/test_helper'
describe '#reset_password' do
let(:user) { create :user }
it 'should create an ActiveJob to send the reset password email' do
expect(enqueued_jobs.size).to eq 0
post :reset_password, user: { email: user.email }
expect(enqueued_jobs.size).to eq 1
end …Run Code Online (Sandbox Code Playgroud) 在RSpec中,特别是版本> = 3,之间有什么区别:
allow设置了返回测试双打参数,消息的预期,然后使用expect,使在返回的测试双打断言expect用于设置参数的期望并返回测试双精度或者只是语义学?我知道提供/指定返回值expect是RSpec模拟2.13中的语法,但据我所知,RSpec模拟3中的语法更改使用allow.
但是,在下面的(传递)示例代码中,使用allow/ expect或只是expect/ and_return似乎生成相同的结果.如果一种语法比另一种语言更受青睐,也许我会期望有某种弃用通知,但由于没有,似乎两种语法都被认为是有效的:
class Foo
def self.bar(baz)
# not important what happens to baz parameter
# only important that it is passed in
new
end
def qux
# perform some action
end
end
class SomethingThatCallsFoo
def some_long_process(baz)
# do some processing
Foo.bar(baz).qux
# do other processing
end
end
describe SomethingThatCallsFoo do
let(:foo_caller) { SomethingThatCallsFoo.new …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用捆绑器与rbenv.我一直工作到今天.我唯一能做的就是打破它gem pristine --all或者gem cleanup?尝试安装bundler时出现以下错误.
Antarrs-MacBook-Pro:some-app antarrbyrd$ sudo gem install bundler
Password:
Bundler gave the error "Could not find mail-2.5.4 in any of the sources" while processing "/Users/antarrbyrd/dev/some-app/Gemfile". Perhaps you forgot to run "bundle install"?
Successfully installed bundler-1.7.12
Parsing documentation for bundler-1.7.12
Done installing documentation for bundler after 3 seconds
1 gem installed
Antarrs-MacBook-Pro:some-app antarrbyrd$ bundle install
/Users/antarrbyrd/.rbenv/versions/2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/dependency.rb:315:in `to_specs': Could not find 'bundler' (>= 0) among 8 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/antarrbyrd/.gem', execute `gem env` for …Run Code Online (Sandbox Code Playgroud) 我感觉下面的解决方案
class Fixnum
def +(x)
self + x + 1
end
end
Run Code Online (Sandbox Code Playgroud)
不应该工作,因为+将递归调用.
我目前正在与其他一些开发人员一起开发Rails应用程序,并且通过Angular通过AJAX对服务器进行了POST.有时,我们注意到InvalidAuthenticityToken我们的电子邮件日志中出现了一些例外情况,这导致我们希望采取行动.
由于此请求是通过Angular发出的,我的信念是我们将服务器视为API,我们应该使用它protect_from_forgery with: :null_session.但是,protect_from_forgery with: :reset_session似乎为我们提供了相同的分辨率.
我不想盲目地插入代码只是因为它是推荐的,所以我想知道这两种伪造保护方法之间的区别.我何时会使用其中一个,为什么我更喜欢它的使用?