简体   繁体   English

哈希,数组和ruby'.first'方法

[英]Hashes, arrays, and the ruby '.first' method

I am working on an Rails API that receives POSTed json input and processes things accordingly. 我正在使用Rails API,该API接收POST的json输入并相应地处理事情。 The current element involves prizes and prize codes, and I have a question about hashes, arrays, and the ruby .first method. 当前元素涉及奖品和奖品代码,我对哈希,数组和ruby .first方法有疑问。

Disclaimer: I am receiving json from a legacy PHP application and as of right now I don't have a way to edit the output from that application. 免责声明:我正在从旧版PHP应用程序接收json,到目前为止,我还无法编辑该应用程序的输出。 I'm getting it as is and am dealing with it the best I can. 我得到了它,并尽我所能处理它。

The prize codes are coming in as a nested element within a larger object, but we are just going to focus on the codes for now. 奖品代码以嵌套元素的形式出现在较大的对象中,但现在我们将仅关注代码。 Here's what I'm getting: 这就是我得到的:

"codes" => {
  "0"=>{"Code"=>"1582566"},
  "1"=>{"Code"=>"2153094"},
  "2"=>{"Code"=>"3968052"},
  "3"=>{"Code"=>"4702730"},
  "4"=>{"Code"=>"5582567"},
  "5"=>{"Code"=>"6153097"},
  "6"=>{"Code"=>"7968052"},
  "7"=>{"Code"=>"8702730"},
  "8"=>{"Code"=>"9582567"},
  "9"=>{"Code"=>"1053097"}
}

Starting there, everything gets pulled into a block, and then I am dealing with each code individually. 从那里开始,一切都陷入一个块,然后我分别处理每个代码。

I'm going to start with my final solution and we will work our way backwards. 我将从我的最终解决方案开始,我们将朝后退。 In order to get a single code, just the number, out of each code, this is my solution: 为了获得单个代码,只是每个代码中的数字,这是我的解决方案:

code[1].first.second.to_i returns 1582566 code[1].first.second.to_i返回1582566

Now let's rewind, and start at the beginning. 现在让我们倒带,从头开始。

code returns "0"=>{"Code"=>"1582566"} code返回"0"=>{"Code"=>"1582566"}

That makes sense. 那讲得通。 Now I want to skip past that first level "0" . 现在,我想跳过第一级"0"

code[1] returns {"Code"=>"1582566"} code[1]返回{"Code"=>"1582566"}

All of that makes sense so far, but this is where things get weird. 到目前为止,所有这些都说得通,但这就是事情变得怪异的地方。 From that point I want to grab the 2nd item, maybe you might call it the value in this hashy key value pair. 从那时起,我想获取第二个项目,也许您可​​以将其称为此哈希键值对中的值。 Unfortunately, the ruby .second doesn't work on a hash, I get an undefined method error, but .first does for some reason. 不幸的是,ruby .second不能在哈希上工作,我收到undefined method错误,但是.first出于某种原因。 And the output is confusing to me. 输出使我感到困惑。

code[1].first returns ["Code", "4582566"] code[1].first ["Code", "4582566"]返回["Code", "4582566"]

And now all of a sudden that hash is an array. 现在突然之间,哈希是一个数组。 Why does the ruby .first array method turn a key value hash into an array? ruby .first array方法为什么将键值哈希转换为数组?

I did a little sleuthing and discovered that :first is a method listed in the output from code[1].methods , but I can't seem to find any documentation out there about what it does. 我进行了一些侦查,发现:firstcode[1].methods的输出中列出的方法,但是我似乎找不到关于它的作用的任何文档。

code[1].class returns Hash code[1].class返回Hash

But then why do I get a completely different set of available methods returned when I run code[1].methods vs Hash.methods ? 但是,为什么当我运行code[1].methods vs Hash.methods时,为什么会得到一组完全不同的可用方法呢? The :first method is listed in the output from code[1].methods but NOT in the output from Hash.methods and it is not on the list of available methods here either: :first方法是在从输出中列出code[1].methods不是在从输出Hash.methods并且不是可用的方法的列表上任何这里:

http://ruby-doc.org/core-2.1.5/Hash.html http://ruby-doc.org/core-2.1.5/Hash.html

If they are both Hashes, why are their different methods available? 如果它们都是哈希,为什么可以使用它们的不同方法? Does it have something to do with class methods vs instance methods? 它与类方法和实例方法有关吗?

I'm still confused about what exactly the .first method does to a hash. 我仍然对.first方法对哈希执行的操作感到困惑。

In the end I realized a better / cleaner way to get what is code[1]['Code'].to_i , but I'm still curious about what is going on under the hood. 最后,我意识到了一种更好/更干净的方法来获取什么是code[1]['Code'].to_i ,但是我仍然对引擎盖下发生的事情感到好奇。

In Ruby, a Hash is an ordered collection of key/value pairs . 在Ruby中, Hash是键/值对的有序集合 We can say they are "ordered" because: 我们可以说它们是“有序的”,因为:

Hashes enumerate their values in the order that the corresponding keys were inserted. 哈希值按插入相应键的顺序枚举其值。

Because of this ordered enumeration, the Enumberable#first method returns the first key/value pair that was added to the hash as an Array of two items [key, value] . 由于此有序的枚举,所以Enumberable#first方法返回作为两个项[key, value]的数组添加到哈希中的第一个键/值对。

{}.first # => nil
{1 => 2}.first # => [1, 2]
{:a => true, :b => false}.first # => [:a, true]
h = Hash.new
h[:m] = "n"
h[:o] = "p"
h.first # => [:m, "n"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM