ruby 使用“&:methodname"来自 array.map(&:methodname) 的快捷方式,用于哈希键字符串而不是方法名 [英] ruby using the "&:methodname" shortcut from array.map(&:methodname) for hash key strings rather than methodname

查看:24
本文介绍了ruby 使用“&:methodname"来自 array.map(&:methodname) 的快捷方式,用于哈希键字符串而不是方法名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数 ruby​​ 开发人员都知道如何通过执行以下操作来节省几次击键次数:

Most ruby developers know how to save a few keystrokes by doing something like this:

array.map(&:methodname)

而不是

array.map {|x| x.methodname }

有什么办法可以应用类似的 &:methodname 快捷方式来调用方法"吗?(通过键调用的值)在哈希数组上?

Is there any way I could apply a similar &:methodname shortcut for calling "methods" (values called via keys) on an array of hashes?

在我的例子中,它的 JSON api 结果以 60 个批次的形式返回,作为源自 JSON 的散列数组.我试图这样做:

In my case its with JSON api results being returned in batches of 60 as arrays of hashes originating from JSON. I tried to do this:

array.map(&:"keyname")

但没有成功,抛出一个 NoMethodError 并说哈希没有keyname"方法,我猜这是合理的.我想知道是否有一些灵丹妙药来模拟这个 &:... 哈希的快捷方式,红宝石人已经解决了.

but was not successful, throwing a NoMethodError and saying that there's no 'keyname' method for Hash, which is reasonable I guess. I'm wondering if there is some magic bullet to emulating this &:... shortcut for hashes that the ruby guys have already worked out.

这是实际的 JSON 响应之一,我认为仅限于 5 个结果

Here's one of the actual JSON responses, limited to 5 results I think

[{"geometry":{"location":{"lat":33.723008,"lng":-78.930593}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/bowling-71.png","id":"095e108208879fe989f2b36e81e38bafe0c17574","name":"Waccamaw Bowling Center","opening_hours":{"open_now":true},"photos":[{"height":612,"html_attributions":["u003Ca href="https://plus.google.com/106701205257534874835"u003EAndy Badiu003C/au003E"],"photo_reference":"CoQBeQAAANZ3d4xBep-4jlCnHp0IcCfSr4k6WVnAMDkPNPhlhSTaZ-NICdLaPVxaRW04qK8ko8x5SDScTeImNKWo2qlyF0JYTQpy-elxdZkx58BYQsjCLdtOaBr_yIoKk3NSuBGk7RS9Lf2IWpW0NaBjeAcPTNdKKHoTt_PZ4212cD67wV0jEhBmRx62d9nUyBmPkrv51nadGhTXEhV-hA5frdzGKal4tEd2pYPtjQ","width":816}],"rating":3.1,"reference":"CoQBeAAAAD5d1gIGf4AG427DlKp-AC74Wt5ZCTd6uPU0-31qTnDRW3mBk4Scq00zjaLpbXnqvs1rlMgEFHPVId9nPGU5oaWr2nYRqRodz5LGdVpZqHx1R_PRciZX7xb068t6c7bqw7Fy_Bs9YIi8pCNdfAdHEZb8Pjxy1mpnL1qN_DBz243eEhCrzUmU34savXzZijjlv8TzGhSX3SofvWtuGYOhhPol9atp3aTSyQ","types":["bowling_alley","establishment"],"vicinity":"101 Philip Gray Drive, Myrtle Beach"},{"geometry":{"location":{"lat":33.817622,"lng":-78.692496}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/bowling-71.png","id":"12fed6fbc66c7d9fc693fe37e6834e32ee6e23b9","name":"North Myrtle Beach Bowling Center","photos":[{"height":460,"html_attributions":["u003Ca href="https://plus.google.com/100443525307746960711"u003EHolly Henifordu003C/au003E"],"photo_reference":"CpQBhwAAADY5u5bWSEVGRZr1A7LR0Dpli5EEDRvOt5oWvzQYCOvRk1XO9bMBPdKvQYy_TwSHDcRRcqo6JZoGyLc-nLvlOoG1gX133KkdJ7lKU33RC9hdeN_dzfAUoxIrArZK8Ssu2QQWpXmq10xsJKPIu2bAzpS3n3vKqLdeKZY86GPw7LR0cM-Su3P0MZiY2AkC7uIJIhIQkQVG3s7We7yNPlqRqBkE4xoULTYXh2O6PPuki5_TXG3tN1O3kLg","width":816}],"reference":"CpQBgwAAAB0eZVssHlDn56UET5IqUpZygTzWdLclQ73lU0xzgNG875YyQPhQWGvOI2ciZq_BO6yZYzjMIdGEFVI-jnOhj_Y56Eq0T1MaLAL0dIkCM9qS4QwjAv3-TjwJOMBnN7aARHr3BuGqYLQZb5fkN3veUr2tWLKDZcPT1SHXRLypDutO5msurlQLub5YgmNhOomPdxIQLbIFLMxiqssFIUbO6bEcrRoU0A49j-VRMk4vHsSqxADOq5OPo1o","types":["bowling_alley","establishment"],"vicinity":"1105 Highway 17S, North Myrtle Bch"},{"geometry":{"location":{"lat":33.617718,"lng":-78.976797}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/bowling-71.png","id":"5b2bcc16481ea7519f5e2858d4aa8af1b836f3c8","name":"Surfside Bowling Center","rating":4.4,"reference":"CoQBeQAAACW_aubzCjtG19Nw9Pm9RIXpzpgXD5rFyKziCYy0Uc7X9g1vVz2YWoDUmXwIYA9uUksSArTypwe8lh76TjYYTnNIPiTXc_dpgw94XGaQwC1dO5DuMBPLtWyKgY4Jqa8UlYdHnTOe4FPJnZV7gTsx25Eid2iasa07gruGui6tltYtEhBpBraa9i7GzEE3FUYWxUU0GhSsfU66SAn8tpT3Oa_PRkTUYLXlCQ","types":["bowling_alley","establishment"],"vicinity":"510 U.S. 17 Business, Surfside Beach"},{"geometry":{"location":{"lat":33.580683,"lng":-79.024837}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/bowling-71.png","id":"259104d58ff8e1aebe70db6149c74c736500a069","name":"Frank Theatres","opening_hours":{"open_now":true},"photos":[{"height":400,"html_attributions":[],"photo_reference":"CnRnAAAAq26m02SGow16WCPVIvqioB646tx-kq39g-KPZ6rydR4gUFS2eFF_M3f0-ntrLiq-Iv3jjiQfUdVt_qMxYSAxxOxhfW9JucBG9-Z4ddJH-teNg6p62rHu5Ebbxs-CI40xoK78w3rdLbYA6GsWaOL5gRIQ5UBFXus-gXSy1UrWQ_aiixoUFE0H2eqxYpFyzbGkQtbq4nUHgUk","width":300}],"reference":"CnRvAAAAMDm7HoTk9pG-nKJGvzl9QIw6Qhso44Nh5Q0-xOwM5rykjQ12DJiF_2vZRMaUk9bOtNJwW9ntoBpTgj6bHtWiVCP3OlMu-gd0dgMygMvrIxGTkaR3cTKab0KuHHnoCFxdVB7esI-rK0v585lWS6xTZxIQ0gFfzO3vhZGkIjF6ThDgNBoUAYAMJyjC7f__SBcqOLKbDl3pFSQ","types":["bowling_alley","restaurant","food","movie_theater","establishment"],"vicinity":"10125 Frontage Road #12f, Murrells Inlet"},{"geometry":{"location":{"lat":33.959856,"lng":-78.400399}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/bowling-71.png","id":"39a632e654125e6aa73a526579cae55cc14ee4a2","name":"Brunswick County Bowling Center","reference":"CoQBgAAAAETfTwuwFT34ynta7FJFME2kBVM0XurlJJKj3IQMaOXlWqTGU2P2L4l3gX9XP3XkGSgbxsQiOxIpGiTkrKRVV_-EzvGMNeE8IpsmSUMY7SBY-w7SlC6DGtlX1Py-YES6ZSTrM36my9f6Y2s7bAqamlvXbdtGfTgJpycuI0cx28fvEhB7Y-gtYgRCIInlgHxli4ygGhQlgaJt7d4PylH0KJBnzjRJeIKbNA","types":["bowling_alley","establishment"],"vicinity":"630 Village Rd SW, South Brunswick"},{"geometry":{"location":{"lat":33.974945,"lng":-78.396355}},"icon":"http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png","id":"abaf128a51021a535a17b1af4a523a5fc3af7d10","name":"Planet Fun","photos":[{"height":485,"html_attributions":[],"photo_reference":"CnRtAAAACFYcotbimoFO1Jrqiy7PgLZ3Uuq4t1BLKvrewRdsV0aUZ4ay1jwtCaDQ-q2sh7EweO5At2YA3Mg-tukES9UXItwOn2uLD2sXenKg2UhkBSwa-bvd4RGAH-dg0uACDU_0_kCi7xHkv5WrgSiKmBVN8hIQYBr23I1W9dOQ5F6Rdn1wfRoUVBdp3FEoKA_0e29nzlnUem15BJs","width":1024}],"rating":2.8,"reference":"CnRrAAAARZu90zQ_rp_pRX80BxqorM2GecSJ9kUkRnWNWEDx5K5JXwe9qgPdkzMsq8yOxITR1iPmS6knSWKNO-eWhYxBtJSti7O0ar1kkK2lBUhS5Kn6AxuT8bxlyuKLSuiGN3Zi-YFXI6d4nxhL7uO3K8LAnxIQNJ5TMpFhegaM3saANGrubBoUD7D9lw61ng3SmpK4g1dzf1cIVJ4","types":["amusement_park","establishment"],"vicinity":"349 Whiteville Road Northwest, Shallotte"}]

推荐答案

你可以用 lambda 来做到这一点:

You can do this with a lambda:

extract_keyname = ->(h) { h[:keyname] }
ary_of_hashes.map(&extract_keyname)

如果块的逻辑比简单地从哈希中提取值更复杂,这往往会更有用.此外,将名称附加到您的逻辑部分可以帮助阐明 Enumerable 方法调用链正在尝试做什么.

This tends to be more useful if the block's logic is more complicated than simply extracting a value from a Hash. Also, attaching names to your bits of logic can help clarify what a chain of Enumerable method calls is trying to do.

如果您多次执行此操作,您还可以使用 lambda 返回一个 lambda:

You can also have a lambda which returns a lambda if you're doing this multiple times:

extract = ->(k) { ->(h) { h[k] } }
ary_of_hashes.map(&extract[:keyname])
ary_of_hashes.map(&extract[:other_key])

或 lambda 构建方法:

or a lambda building method:

def extract(k)
  ->(h) { h[k] }
end

ary_of_hashes.map(&extract(:keyname))
ary_of_hashes.map(&extract(:other_key))

这篇关于ruby 使用“&:methodname"来自 array.map(&:methodname) 的快捷方式,用于哈希键字符串而不是方法名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆