Tag: 哈希

Ruby合并哈希值

我编写了一个API来组织我的日常用户数据。 原始格式就像 “dau”: { “2017-05-02”: 1, “2017-05-04”: 2, “2017-05-05”: 2, } “new_user”: { “2017-05-02”: 1, “2017-05-04”: 0, “2017-05-07”: 0, } 很难在HTML表格中逐行显示。 因此,我希望格式成为这个格式。 但是,我不知道如何处理。 info: { “2017-05-02”: { dau: 1, new_user: 1 }, “2017-05-04”: { dau: 2, new_user: 0 }, “2017-05-05”: { dau: 2 }, “2017-05-07”: { new_user: 0 } }

DFS遍历深度嵌套的哈希

我已经看到这个问题在Ruby中递归遍历哈希 但是我仍然无法做到这一点。 任何人都可以请帮助(也解释)。 输入哈希: {“.” => {“foo” => {“hello” => {}, “world” => {}}, “bar” => {}}} 我只需要遍历它而不是搜索任何东西

Ruby on Rails 2在Hash中搜索字符串

我需要帮助… 我有这样的哈希: @ingredients = Hash.new @ingredients[1] = “Biscottes Mini(recondo)” @ingredients[2] = “Abadejo” @ingredients[3] = “Acelga” @ingredients[4] = “Agua de Coco” @ingredients[5] = “Ajo” @ingredients[6] = “Almidón de Arroz” @ingredients[7] = “Anillos Con Avena Integral cheerios (nestle)” @ingredients[8] = “Apio” 当我写“scotte”时,我需要搜索那个哈希以找到“Biscottes Mini(recondo)” 一些帮助? THK!

在Ruby中使用注入?

我有一个名为Hsh的类,它基本上模拟了一个哈希。 它有一个Couple对象数组(其中包含名为one和two的字段,一个是另一个int,另一个是int的字符串名称)。 我应该能够接受以下电话: h = x.inject({}) {|a, b| a[b.one] = b.two; a} 其中x是Hsh对象。 我不知道如何在Hsh中实现inject方法? 比如,我会写什么: def inject ???? ?? ?? end 它应该做的就是创建一个哈希映射。

按嵌套值排序哈希值

我的哈希看起来如下。 我希望输出是相同的哈希forms,但根据价格排列哈希。 { 1=>{ “name”=>”Mark”, “date”=>”27/08/2015”, “bed”=>”3”, “furnish”=>”Fully”, “size”=>”10”, “price”=>790000 }, 2=>{ “name”=>”Mark”, “date”=>”27/08/2015”, “bed”=>”3”, “furnish”=>”Fully”, “size”=>”10”, “price”=>720000 }, 3=>{ “name”=>”Mark”, “date”=>”27/08/2015”, “bed”=>”3”, “furnish”=>”Fully”, “size”=>”10”, “price”=>750000 }, 4=>{ “name”=>”Mark”, “date”=>”27/08/2015”, “bed”=>”3”, “furnish”=>”Fully”, “size”=>”10”, “price”=>710000 } } 我读过如何按数值对Ruby Hash进行排序? 但它只是一个嵌套的哈希。 对我如何实现这一目标完全无能为力。 如果你们中的任何人愿意帮助我,将不胜感激。

如何从嵌套哈希获得密钥或密钥存在的价值?

如何从嵌套哈希中获取密钥的值或密钥的存在? 例如: a = { “a”=> { “b” => 1, “c” => { “d” => 2, “e” => { “f” => 3 } }}, “g” => 4} 有没有直接的方法来获得“f”的值? 或者是否有一种方法可以知道嵌套哈希中是否存在密钥?

Ruby OOP tic-tac-toe hash / conditional / itiration

试图在Ruby中创建一个经典的tic tac toe OOP但是我的game_results()方法遇到了麻烦。 我意识到这不是很完整,需要更多的function,但是现在我只是试图用我从用户输入的对象填充我的电路板并输出填充板和赢家。 当我在我的主板上看到我们有一个胜利者时,我呼吁使用game_results()方法并且它给了我正确的赢家,但是每当发生平局游戏时,我都会收到错误。 有人对操作员的错误有什么想法或解决方案吗? 顺便说一下,我知道这一切都很混乱,但我是初学者。 #require “pry” class Game attr_reader :turn def initialize @turn = 1 @board = { a1: ” “, a2: ” “, a3: ” “, b1: ” “, b2: ” “, b3: ” “, c1: ” “, c2: ” “, c3: ” ” } end # def start_game # […]

当使用ruby定义一系列键时,将所有Key值作为组合字符串获取

我有以下代码 class MyClass def my_method(first,last) #this should take 2 numbers as arguments and should return the keys value(s) as a combined string end private def lines_of_code { 1 => “This is first”, 2 => “This is second”, 3 => “This is third”, 4 => “This is fourth”, 5 => “This is fifth” } end m […]

删除字符串中的斜杠

我在获得令牌后在facebook中请求用户详细信息。 我收到了以下回复。 回复如下 “{\”id\”:\”xxxxx\”,\”name\”:\”abcd\”,\”first_name\”:\”ab\”,\”last_name\”:\”cd\”, \”link\”:\”http:\\/\\/www.facebook.com\\/profile.php?id=xxxxx\”,\”quotes\”:\”Life is a difficult game. You can win it only by retaining your birthright to be a person.\”,\”gender\”:\”female\”,\”timezone\”:5.5,\”locale\”:\”en_US\”,\”verified\”:true,\”updated_time\”:\”2012-02-22T12:59:39+0000\”}” 我打印出显示为”String”的响应类。 我想把它改成哈希。 (我想删除上面的\)。 我试过但没有得到正确的格式。

Ruby:使用Hash跟踪股票交易

我是Ruby的新手,我正在尝试编写一个“简单”系统,因此我可以跟踪股票交易。 计算平均价格,将来我会尝试获得股息信息。 到目前为止,我的代码看起来像这样(请随意提出更好的方法来执行我的代码,正如我所说,我是新的)。 require ‘money’ require ‘money/bank/google_currency’ require ‘monetize’ require ‘date’ require ‘ystock’ require ‘ostruct’ # (optional) # set the seconds after than the current rates are automatically expired # by default, they never expire Money::Bank::GoogleCurrency.ttl_in_seconds = 86400 I18n.enforce_available_locales = false #erro no formatting… # set default bank to instance of GoogleCurrency Money.default_bank = Money::Bank::GoogleCurrency.new […]