Ruby's hash is a Swiss-army knife
akshaykhot.com
Ruby's hash is a Swiss-army knife
1–10 of 81 posts
Re: Ruby's hash is a Swiss-army knife
#2Re: Ruby's hash is a Swiss-army knife
#3Re: Ruby's hash is a Swiss-army knife
#4Ruby’s Hash is probably the handiest data structure I’ve ever encountered. Thanks Matz.
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.pm/elixir/1.15.4/Map.html), with similar properties.
Re: Ruby's hash is a Swiss-army knife
#5Re: Ruby's hash is a Swiss-army knife
#6 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 sugar is that it makes the code more brittle in the face of future changes. In particular, in the example above, if we add a new keyword argument to `foo`, then any call which omitted the curly braces will break, while calls which used them will keep working fine: # added a new keyword arg here
def foo(kwargs = {}, frob: false)
kwargs
end
foo({k: 1}) # still ok: `frob` defaults to false
foo(k: 1) # ArgumentError: no keyword: :k
This is touched on in the blog post describing the extensive changes made to keywords in Ruby 3: https://www.ruby-lang.org/en/news/2019/12/12/separation-of-p...Re: Ruby's hash is a Swiss-army knife
#7Since 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…
Re: Ruby's hash is a Swiss-army knife
#8PHP's associative array is very similar yea?
[1]: https://gobyexample.com/maps [2]: https://docs.python.org/3/tutorial/datastructures.html#dicti... [3]: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...
Re: Ruby's hash is a Swiss-army knife
#9PHP's associative array is very similar yea?
Yes, hashes in Ruby, associative arrays in PHP, maps in Go[1], dictionaries in Python[2] and C#[3] represent the same concept, a collection of key-value pairs. [1]: https://gobyexample.com/maps [2]: https://docs.python.org/3/tutorial/datastructures.html#dicti... [3]: https://learn.microsoft.com/en-us/dotnet/api/system.collecti...
I've noticed a lot of novice-level posts on here lately. Although I admit the post was very well written.
Re: Ruby's hash is a Swiss-army knife
#10PHP's associative array is very similar yea?
It's a doubly linked list, array, hashmap all in one, with some weird magic when handling numeric indices.
There's often the time when using another language that I made a bad choice by picking either an array or hash or list while later I need one of the other properties PHP would give me by default. The extra cost for most uses is neglectible.