Live data from Hacker News

Ruby's hash is a Swiss-army knife

akshaykhot.com

21–30 of 81 posts

Re: Ruby's hash is a Swiss-army knife

#21
post #14

In my opinion Lua's equivalent tables or more of a Swiss army knife. It's been a while since I've used them but afair they combine hash/dict, array and objects in one data structure. They're hashes as usual. With a special constructor or int keys they turn into arrays, last I looked the Lua interpreter even optimizes contiguous ranges of keys as arrays. And by adding metatables and metamethods they also emulate JS-st…

My feeling with Lua is that it's lacking arrays due to minimalism, as in "we don't need arrays because tables will do". Probably the same reason why there is no "continue" in a for-loop.

In comparison, Ruby has arrays and hashes, and gives me the feeling "yes these do exactly what you expected, but wait there is more!" In that sense it does feel more like a Swiss army knife.

Re: Ruby's hash is a Swiss-army knife

#22

Since Ruby 3, the automatic coercion of keywords to a hash—the second example underneath "Passing Hash to Functions" in this post—is considered a legacy style and is generally frowned upon in new code. That is to say, code like the second call to `foo` here: def foo(kwargs = {}) kwargs end foo({k: 1}) # ok: passing hash argument foo(k: 1) # ok: keywords coerced to hash One of the strongest arguments for avoiding this…

This might be me having huffed too many types lately, but I feel like I would want that to break.

Re: Ruby's hash is a Swiss-army knife

#24

My favorite little-known fact about Ruby hashes is that they respond to `to_proc` and can be used as procs. For example, you can do this: a = { 1 => 'a', 2 => 'b' } [1, 2, 3].map(&a) #=> ['a', 'b', nil]

I don't quite understand how this code works. Where does the `nil` come from? What operation are we performing on 3 that causes it to return `nil`?

Re: Ruby's hash is a Swiss-army knife

#25

My favorite little-known fact about Ruby hashes is that they respond to `to_proc` and can be used as procs. For example, you can do this: a = { 1 => 'a', 2 => 'b' } [1, 2, 3].map(&a) #=> ['a', 'b', nil]

I don't quite understand how this code works. Where does the `nil` come from? What operation are we performing on 3 that causes it to return `nil`?

1, 2, and 3 are being passed as lookups to the a hash. 3 is undefined on the hash, hence nil.

Re: Ruby's hash is a Swiss-army knife

#26
I've been in Python/Django for about a year now and I really miss Ruby's hash vs the dict.

`.dig(:key, :key, :etc)` is so nice to find deeply nested data without blowing up.

One thing I don't miss is knowing whether a hash's keys are strings vs symbols. While it's easily solvable, I've definitely lost time only to smack myself that I need to use a str but was always feeding a sym and swore that this should be a sym based hash.

Re: Ruby's hash is a Swiss-army knife

#27

Earlier quoted context omitted.

I don't quite understand how this code works. Where does the `nil` come from? What operation are we performing on 3 that causes it to return `nil`?

1, 2, and 3 are being passed as lookups to the a hash. 3 is undefined on the hash, hence nil.

Ah, that makes sense. Thank you!

Re: Ruby's hash is a Swiss-army knife

#28

I've been in Python/Django for about a year now and I really miss Ruby's hash vs the dict. `.dig(:key, :key, :etc)` is so nice to find deeply nested data without blowing up. One thing I don't miss is knowing whether a hash's keys are strings vs symbols. While it's easily solvable, I've definitely lost time only to smack myself that I need to use a str but was always feeding a sym and swore that this should be a sym b…

I don't know ruby and if it's the same, but if you're not using a library like funcy or toolz for a nested get helper, you can do `dict.get('key', {}).get('key2', {}).get('key3')`. Not the prettiest, but can do in a pinch.

Re: Ruby's hash is a Swiss-army knife

#29

Ruby’s Hash is probably the handiest data structure I’ve ever encountered. Thanks Matz.

Definitely! As a matter of fact, this is the default data structure I use when writing Ruby ETL code (e.g. https://github.com/thbar/kiba/wiki ). Methods like "except" ( https://docs.ruby-lang.org/en/3.2/Hash.html#method-i-except ) or "fetch" (raising an error on missing key) are very convenient to write defensive data processing code! Similarly, in Elixir, I use Maps a lot for the same type of jobs ( https://hexdocs.…

Kiba looks like a really cool framework, thanks for posting it!

Re: Ruby's hash is a Swiss-army knife

#30
post #18

Earlier quoted context omitted.

Yes. The OP's post is basically "baby's first API." The are a very handy API but relying on them at scale is much less efficient than structs or arrays. This post doesn't even go into the different performance characters of these APIs, which can be a major foot gun. I've noticed a lot of novice-level posts on here lately. Although I admit the post was very well written.

I find it funny that he said he didn't know what to pick between 5 types of IDictionary in C#. * Hashtable * SortedList * SortedList * Dictionary * ConcurrentDictionary I don't even know C# (just Java), yet I know that you'd probably want to use Dictionary most of the time.

To be honest, I wrote that just to add a little drama ;)
Post reply on HN