By Jesus Castello
Ruby is an amazing language with a lot of interesting details that you may not have seen before...
…in this post I compiled some of those details for your own enjoyment in a nice-looking list :)
If you have some data that you want to embed into your program you may want to use a “heredoc”.
This will give you a string. But you may want to do some post-processing, like splitting this string into an array of strings.
Ruby lets you do this:
Ruby 2.3 introduced the "squiggly heredoc"
<<~. This will remove all the extra spaces introduced by indentation, which is a common problem when using heredocs for text.
Apparently this is a thing…
Pretty simple, but could be useful in some situations I guess.
This works because
 is just a method & it keeps returning the first character, which is also a string.
Want to get rid of some local variables? You will love this trick!
This has the same effect as if we did this:
But it saves you one line of code 🙂
When you use a regular expression with capture groups it will set the global variable
$1 to the first group,
$2 for the second group, etc.
The thing about these is that they don't behave like normal variables. They are ‘method-local’ & ‘thread-local’, as described by the documentation.
Also they can’t be directly assigned to like regular global variables.
Not sure if there are any practical uses for this one…
Let me know in the comments what you think 🙂
RbConfig is a module which is not documented & it contains some info about your Ruby installation.
There is some useful info under
RbConfig::CONFIG (like compile flags, ruby version & operating system).
You can put as many spaces as you want between a method call & the receiver of that call.
Yes, this is valid Ruby syntax 🙂
You can have an infinite amount of nested constants like this:
The reason this works is that all top-level constants (defined outside any class) are contained in the
Object class & every class inherits from
Object by default.
Object.constants to see what I mean.
You can chain the shovel
<< operator multiple times:
Two keywords that you don’t see very often are
END. I believe these come from the Perl / Unix world, where it’s common to write short scripts for processing output from other programs.
Let’s see an example of how this works:
This code will print
"Program starting..." before it prints
123. It could be useful if you are writing the kind of short scripts that this is meant for, but probably not very useful in web applications.
Reader Ronald sent me some interesting uses for this trick. Here is what he said:
"It is very useful, for example for fiddling with the RUBYLIB path for the 'require' statements, because it is guaranteed to be executed before all the 'require'. I also use it to set $VERBOSE to true, or set some environment variables, etc."
I don’t even know why this is a thing, but I would advice to stay away from it because it can be confusing & most people are not familiar with this feature
But it could be useful to know in case you find this in other people’s code.
This is the syntax:
The idea is that once the first condition is true an invisible “switch” will turn on & everything from there will evaluate as true until the 2nd condition is true.
This prints all the numbers from 3 to 15, but if you skip 15 it will keep printing.
Another keyword that you don’t see very often is
redo, this allows you to repeat the same iteration inside a loop…
…but unless you use
break you will have an infinite loop. So I think you should not use this feature.
You learned about a few cool Ruby tricks & tips. If you want more see my other post here.
Don’t forget to share this post so more people can see it!