Tag: Ruby

Ruby: Hash default value – be cautious when you use it

Few weeks ago a friend asked me, why this Ruby example acts so strangely:

hash = Hash.new([])
puts hash #=> {}
hash['foo'] << 1 << 2 << 3
puts hash['foo'] #=> [1, 2, 3]
puts hash #=> {}
hash.delete('foo') #=> nil
puts hash['foo'] #=> [1, 2, 3]

You may ask, why a hash that clearly has some values in a 'foo' key is empty when we print it? Furthermore, why once we delete this key, the values are still present?

Everything goes down to the ::new method and the way Hash deals with the default value. Most of the programmers that I know were assuming, that when they pass an empty array to a hash initializer, each key without a value will be initialized with an empty array:

hash = Hash.new([])
puts hash #=> {}
puts hash['foo'] #=> []
puts hash['bar'] #=> []
puts hash #=> { 'foo' => [], 'bar' => [] }

However Ruby does not work like that. Under the hood, when ::new method is invoked, Ruby stores the default value inside newly created hash instance and uses it each time we request a value of a key that is not present. Internal implementation of this fetching method looks similar to this (in terms of how it works):

def fetch(key)
  instance_variable_get("@_#{key}") || @_defaults
end

It means that when you provide a default object, it will always be one and the same object. Ok. But it does not explain why when we print this array, it appears to be empty! Well... it does. What we were doing up until now in our examples was modifying the internal structure of a default array. This is the reason why Ruby thinks, that there's nothing new in the array. In fact, there is nothing new and from Ruby perspective, the array is empty. We were reusing the default value all the times.

If you decide to use a Hash default value that is other than nil and you don't understand this concept, you might get into trouble. That's why it is a really good practice to initialize non-nil hashes with a block:

hash = Hash.new { |hash, key| hash[key] = [] }
puts hash #=> {}
hash['foo'] << 1 << 2 << 3
puts hash['foo'] #=> [1, 2, 3]
puts hash #=> { 'foo' => [1, 2, 3] }
hash.delete('foo') #=> [1, 2, 3]
puts hash['foo'] #=> []
puts hash #=> { 'foo' => [] }

Grill.rb 2016 – Poland’s first outdoor conference review

grillrbIt's about time to summarize some of the conferences that I've recently attended to. This post will be about Grill.rb that organizers claim to be "Poland's first outdoor conference". I was quite skeptical about that claim, but after I couldn't find any other Poland's outdoor conferences, I must admit that :).

And as many "first of a kind" conferences, this one was quite intimate. 60 attendees, 6 talks, open space, lightning talks, 35 kg of meet and a huge amount of great attitude.

Location

Day 1

On Saturday, we had a great weather. More than 30 Celsius degrees and gentle breeze. Anders Hill was definitely a great choice - peaceful and quiet place, with some trees giving us a bit of shadow to hide from the sun.

20160625_191809

Day 2

Organizers on Facebook called it the "Dungeon.rb" and I couldn't agree more. The weather was not too kind for us the next day and we had to move to an indoor location. But even then, you could still feel the outdoor, "non commercial" vibe.

13495094_1705525553034950_1080983640665734947_n

Speakers/presentations/talks

One could say, that 6 presentations for 2 days is not enough. And it would be true, if it wasn't for the Poland vs Switzerland game, barbecue time, open space and some other activities that were presented. If you ask me, I would say, that it was well-balanced. Especially, that it was suppose to be an outdoor, chill conference, not the "most professional, top knowledge !!!1111oneone" IT event ever. It's not that it didn't have pro talks ;), it's more that its main goal was to provide a solid technical base in an open and friendly environment, where one could easily talk with one another. Think of it as a constant 2 day afterparty ;)

I've enjoyed all the talks and what strikes me is that more and more speakers seem to notice and speak about software quality (even when their talks are not directly related to it). I believe that it is a great trend. Achieving (at least) a decent quality is really important and unfortunately especially unexperienced programmers don't see huge advantages that they gain reaching a certain point. The most interesting talk for me, was Krzysztof's Tarnowski "Functional programming in Ruby". It showed a different approach that aims to solve some of the Ruby programmers issues with an Erlang/Elixir approach.

If you're interested in more details about the talks, ping organizers, hopefully they will publish presentations at some point, because it is definitely worth reviewing them, if you haven't attended.

Open space - freedom of choice

What if you could propose a subject on which all the attendees can discuss? What if many people would do that, and you could pick about what you want to talk with others? This is exactly what happened during the 1st day. 3 groups, 3 topics, 3 rounds. Open Source gems vs Law and licences, ReactJS + Rails, Code Quality, DDD and many more. The only downside is that you cannot attend more than 1 at the same time. The more people, the more points of view you can see and understand. Great idea and great execution.

Food/grill

See for yourself :-) Yummy!

13497982_1705825163004989_5415868372842741787_o

Organization

It is hard to describe something that is good. There were only minor things that I could whine about, but it didn't change my  perception of the whole conference so I consider  them irrelevant. Good job, guys!

Summary

Was it worth attending? Yes! Definitely yes! Was it good? Even more. Does the outdoor approach fits into IT conferences? I would say, that we need way more people with that approach (and attitude) than indoor, strict meetings. IT community is a community of people with open minds, and I feel that we integrate and feel way better in this type of environment, than inside of tiny, dark rooms with only projectors light.

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑