小编Man*_*Das的帖子

如何用另一个键替换哈希键

我有一个条件,我得到一个哈希

  hash = {"_id"=>"4de7140772f8be03da000018", .....}
Run Code Online (Sandbox Code Playgroud)

我希望这个哈希为

  hash = {"id"=>"4de7140772f8be03da000018", ......}
Run Code Online (Sandbox Code Playgroud)

PS:我不知道哈希中的键是什么,它们是随机的,每个键带有一个"_"前缀,我不想要下划线

ruby hash ruby-on-rails ruby-on-rails-3

174
推荐指数
7
解决办法
9万
查看次数

Rspec通过mTurk测试实时结果

我正在通过Rspec测试代码在mTurk创建点击,但同时我需要测试必须从mTurk发回的结果.为了节省每次测试的复杂性测试,我使用VCR在盒式磁带中记录HTTP请求.我该如何实现这个测试?

ruby rubygems ruby-on-rails mechanicalturk ruby-on-rails-3

12
推荐指数
1
解决办法
274
查看次数

如何使用关键字编写尾随方法

我有一种情况需要调用这样的东西:

class Office
   attr_accessor :workers, :id

   def initialize
      @workers = []
   end

   def workers<<(worker)
      type = worker.type
      resp = Organiation::Worker.post("/office/#{@id}/workers.json", :worker => {:type => type})
   end
end
Run Code Online (Sandbox Code Playgroud)

这是我需要打电话的地方

office = Office.new()

new_worker = Worker.new()

office.workers << new_worker
Run Code Online (Sandbox Code Playgroud)

我应该如何修改上面的worker方法以实现上面的代码.

ruby rubygems ruby-on-rails ruby-on-rails-3

9
推荐指数
1
解决办法
175
查看次数

问题在ruby中支持双向语法

我有一种情况需要调用这样的东西:

class Office

  attr_accessor :workers, :id

  def initialize
    @workers = []
  end

  def workers worker
    type = worker.type
    resp = Worker.post("/office/#{@id}/workers.json", :worker => {:type => type})
    worker = Worker.new()
    resp.to_hash.each_pair do |k,v|
      worker.send("#{k}=",v) if worker.respond_to?(k)
    end
    self.workers << worker
  end

end
Run Code Online (Sandbox Code Playgroud)

工人阶级

class Worker
  attr_accessor :office_id, :type, :id

  def initialize(options={})
    @office_id = options[:office].nil? ? nil : options[:office].id
    @type = options[:type].nil? ? nil : options[:type].camelize
    if !@office_id.nil?
       resp = self.class.post("/office/#{@office_id}/workers.json", :worker => {:type => @type})
       @id = resp.id
       office = options[:office] …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails

9
推荐指数
1
解决办法
121
查看次数

使用类似的键加载和生成Yaml文件

我有一个yaml文件文件有类似的键: -

sample_file.yml

line:
  title: line-name
  department: transcription

  input_formats:
    - input_format:
        name: company
        required: true
        valid_type: general
    - input_format:
        name: website
        required: false
        valid_type: url
Run Code Online (Sandbox Code Playgroud)

生成new_file.yml后,键按字母顺序排序: -

new_file.yml

line: 
  department: transcription
  input_formats: 
    - 
      input_format: 
      name: company
      required: true
      valid_type: general
    - 
      input_format: 
      name: website
      required: false
      valid_type: url

  title: line-name
Run Code Online (Sandbox Code Playgroud)

打开sample_file并生成new_file的代码如下: -

require 'yaml'
require 'ya2yaml'


@file = YAML::load(File.open("/Users/manish/Desktop/yaml/sample_file.yml"))
@new_file = File.new("/Users/manish/Desktop/yaml/new_file.yml", "w+") 
@new_file.syswrite(@file.ya2yaml(:hash_order => ['title','department','input_formats']))
Run Code Online (Sandbox Code Playgroud)

我正在使用"ya2yaml"gem生成yaml文件.为了获得与sample_file.yml中相同的顺序,我在这里使用了hash_order @new_file.syswrite(@file.ya2yaml(:hash_order => ['title','department','input_formats'])),但它不起作用.我如何保留订单?

ruby yaml rubygems ruby-on-rails ruby-on-rails-3

9
推荐指数
1
解决办法
1420
查看次数

设计ensure_authentication_token报告未定义的方法

用户模型看起来像这样:

before_save :ensure_authentication_token

devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable
  :token_authenticatable

attr_accessible :email, :password, :password_confirmation, 
                :remember_me, :first_name, :last_name, 
                :cell_phone, :city, :state, :country, :user_type
Run Code Online (Sandbox Code Playgroud)

在devise.rb文件中我没有注释:

config.token_authentication_key = :auth_token
Run Code Online (Sandbox Code Playgroud)

我的用户迁移如下:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
   create_table(:users) do |t|
    t.database_authenticatable :null => false
    t.recoverable
    t.rememberable
    t.trackable
    t.token_authenticatable
    t.timestamps
   end

   add_index :users, :email,                :unique => true
   add_index :users, :reset_password_token, :unique => true
   add_index :users, :authentication_token, :unique => true
  end

  def self.down
   drop_table :users
  end
end
Run Code Online (Sandbox Code Playgroud)

在创建用户时,会出现以下参数

{"utf8"=>"?",
 "authenticity_token"=>"+F8cjCoauVKhZPSJLhW+AAhui1DygBcODsYn4Va/ktY=",
 "user"=>{"first_name"=>"any_name",
 "last_name"=>"any_name",
 "email"=>"any_email@gmail.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]", …
Run Code Online (Sandbox Code Playgroud)

authentication ruby-on-rails devise ruby-on-rails-3 ruby-on-rails-3.1

8
推荐指数
1
解决办法
8376
查看次数

未初始化的常量Psych :: Syck(NameError)

在我的宝石中,我需要yaml并在我的电脑本地工作.

但是在将我的宝石推入rubygems.org并且当我尝试使用我的宝石时,我得到一个错误说=>"未初始化的常量Psych :: Syck(NameError)"

任何人都可以帮我解决这个问题吗?

PS

Ruby Version => ruby 1.9.2,
Gem Version => 1.6.2,
Bundler version => 1.0.15
Run Code Online (Sandbox Code Playgroud)

ruby rubygems ruby-on-rails ruby-on-rails-3

5
推荐指数
1
解决办法
3743
查看次数

获取类方法中属性的值

我有一个类方法,我想访问属性的值

class Run

  attr_accessor :line

  def self.output(options={})
    station_no = options[:station]
    title = options[:title]
    line = self.line
    station = line.stations[station_no-1]
  end
end
Run Code Online (Sandbox Code Playgroud)

在这个类方法中我想访问line属性的值,在类方法中我无法访问属性的值self.line.所以请建议我如何访问.

ruby rubygems ruby-on-rails ruby-on-rails-3

1
推荐指数
1
解决办法
361
查看次数