Tag: hash

如何基于Rails中的哈希键合并哈希数组

我想基于唯一性合并这两个数组: “template_variables”: [{ “info_top”: “Some string” }, { “info_bottom”: “Other string” }], “user_variables”: [{ “info_top”: “Default string” }, { “info_bottom”: “Other default string” }, { “other_info”: “Default number” }] 因此,如果我从user_variables数组开始并向其添加template_variables ,则替换找到匹配项的哈希值。 我想要的输出是: “new_variables”: [{ “info_top”: “Some string” }, { “info_bottom”: “Other string” }, { “other_info”: “Default number” }] 我已经尝试了user_variables.merge(template_variables)及其变体,但这似乎不适合哈希数组。 我该怎么做呢?

Rails:如何从哈希迭代产品? (亚马逊API /真空吸尘器)

扩展Rails:如何从哈希中提取值? (亚马逊API /真空吸尘器) – 如何将这些新挖出的值用于我的视图? 目前获得 : undefined method `image_url’ for # Extracted source (around line #5): main_controller.rb : class MainController < ApplicationController def index request = Vacuum.new('GB') request.configure( aws_access_key_id: 'ABCDEFGHIJKLMNOPQRST', aws_secret_access_key: '’, associate_tag: ‘lipsum-20’ ) params = { ‘SearchIndex’ => ‘Books’, ‘Keywords’=> ‘Ruby on Rails’, ‘ResponseGroup’ => “ItemAttributes,Images” } raw_products = request.item_search(query: params) hashed_products […]

将ruby hash转换为URL查询字符串…没有那些方括号

在Python中,我可以这样做: >>> import urlparse, urllib >>> q = urlparse.parse_qsl(“a=b&a=c&d=e”) >>> urllib.urlencode(q) ‘a=b&a=c&d=e’ 在Ruby [+ Rails]中,我无法弄清楚如何在没有“滚动我自己”的情况下做同样的事情,这看起来很奇怪。 Rails方式对我不起作用 – 它为查询参数的名称添加方括号,另一端的服务器可能支持也可能不支持: >> q = CGI.parse(“a=b&a=c&d=e”) => {“a”=>[“b”, “c”], “d”=>[“e”]} >> q.to_params => “a[]=b&a[]=c&d[]=e” 我的用例很简单,我希望使用URL的query-string部分中的某些值的值。 依靠标准库和/或Rails似乎很自然,写下这样的东西: uri = URI.parse(“http://example.com/foo?a=b&a=c&d=e”) q = CGI.parse(uri.query) q.delete(“d”) q[“a”] << "d" uri.query = q.to_params # should be to_param or to_query instead? puts Net::HTTP.get_response(uri) 但是,只有结果URI实际上是http://example.com/foo?a=b&a=c&a=d […]

Rails:如何从哈希中提取值? (亚马逊API /真空吸尘器)

如何从散列的Amazon Products API响应中提取标题,URL和图像的值(下面评论的部分)? 我正在使用Vacuum与亚马逊互动。 显然我不能使用map作为Vacuum::Response只接受to_h ? 目前获得 : can’t convert String into Integer main_controller.rb : class MainController < ApplicationController def index request = Vacuum.new('GB') request.configure( aws_access_key_id: 'ABCDEFGHIJKLMNOPQRST', aws_secret_access_key: '’, associate_tag: ‘lipsum-20’ ) params = { ‘SearchIndex’ => ‘Books’, ‘Keywords’=> ‘Ruby on Rails’, ‘ResponseGroup’ => “ItemAttributes,Images” } raw_products = request.item_search(query: params) hashed_products = raw_products.to_h # NOT […]

在哈希中查找重复键?

为了简化事情,我们假设我有下面的哈希值。 我想找到多个哈希中的键和哈希的名称。 理想情况下,我想最终结束 A is also in a A is also in b B is also in a B is also in b D is also in b D is also in c E is also in b E is also in c 我能想到的唯一方法是:将所有键放在一个数组中,对其进行排序,删除唯一元素,搜索包含剩余数组元素的每个哈希。 我猜这有点复杂和笨拙。 题 是否有更简单的方法可以在哈希中找到重复的键? !/usr/bin/ruby require ‘ap’ a = {} b = […]

为什么我把它推到arrays时没有得到正确的价值?

我有以下格式的一些数据。 # AB Tests # – – – – – – – – – – – – – – – – – – – – – – – – – – – – – abTests: productRanking: version: 4 groups: [ ratio: default: 1 us: 0.90 me: 0.0 value: “LessPopularityEPC” , ratio: default: 0 us: 0.1 […]

使用twitter gem获取标签

我正在使用twitter gem在ruby中传输推文。 我的代码是 client.filter(:track => topics.join(“,”)) do |object| if(object.is_a?(Twitter::Entities)) puts object.text if object.is_a?(Twitter::Tweet) puts object.created_at puts object.hashtags.to_s puts object.user.name end end 我需要在推文中获取主题标签 ,如果我使用上面的代码我得到输出为 [#”coffee”, :indices=>[20, 27]}>, #”mornings”, :indices=>[28, 37]}>, #”cantstopmoving”, :indices=>[38, 53]}>] 有没有办法获取单个主题标签

Ruby – 哈希属性的Setter方法

通过将笔和纸RPG转换为脚本,我一直在使用Ruby。 现在我有一个角色的统计数据保存在哈希中,我希望能够通过公共方法设置。 我得到了这个: class Character attr_reader :str, :con, :dex, :wis, :int, :level, :mods, :stats def initialize str, con, dex, wis, int, cha, level = 1 @stats = { :str => str, :con => con, :dex => dex, :wis => wis, :int => int, :cha => cha } @mods = {} @level = level @stats.each_pair do […]

Ruby:根据条件从深层嵌套的JSON结构中提取元素

想要从每个具有marketName == ‘Moneyline’市场中提取每个marketID 。 尝试了.map s, .reject s和/或.select s的一些组合,但由于复杂的结构使我感到困惑,所以无法缩小范围。 活动中有许多markets ,也有很多events 。 结构样本(为简洁起见,尝试编辑): {“currencyCode”=>”GBP”, “eventTypes”=>[ {“eventTypeId”=>6423, “eventNodes”=>[ {“eventId”=>28017227, “event”=> {“eventName”=>”Philadelphia @ Seattle” }, “marketNodes”=>[ {“marketId”=>”1.128274650”, “description”=> {“marketName”=>”Moneyline”} }, {“marketId”=>”1.128274625”, “description”=> {“marketName”=>”Winning Margin”} }}}]}, {“eventId”=>28018251, “event”=> {“eventName”=>”Arkansas @ Mississippi State” }, “marketNodes”=>[ {“marketId”=>”1.128299882”, “description”=> {“marketName”=>”Under/Over 60.5pts”} }, {“marketId”=>”1.128299881”, “description”=> {“marketName”=>”Moneyline”} }}}]}, {“eventId”=> etc…. 尝试各种各样的事情,例如, markets = […]

在Ruby哈希中忽略重复键的哪个值?

如果散列有多个出现的相同键指向不同的值,那么Ruby如何确定为该键分配了哪个值? 换一种说法, hash = {keyone: ‘value1’, keytwo: ‘value2’, keyone: ‘value3’} 结果是 warning: duplicated key at line 1 ignored: :keyone 但我怎么知道分配给哪个值:keyone ?