# hash

Zombies Hashes Archaisms of Ruby Core

I just love hashes. So much so, I named my blog after them. I also like that the hash sign is used for comments, in Ruby, or the way hash resembles hatch, thus the messy graphic theme and all. But I really like hashes. They are like mini-objects (object hatchlings?) and I tend to use them to store all sorts of information or instead of many conditions with case x; when :a ...; when :b ....

So I was quite surprised to note that in Ruby, either it’s really easy and natural to create a hash (with the super nice {:key => value, ...} syntax) or

Whats Point of Ruby 187

Can you guess how many built-in methods were introduced or modified when Ruby 1.8.5 came out? How about Ruby 1.8.6? Or the most recent 1.8.7?

Ruby Changes
1.8.5Roll over
1.8.6for the
1.8.7answers!
Ruby Changes
1.8.52
1.8.63
1.8.7137

Love & Hate: Array#product

Quick quirky quizz:

1
2
3
4
# What is the output of
p [40, 2].sum
p [2,3,7].product
# ?

Are you expecting a reference to the late Douglas Adams?

Leave My Options Alone

Let me start by asking you a small quiz:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Will there be any difference between the output of:

<% content_tag_for(:tr, Foo.new, :class => "css_class") do %>
...<% end %>
<% content_tag_for(:tr, Bar.new, :class => "css_class") do %>
...<% end %>

# and the output of:

<%- @style = {:class => "css_class"} -%>
<% content_tag_for(:tr, Foo.new, @style) do %>
...<% end %>
<% content_tag_for(:tr, Bar.new, @style) do %>
...<% end %>

# ?

Does Bill Gates Use IE?

Anyone who knows me personally is bound to know that I despise Windows (and Internet Explorer among other Microsoft products). I’m the first to admit that my hatred borders on irrationality. The fact that I’m a complete newbie on Windows probably doesn’t help either. I can count on my fingers the number of hours I spent playing/cursing on windows. That being said, every single time I have to use windows, I always wonder: does Bill Gates uses it? What’s his reaction to all those things that pop-up? Does he browse on Internet Explorer? Does he ever wonder if he just clicked properly and something is happening, or if the computer is just waiting for another click?

Please Write Ruby in Ruby

I’m always surprised when I see bright people writing ruby code without using ruby’s standard lib. Do I need to point out that it’s less readable and more error prone?

I plead all rubyists to re-read the doc for Array, Hash and Enumerable/Enumerator. Refer back to it. Use it. Please!

I was quite amazed to see the following code (written by an ex rails-core programmer, nothing less!). Check out the three methods and ask yourself what they do and how they should be written (mouse-over the code for the answers).

Ruby Doesnt Dig Threads

Either I’m missing something, or threads in both MRI and YARV just plain suck. My test program goes through a 10 MB file of random data, splits it in chunks (either 1K, 10K or 100K each). The results for MRI show the threaded version is much slower (~2x), in YARV performance is similar but usually slower for the threaded version. Mind you, I’m running this on 4 cores! rubinius looks like YARV on a valium overdose (20x slower…). Only in JRuby are things like what I expected, i.e. similar performance or faster for threads, with the difference being noticeable with more processing.

Ruby Threads

I’m pondering a really neat scheme for my upcoming FLV editor. My editor can be thought of as a series of processors acting on tags; the first processor reads them, then others analyse/modify them and the last one writes them. The scheme would need some sort of disconnection in the processing, either with continuations (which appear to be implemented two different ways in ruby 1.8 and 1.9) or threads. Which leads to the questions:

Please Dont Abbreviate

Abbreviation sucks. I’ll add famous people that agree with me here when I get the time. And if I find any!

I dislike the fact that ruby’s Time class as a mon method (c’mon!), but at least it is aliased by month. Now why oh why does ruby’s Time class has a min method and no minute method? Same goes for sec vs second. At least sec isn’t as ambiguous as min.

Can’t stand elsif (wow! one less character! impressive gain…), Enumerable.uniq, …

I’m counting on you to start a Facebook group “don’t abbreviate in ruby”!