When playing around with Ruby you quickly realise that it is full of fun tricks. Here a few that I’ve come across and wanted to share with you.
1. upcase! method. Have you ever come across this method? Do you know how it really works? Are you sure? Ok, let’s check.
Why did the last line return a nil? You will find a trick in the very last lines of the code.
So the method will return a nil unless a string has been modified. Strange, but you should know that! The downcase! and capitalize! methods have the same behaviour.
2. When you call self in irb or a rails console main is returned and it is an instance of Object class but main is undefined which is really interesting.
You should definitely check out this article to learn more about Top-Level.
3. Usually we use a dot “.” to call a method but you can also use double colon “::” to achieve the same results!
Weird, but it really works.
4. Question marks will help you generate a single char in Ruby.
You will get a SyntaxError if you try to generate more than one char.
I’m not sure that you will use it in your real projects... but it's fun!
5. Last but not least, here’s a short way to invoke a lambda with params. Nifty, right?
I hope that this short guide has uncovered something new for you. Ruby is a powerful language and it has countless tricks, so make sure to code and have fun!