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 »