Technology
Mobile
Internet & Software
Events
More
Tech News

Ruby Tricks That Will Make Your Code Beautiful

What’s the most common problem developers face? Re-inventing the wheel! Sometimes, a developer is unaware of built-in features a particular language or framework offers. And what do RoR developers do in such a case? Right, he/she is starting to look for a solution. Usually such a solution is a lot of great but still redundant code. Forgetting or being unaware of some shortcuts isn’t a big deal. Let’s recap some of Ruby tricks that will make your life easier and code just awesome.

Regular expressions: how to get matches?

Regexp. No sweat! It looks scary but once you get to know how to deal with them, you’ll love regular expressions.

Usually, a match method is used. Here’s a simple example of how one does it in a traditional way. No rocket science, huh?

email = “John Smith <jsmith@example.com>”

email.match(/<(.*?)>/)[1]            # => “jsmith@example.com”

However, there’s a simpler and cleaner way to do it with String#[]:

x = ‘this is a test’

x[/[aeiou].+?[aeiou]/]    # => ‘is i’

At first it looks like a bit of magic, but it isn’t in fact. Do you love regular expressions now?

Array#join and how to use it efficiently

We all know that  Array#* multiplies the array size when a number is passed to it. Quite simple. in fact.

[5, 6, 7] * 3 == [5, 6, 7, 5, 6, 7, 5, 6, 7]

However, we tend to forget that it can perform a join, provided that you pass a String as an argument. Whoa!

%w{hello world} * “, ”                 # => “hello, world”

h = { :name => “John”, :age => 30 }

h.map { |i| i * “=” } * “n”              # => “age=30nname=John”

Decimal formatting

Processing data can be a daunting task, especially if you need it in a special format. Here’s a simple example how you can format a decimal number:

amount = 7.6

“%.2f” % amount                       # => “7.60”

Non-strings as hash keys

Well, yeah, it’s not common to use non-strings as hash keys. But for some reason you may need it. Is it possible? Of course. it is! Moreover, it’s often very convenient:

it = is = { true => ‘Yes’, false => ‘No’ }

it[20 == 60]                       # => “No”

is[20 > 6]                           # => “Yes”

Grouping operations for single liners

The below trick will make your code more elegant by removing unnecessary statements (if and unless):

run = []

%w{hello x moto}.each do |moto|

 run << word and puts “Added to queue” unless word.length <  2

end

puts run.inspect

# Output:

#   Added to queue

#   Added to queue

#   [“hello”, “moto”]

Special conditions: execute some code only if it is implicitly run:

if __FOLDER__ == $0

 # TODO.. e.g. run tests

end

This is a very handy condition if you have many tests and want them to run only if your code is being implicitly invoked.

 


nielsbosch

About the author nielsbosch

Hello, my name is Niels Bosch and i am the founder of AmongTech. Currently living in the South of Spain. My main interests are web development, playing video games once in a while, Apple, Marketing and SEO. I am currently studying Business administration, Ne Read More

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *