Map and reduce methods in Ruby

There are two methods in Ruby's Enumerable module that often people coming
from an object oriented background find confusing but are indeed very
useful: .map and .reduce.

They both are constructions taken from functional programming so they
both take a block as a parameter.

There are also .collect and .inject aliases for them. They are there
only to use whatever one the domain you are working thinks is best.

.map / .collect

Map takes a function and returns the array that has in each position
the result of applying that function to the corresponding element of the
original array.

map.png

This is a highly practical method. Imagine that, for example, you have
an array of people and you want to get an array of the names. It is
as easy as:

.reduce / .inject

Reduce is similar to map but instead of receiving a function that takes
only one parameter it receives a function that receives an accumulator and
the value. Instead of returning an array it just returns the accumulator.
We can say that this method reduces the array to a single value.

reduce.png

Imagine now that with our array of people we want to get the total number
of letters that all the names have. We can do:

Differences between symbols and strings in Ruby

You can think of a symbol as an immutable string. Since it can't change each time you ask for a symbol using :name syntax the ruby interpreter will return the same instance of the symbol.

First al all let's prove that.

As you can see each time we write: "Yay", we are getting a different
object. Although they contain the same characters and they are equal
they are different objects

But in the case of symbols

Also, strings can be changed

But symbols can't

This causes symbols to be often used as keys to hashes or to
be used as a substitute for enumerated types.

Bonus points:

You can freeze a string and cause it to be immutable with freeze method

You can also define 'constants'. Ruby compiler will consider any variable that is declared uppercased as a constant. It will raise a warning in case you change it. Anyway it will allow you to do so:

Software craftsmanship Madrid

For some time since my two kids were born I stopped attending to user groups, conferences and events in general. These days I decided to make a small effort and return again to those activities. To be completely hones, I must say I missed it a lot. Maybe there is nothing you can learn there that you can do by yourself diving through the internet or reading some books but, in my humble opinion, it is not the same thing.

So I'm spending some effort in a small meetup group in Madrid. We prepare some small workshops from time to time and it really feels good to connect to other professionals.

There is always a time in all software developer's life when you see going to this kind of events as a waste of time since you do a lot of socialization and not that much learning. Anyway I'm starting to think that this is not that bad. Developing software is an activity that has an important social component. Cultivating this part is also important. We build sofware for humans so we need to communicate and interact with people constantly, not only with machines.

Go outside your cubicle. Speak with people. Enjoy having discussions.