简体   繁体   English

从对象数组中提取红宝石哈希元素值

[英]Extract ruby hash element value from an array of objects

I've got the following array 我有以下数组

[#<Attachment id: 73, container_id: 1, container_type: "Project", filename: "Eumna.zip", disk_filename: "140307233750_Eumna.zip", filesize: 235303, content_type: nil, digest: "9a10843635b9e9ad4241c96b90f4d331", downloads: 0, author_id: 1, created_on: "2014-03-07 17:37:50", description: "", disk_directory: "2014/03">, #<Attachment id: 74, container_id: 1, container_type: "Project", filename: "MainApp.cs", disk_filename: "140307233750_MainApp.cs", filesize: 1160, content_type: nil, digest: "6b985033e19c5a88bb5ac4e87ba4c4c2", downloads: 0, author_id: 1, created_on: "2014-03-07 17:37:50", description: "", disk_directory: "2014/03">]

I need to extract the value 73 and 74 from this string which is Attachment id. 我需要从该字符串(附件ID)中提取值73和74。

is there any way to extract this value 有什么办法可以提取这个值

just in case author meant he has an actual String instance: 以防万一作者表示他有一个实际的String实例:

string = '[#<Attachment id: 73, container_id: 1, container_type: "Project", filename: "Eumna.zip", disk_filename: "140307233750_Eumna.zip", filesize: 235303, content_type: nil, digest: "9a10843635b9e9ad4241c96b90f4d331", downloads: 0, author_id: 1, created_on: "2014-03-07 17:37:50", description: "", disk_directory: "2014/03">, #<Attachment id: 74, container_id: 1, container_type: "Project", filename: "MainApp.cs", disk_filename: "140307233750_MainApp.cs", filesize: 1160, content_type: nil, digest: "6b985033e19c5a88bb5ac4e87ba4c4c2", downloads: 0, author_id: 1, created_on: "2014-03-07 17:37:50", description: "", disk_directory: "2014/03">]'

string.scan(/\\sid: (\\d+)/).flatten => ["73", "74"] string.scan(/\\sid: (\\d+)/).flatten => [“ 73”,“ 74”]

Do as below using Array#collect : 使用Array#collect执行以下操作:

array.collect(&:id)

In case it is a string use JSON::parse to get the array back from the string first, then use Array#collect method as below : 如果是字符串,请使用JSON::parse首先从字符串中获取数组,然后使用Array#collect方法,如下所示:

require 'json'
array = JSON.parse(string)
array.collect(&:id)

The elements of the array (I'll call it a ) look like instances of the class Attachment (not strings). 数组的元素(我将其a )看起来像类Attachment实例(不是字符串)。 You can confirm that by executing e.class in IRB, where e is any element a (eg, a.first ). 您可以通过在IRB中执行e.class来确认这一点,其中e是任何元素a (例如a.first )。 My assumption is correct if it returns Attachment . 我的假设是正确的,如果它返回Attachment The following assumes that is the case. 以下假设是这种情况。

@Arup shows how to retrieve the values of the instance variable @id when it has an accessor (for reading): @Arup显示了如何在实例变量@id具有访问器(用于读取)时检索其值:

a.map(&:id)

(aka collect ). (又称collect )。 You can see if @id has an accessor by executing 您可以执行以下命令来查看@id是否具有访问器

e.instance_methods(false)

for any element e of a . 对于任何元件ea This returns an array which contains all the instance methods defined for the class Attachment . 这将返回一个数组,其中包含为Attachment类定义的所有实例方法。 (The argument false causes Ruby's built-in methods to be excluded.) If @id does not have an accessor, you will need to use Object@instance_variable_get : (参数false导致Ruby的内置方法被排除。)如果@id没有访问器,则需要使用Object @ instance_variable_get

a.map { |e| e.instance_variable_get(:@id) }

(You could alternatively write the argument as a string: "@id" ). (您也可以将参数写为字符串: "@id" )。

If 如果

s = '[#<Attachment id: 73, container_id: 1,..]'

in fact a string, but you neglected to enclose it in (single) quotes, then you must execute 实际上是一个字符串,但是您忽略了将其用(单引号)引起来,则必须执行

a = eval(s)

to convert it to an array of instances of Attachment before you can extract the values of :@a . 将其转换为Attachment实例的数组,然后才能提取:@a的值。

Hear that 'click'? 听到“点击”? That was me starting my stop watch. 那是我开始我的秒表。 I want to see how long it will take for a comment to appear that scolds me for suggesting the use of (the much-maligned) eval . 我想看看发表评论要花多长时间,这让我为使用(恶意的) eval提出建议而感到eval

Two suggestions: shorten code to the essentials and avoid the need for readers to scroll horizontally to read it. 有两个建议:将代码简化为基本内容,并避免读者水平滚动以阅读代码。 Here, for example, you could have written this: 例如,在这里,您可能这样写:

a = [#<Attachment id: 73, container_id: 1>, #<Attachment id: 74, container_id: 1>]

All the instance variables I've removed are irrelevant to the question. 我删除的所有实例变量都与问题无关。

If that had been too long to fit on one lines (without scrolling horizontally, write it as: 如果太长而无法容纳一行(不进行水平滚动,则写为:

a = [#<Attachment id: 73, container_id: 1>,
     #<Attachment id: 74, container_id: 1>]

Lastly, being new to SO, have a look at this guide . 最后,作为SO的新手,请看一下本指南

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

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