Tag: 哈希

如何在Ruby中实现注入?

我需要能够使用此调用: h = x.inject({}) {|a, b| a[b.one] = b.two; a} 其中x是一对Couple对象(这些对象只包含两个数字字段,一个和两个)。 我不确定如何在Couple中实现inject方法。

Twitter API – Ruby Twitter Gem

如何访问Twitter API返回的Twitter :: Cursor哈希值? 我正在关注Jumpstartlab Microblogger教程,通过jumpstart_auth gem使用Twitter gem 。 我正在迭代4步骤1.我可以使用以下代码返回一个friends对象: def friends_last_tweets friends = client.friends puts friends end => Twitter::Cursor:0x00000104051928 但是,在这种情况下,示例帐户“客户端”有两个“朋友”而不仅仅是一个,所以为什么它只返回一个对象? 我想也许那个对象就是数组或数组,所有的朋友都相应地在哈希值中,因此使用[]来访问,但这会返回“Twitter :: Cursor的未定义方法”。 我在Twitter :: Cursor对象上运行each ,它返回两个fixnums: def friends_last_tweets friends = client.friends friends.each { |f| puts f } end => 18908095 108528349 所以这些数字肯定必须代表我认为的Twitter :: Cursor对象中的每个“朋友对象”。 我需要访问该对象中的键/值对,但我尝试的哈希访问导致未定义的方法或变量。 如果它的版本问题相关,我使用的是Twitter5.11.0和Jumpstart_auth 0.6.0。

合并哈希数组

我有两个数组,每个数组都包含属性哈希的数组。 Array1 => [[{attribute_1 = A}, {attribute_2 = B}], [{attribute_1 = A}, {attribute_4 = B}]] Array2 => [{attribute_3 = C}, {attribute_2 = D}], [{attribute_3 = C, attribute_4 = D}]] 数组中的每个数组都保存对象的属性哈希值。 在上面的例子中,我正在使用两个对象。 每个数组中有两个属性用于两个对象中的每一个。 如何合并这两个数组? 我试图获得一个’对象’数组的单个数组(从一开始就没有办法获得单个数组,因为我必须进行两次不同的API调用才能获得这些属性)。 DesiredArray => [[{attribute_1 = A, attribute_2 = B, attribute_3 = C, attribute_4 = D}], [{attribute_1 = A, attribute_2 = B, attribute_3 […]

哈希默认值是具有相同默认值的哈希

像这样设置哈希的默认值: hash = Hash.new { |hsh, key| hsh[key] = {} } 将为未知密钥创建(并分配)新哈希,但对于创建的哈希的未知密钥将返回nil : hash[:unkown_key] #=> {} hash[:unkown_key][:nested_unknown] #=> nil 我可以让它适用于第二级,如下所示: hash = Hash.new do |hsh, key| hsh[key] = Hash.new { |nest_hsh, nest_key| nest_hsh[nest_key] = {} } end 但是,它在第​​三级不起作用: hash[:unkown_key][:nested_unknown] #=> {} hash[:unkown_key][:nested_unknown][:third_level] #=> nil 如何让它在任意级别工作? hash[:unkown_key][:nested_unknown][:third_level][…][:nth_level] #=> {}

在ruby 2.2.0中创建哈希的新方法

在ruby 2.2.0中你可以写这样的哈希: hash = { ‘new_possible_style’: :of_hashes } hash[:new_possible_style] #=> :of_hashes hash[‘new_possible_style’] #=> nil 我无法意识到实现这种风格的原因。 如果我需要字符串作为键(例如,对于某些第三方库),我仍然需要使用旧式哈希。 这个’function’的用例是什么? 核心开发人员为什么要添加这种风格? 提前致谢。

一个关于ruby哈希的恼人问题

为什么ruby有时会将哈希解释为代码块? 例如,我有一个名为’required’的函数,它接受一个哈希所以我做: required { base_uri: String, endpoints: String, destination: String, credentials: Hash} 它认为它是一块代码和错误的unexpected tSYMBEG 。 修复此问题的唯一方法是在其周围放置括号,使其显示为: required({ base_uri: String, endpoints: String, destination: String, credentials: Hash}) 然后它开始将其作为哈希读取(就像我想要的那样)。 我并不打算在哈希周围放置括号,令人恼火的是它不一致,有时是有效的,有时则不然。 这是怎么回事?

使用反向引用和散列时的Ruby gsub问题

以下代码定义了具有正则表达式(键)和替换(值)的哈希。 然后它迭代哈希并相应地替换字符串。 简单的字符串替换效果很好,但是当我需要在替换它之前计算resut(几年到几天的情况发生变化)时,它不会。 并且预先定义哈希是关键。 我错过了什么? 任何帮助将非常感谢。 a = “After 45 years we cannot use this thing.” hash = { /(\d+) years/ => “#{$1.to_f*2}” + ‘ days’, /cannot/ => ‘of course we CAN’ } hash.each {|k,v| a.gsub!(k) { v } } puts a 谢谢!

动态显示html表中的哈希数组?

我有一个名为“products”的数据库,它有一个使用名为data hstore的字段。 目前,在我的index.html.haml中,我只是循环浏览产品并将其data显示为哈希: – @products.each do |product| =product.data #THIS PRINTS A HASH %hr 例如,哪个可以打印哈希,如: {“Name”=>”Example”,”type”=>”book”, “price”=>”7.99”} 我想创建一个可以保存动态数量的键和值的HTML表,并将它们打印到具有与键对应的值的列中。 这是一个图表: 谢谢大家的帮助!

如何使用多个{‘not found’}干扰方法?

我正在尝试优雅地处理以下错误的JSON,其中Hash#fetch似乎不是一个选项( 使用Hash #fetch优雅地处理错误的JSON ): 实时应用: http : main_controller.rb ( main_controller.rb ) 显然,最好的方法是在每个参数的末尾添加{ ‘not found’ } : mashie.products.each do |product| product.extend Hashie::Extensions::DeepFetch product.name = product.deep_fetch :name { ‘not found’ } product.brand = product.deep_fetch :brand, :name { ‘not found’ } product.price = product.deep_fetch :sale_price { ‘not found’ } product.currency = product.deep_fetch :currency { ‘not found’ } @products […]

迭代ruby中的哈希数组

所以,如果我有像这样的哈希数组:(ruby初学者) input = [ {“last_name”=>”Gay”, “first_name”=>”Rudy”, “display_name”=>”Rudy Gay”, “position”=>”SF”, “minutes”=>39, “points”=>25, “assists”=>6}, {“last_name”=>”Collison”, “first_name”=>”Darren”, “display_name”=>”Darren Collison”, “position”=>”PG”, “minutes”=>39, “points”=>14, “assists”=>4} ] 我将如何遍历数组以及迭代每个哈希以得到类似这样的东西: player1 = {display_name =>“rudy gay”,“position”=>“SF”} player2 = {display_name =>“darren collison”,“position”=>“PG”} 它会是什么样的 input.each do |x| Player.create(name: x[‘display_name’], position: x[‘position’] end (假设我有一个玩家模型) 有没有更好的方法来实现这一目标? 谢谢!