As a junior developer who started his professional coding journey fairly recently I realized that I only used a limited number of methods and ruby capabilities. Since I got started at Drivy, I have discovered several ruby tricks that helped me make my code more readable and efficient. The dig method The dig method can […]
As a junior developer who started his professional coding journey fairly recently I realized that I only used a limited number of methods and ruby capabilities. Since I got started at Drivy, I have discovered several ruby tricks that helped me make my code more readable and efficient.
dig method can be used on hashes (and Arrays) to, as its name suggests, dig through the potential multiple layers in the object and retrieve the value corresponding to the argument provided to the method.
dig you find yourself able to nicely shorten your code and improve overall readability:
Formerly, if I wanted to access our user’s brother name, I would have written something like that:
But that is considering the fact that I was SURE that each key in fact existed in the hash structure.
For instance, if I executed the following statement, my program would crash because of a non existing key:
Therefore, if I wanted to be safe while navigating in my hash structure, I should have written something like that:
This is way too long and annoying to write… With the
dig method I can simplify this statement a lot:
Everyone knows about
private methods but I’ve found that not many people use
private methods nor
protected ones can be called directly by instances of the class in which the method is defined.
For an instance to access these methods they must be called within a public method defined in the class.
current_activity method could be called by any instance of the Animal class (or any other class inheriting from the Animal class).
However, when a method is
private it cannot be called on
self within a class…
For instance, this would raise an error:
Being able to do so could be especially handy if you wanted to call it on other instances of your class passed as method arguments. For instance it would be useful to be able to do that:
In order for the above code not to break we can make the
protected instead of
private and everything will work just fine:
NB: Please note that the protected method can here be called on instances of the class but only within the class definition body. These methods cannot be directly called on an instance of the class such as:
fido.define_activity which would return an error
NoMethodError: protected method 'define_activity' called for #<Animal:0x007ff688846120>
When I got started I didn’t know about ruby’s
double pipe equals: ||=. The concept is fairly simple and can be very useful in a variety of situations.
||= allows you to perform a variable assignment if and only if the variable is not yet defined or if its value is currently falsey (
This one allows to navigate safely through the layers of objects relations.
Basically, let’s say that we have a company with only one employee and that this employee has a name and an email address:
In this context if I wanted to access Drivy’s employee name I woud probably do the following:
However, this only works in an environment where none of the elements in the chain (except possibly for the last one) can be
Now, let’s imagine a case where the company does not really have any employee. The
drivy object would be instantiated as follows and the above code would raise an error:
In order to prevent this behaviour, ruby has the
& operator (since version 2.3) which behaves a bit like the
try method in rails.
It tries to fetch the object attribute and returns
nil if any element in the chain is
Struct is kind of a shortcut class which acts in a way more “liberal” manner. It allows for faster development when you are not willing to create a new class with an
initialize method and other behavioural methods.
Struct is created as follows:
And the parameters passed to the
new method are instance variables directly accessible from the instance:
In addition to
Struct, ruby provides the ability to use
OpenStruct. The main difference is that
Struct creates a new class whereas
OpenStruct directly creates a new object.
These tools can be very useful to stub simple object or classes while testing since they allow to replicate basic behaviours very quickly without much hassle.
Finally I’d like to share a ruby idiom which allows to nicely shorten some statements and improve readability 🙂
You may have already seen things such as:
When ruby sees the
& followed by a symbol, it calls the
to_proc method on the symbol and passes the proc as a block to the method.
The above examples are equivalent to writing: