我需要Ruby中的双向哈希表.例如:
h = {:abc => 123, :xyz => 789, :qaz => 789, :wsx => [888, 999]}
h.fetch(:xyz) # => 789
h.rfetch(123) # => abc
h.rfetch(789) # => [:xyz, :qaz]
h.rfetch(888) # => :wsx
Run Code Online (Sandbox Code Playgroud)
方法rfetch意味着反向提取,只是我的建议.
注意三件事:
rfetch返回所有这些键,以数组形式打包.rfetch在数组的元素中查找它的参数.fetch和rfetch在固定时间内应该执行.Ruby中是否存在这种结构(包括外部库)?
我考虑过使用两个单向Hashes同步修改它们中的一个(并将其打包到类中以避免同步问题),但也许我可以使用已有的解决方案?
我有一个对象数组,其中一些响应:描述,我想从第一个描述得到描述.我能做到这一点:
objects.detect{|o| o.try(:description)}.description
Run Code Online (Sandbox Code Playgroud)
或这个:
objects.map{|o| o.try(:description)}.detect{|o| o}
Run Code Online (Sandbox Code Playgroud)
但第一个不是DRY(描述在那里两次),第二个是在找到值之前遍历整个数组.ruby标准库中有什么东西,或者Rails对它的扩展,这可以让我做这样的事情:
objects.detect_and_return{|o| o.try(:description)}
Run Code Online (Sandbox Code Playgroud)
我知道我可以轻松地编写它,但标准库足够大,我可能不需要.有没有像我这样的功能detect_and_return?
我根本无法获得文档中 "写入" 示例下的"To a String"示例.
ruby -v返回:ruby 1.9.2p290(2011-07-09修订版32553)[x86_64-darwin10.8.0]
我无法工作的文档中的示例如下:
csv_string = CSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
end
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
wrong number of arguments (0 for 1)
Run Code Online (Sandbox Code Playgroud)
所以看起来我错过了一个论点,在这里的文档中它指出:
This method wraps a String you provide, or an empty default String
Run Code Online (Sandbox Code Playgroud)
但是当我传入一个空字符串时,它会给我以下错误:
No such file or directory -
Run Code Online (Sandbox Code Playgroud)
我不打算生成一个csv文件,我只是想创建一个csv字符串,我将其作为文本发送给用户.
我的应用程序将以下json对象发布到rails.
[{\"completed\":false,\"id\":196,\"position\":0,\"title\":\"Item 1\",\"updated_at\":\"2011-08-03T21:17:09Z\"},{\"completed\":false,\"id\":193,\"position\":1,\"title\":\"Item X\",\"updated_at\":\"2011-08-03T21:16:19Z\"},{\"completed\":false,\"id\":197,\"position\":2,\"title\":\"Item 2\",\"updated_at\":\"2011-08-03T21:17:11Z\"},{\"completed\":false,\"id\":192,\"position\":3,\"title\":\"Item Z\",\"updated_at\":\"2011-08-03T21:16:17Z\"},{\"completed\":false,\"id\":194,\"position\":4,\"title\":\"Item Y\",\"updated_at\":\"2011-08-03T21:16:21Z\"},{\"completed\":false,\"id\":195,\"position\":5,\"title\":\"Item P\",\"updated_at\":\"2011-08-03T21:16:25Z\"},{\"completed\":false,\"id\":199,\"position\":6,\"title\":\"Why you no stay done?\",\"updated_at\":\"2011-08-03T23:22:41Z\"},{\"completed\":false,\"id\":200,\"position\":7,\"title\":\"Wow\",\"updated_at\":\"2011-08-04T00:02:07Z\"},{\"completed\":false,\"id\":201,\"position\":8,\"title\":\"Hello World\",\"updated_at\":\"2011-08-04T00:02:11Z\"},{\"completed\":false,\"id\":202,\"position\":9,\"title\":\"GOLDEN GATE BRIDGE CATALYST,\",\"updated_at\":\"2011-08-04T00:44:19Z\"}]
Run Code Online (Sandbox Code Playgroud)
我无法遍历对象.我可以:
JSON.parse(list_items_open).each do |item|
Rails.logger.info item.inspect
end
Run Code Online (Sandbox Code Playgroud)
哪个输出:
{"completed"=>false, "id"=>193, "position"=>0, "title"=>"Item X", "updated_at"=>"2011-08-03T21:16:19Z"}
{"completed"=>false, "id"=>197, "position"=>1, "title"=>"Item 2", "updated_at"=>"2011-08-03T21:17:11Z"}
{"completed"=>false, "id"=>192, "position"=>2, "title"=>"Item Z", "updated_at"=>"2011-08-03T21:16:17Z"}
{"completed"=>false, "id"=>196, "position"=>3, "title"=>"Item 1", "updated_at"=>"2011-08-03T21:17:09Z"}
{"completed"=>false, "id"=>194, "position"=>4, "title"=>"Item Y", "updated_at"=>"2011-08-03T21:16:21Z"}
{"completed"=>false, "id"=>195, "position"=>5, "title"=>"Item P", "updated_at"=>"2011-08-03T21:16:25Z"}
{"completed"=>false, "id"=>199, "position"=>6, "title"=>"Why you no stay done?", "updated_at"=>"2011-08-03T23:22:41Z"}
{"completed"=>false, "id"=>200, "position"=>7, "title"=>"Wow", "updated_at"=>"2011-08-04T00:02:07Z"}
{"completed"=>false, "id"=>201, "position"=>8, "title"=>"Hello World", "updated_at"=>"2011-08-04T00:02:11Z"} …Run Code Online (Sandbox Code Playgroud) 在shell中,我可以做到
$ cat name_of_file_with_a_lot_of_text | grep "What I am looking for"
Run Code Online (Sandbox Code Playgroud)
在Rails控制台内部,我可以实现类似的功能,例如当我运行命令并且输出很大时,尤其是数据库查询.
我知道将它输出为YAML,但这不是我想要的.
谢谢.
实际上,所有与gem相关的命令都会导致相同的错误消息,当从我从git repo克隆的现有rails应用程序内部运行时.
$ bundle install
Could not find tzinfo-0.3.27 in any of the sources
Run `bundle install` to install missing gems.
$ gem list
Could not find tzinfo-0.3.27 in any of the sources
Run `bundle install` to install missing gems.
$ bundle update
Could not find tzinfo-0.3.27 in any of the sources
Run `bundle install` to install missing gems.
$ rails -v
Could not find tzinfo-0.3.27 in any of the sources
Run `bundle install` to install missing gems.
Run Code Online (Sandbox Code Playgroud)
我以为我已经安装了rails ...(以下命令是从app目录外部运行的): …
我正在寻找一种方法来维护我在Ruby中使用的Hash的插入顺序.我的数据来自数据库,并且已按照我想要的方式进行分组/排序,但Ruby不保证在我的版本中维护Hash中的顺序1.8.4.
这有什么解决方法吗?如果没有,我可以创建自定义比较器吗?
这是哈希:
{
"February"=>[0.5667, 14.6834, 79.7666, 261.8668, 342.1167, 723.517],
"March"=>[0.0, 26.4667, 554.45, 681.3164, 2376.0668, 10353.0358],
"May"=>[2.75, 34.6666, 342.1831, 1331.8999, 1589.617, 9282.9662],
"July"=>[1.9, 2.3666, 59.45, 302.1501, 554.1652, 5195.0839],
"June"=>[0.15, 24.2166, 244.1498, 335.6834, 536.067, 1498.949],
"August"=>[0.0, 0.4, 9.3668, 30.7164, 67.7504, 162.0337],
"April"=>[0.0, 8.3, 68.9331, 357.9168, 815.9662, 2870.217]
}
Run Code Online (Sandbox Code Playgroud)
任何想法都会很棒,谢谢
我有一个在Heroku上托管的简单Rack应用程序.config.ru:
use Rack::Static,
:urls => ["/stylesheets", "/images", "/javascripts"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
Run Code Online (Sandbox Code Playgroud)
如何为此添加HTTP Basic Auth?奖励积分,如果它只适用于生产环境.
谢谢
s = "#main= 'quotes'
s.gsub "'", "\\'" # => "#main= quotes'quotes"
Run Code Online (Sandbox Code Playgroud)
这似乎是错误的,我希望得到 "#main= \\'quotes\\'"
当我不使用转义字符时,它按预期工作.
s.gsub "'", "*" # => "#main= *quotes*"
Run Code Online (Sandbox Code Playgroud)
所以必须与逃避有关.
使用ruby 1.9.2p290
我需要用反斜杠和引号替换单引号.
更多的不一致:
"\\'".length # => 2
"\\*".length # => 2
# As expected
"'".gsub("'", "\\*").length # => 2
"'a'".gsub("'", "\\*") # => "\\*a\\*" (length==5)
# WTF next:
"'".gsub("'", "\\'").length # => 0
# Doubling the content?
"'a'".gsub("'", "\\'") # => "a'a" (length==3)
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
可能重复:
attr_accessor默认值
我正在使用Ruby on Rails 3.0.9,我想attr_accessor在我继承的类\ model中初始化一些属性值ActiveRecord::Base.那是,
...在我的模块中我有:
class User < ActiveRecord::Base
attr_accessor :attribute_name1,
:attribute_name2,
:attribute_name3,
...
end
Run Code Online (Sandbox Code Playgroud)
并且我想设置true的所有attr_accessor属性值.我怎样才能做到这一点?
PS:当然我想解决上述问题,接近"Ruby on Rails Way".我知道after_initialize回调但是通过使用该方法,我应该重复每个attribute_name<N>语句,我想将值设置true在该after_initialize语句中(...这不是 DRY - 不要重复自己).也许有更好的方法来实现这一目标.当您声明这些属性时,有没有办法attr_accessor"动态" 设置属性值?也就是说,我希望立即声明和设置attr_accessor属性!
ruby attributes initialization ruby-on-rails ruby-on-rails-3