Ruby tip. Double association in a ternary operator

Posted: February 9th, 2012 | Author: | Filed under: ruby, Uncategorized | No Comments »

I found myself today in the opportunity to use a double association in a ternary operator. Basically I wanted to assign 2 variables based on a certain condition.

Normal ruby double assignment works easy like this:

Ruby
1
2
3
4
#simple double assignment
a,b = 1,2
a  # 1
b  # 2

Up to here no surprise. But what if I want to evaluate a condition and assigns both the variables??

Ruby
1
2
3
#double assignment . Wrong ways
a,b = true ? (1,2) : (3,4)
a,b = true ? 1,2  : 3,4

Both this attempts ends in a syntax error. The right way:

Ruby
1
2
3
a,b = false ? [1,2] : [3,4]
a  # 3
b  # 4

Easy and clean. Values comes in an array and are correctly assigned.

Of course this works for any number of variables you need to assign

Ruby
1
2
3
4
5
# triple assignment in ternary operator
a,b,c = false ? [1,2,3] : [4,5,6]
a   # 4
b   # 5
c   # 6

Rails migration redo

Posted: January 29th, 2012 | Author: | Filed under: rails, ruby | No Comments »

A very good practice, when writing a rails migration, is to be sure that the down methods works as expected. Once the migration is ready you should make it run and, if anything is fine, you should  rollback it and run it again. In this way you are sure that both the up and down migration works as you planned.

To make it faster you can use the following command

Ruby
1
rake db:migrate:redo

This will run a rollback of the lates migration and will run it again.

You can even say how many steps you want to rollback and the re apply using the steps attributes as you do in a normal roll back command:

Ruby
1
rake db:migrate:redo step=2

Monitor resque workers via upstart

Posted: January 11th, 2012 | Author: | Filed under: resque, ruby, upstart | No Comments »

Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later. Resque works firing up a set number of workers than will coninuously look for job to be processed and will execute them following the rules defined when the workers are launched. You can have a single worker process any queue or having a single worker processing a single queue or even fire up more workers that will look forward to a single queue. Read more about resque.

A very important topic is to be sure that the workers are always available and that any time your application is deployed also the workers are restarted. This is crucial cause the worker load an instance of your application and will then process the jobs against this application instance. If you deploy a new version of your application but you do not restart your workers the jobs will be run on an outdated code version and this is clearly something you do not want! Read the rest of this entry »


Make your ruby classes Comparable and Enumerable

Posted: July 27th, 2011 | Author: | Filed under: ruby | No Comments »

Something very cool in ruby are mixins. As the word says mixin is a technique to mixed a module into a class using the statement “include”.
While including a module is a very large topic I focused on the opportunity to include module defined in the Ruby standard library into your class to take advantages of the methods and abilities that these modules provide.
The Comparable module in ruby define methods like  <   <=   ==   >   >=   between?  .
What if I want to achieve is making my class instances comparable so that I can ask to ruby if class_a > class_b. Read the rest of this entry »


Ruby instance_eval

Posted: July 2nd, 2011 | Author: | Filed under: ruby | Tags: | No Comments »

Working on one of my first ruby gem I have started to learn more about ruby meta programming and ruby core language.
My gem needs a sort of singleton class that needs to be configured when the application starts. I have choosed to use a Module as singleton cause a ruby module cannot be initialized and so it just fits my needs.
I will then include the configured module into the model that will need it.
Read the rest of this entry »


Learning Ruby: gets and chomp

Posted: June 11th, 2011 | Author: | Filed under: ruby | Tags: | No Comments »

Playing with the ruby console I am wondering how I can read some input from the console.

Ruby ships with the method gets that makes easy to read console input. Just try the following code:

print "What is your name? "
$stdout.flush
name = gets
puts 'Hi ' + name + '!!!' + name

# => Hi andrea
# !!!


view raw ruby_gets.rb This Gist brought to you by GitHub.

When the console asks for your name just type it. The ruby program will read the console input and will welcome you. You will note that  a new line is added after that the variable name is  printed out. This is the default behaviour of the gets function. Gets add a new line after any variable that memorize. If this is not desired  you can call the method chomp on the gets result and the newline is suppressed.

Change your code as follows and the new line will not be printed anymore:

print "What is your name? "
$stdout.flush
name = gets.chomp
puts 'Hi ' + name + '!!!' + name

#=> Hi andrea!!!

Something about bundler and gems dependencies

Posted: June 5th, 2011 | Author: | Filed under: bundler, ruby | Tags: | No Comments »

Since some months I have started to programm in Ruby and especially using the Rails framework. I have to say I find Rails so great that I realized, that after some months, that I can be quite productive knowing very little about Ruby itself. This tells you how powerfull can be a dsl written in Ruby and talks about the nice job Rails developers did but is not going to make me shine as a Ruby developer.

I have decided to step back and I am finally going to open a Programming Ruby book seriously. I will go throught my Ruby discovering writing here my tests and anything I will discover in my learning Ruby travel. This will help me to fix some concept in my mind and hopefully someone else will learn from my mistakes.

For the moment I discovered something about bundler I really didn’t know before and I ‘d like to share. I have just ended reading a post of Yehuda Katz about Bundler and Gems dependencies that really opened my eyes.

Something from Yehuda post:

Read the rest of this entry »