Posted: February 9th, 2012 | Author: Andrea | 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:
|
|
#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??
|
|
#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:
|
|
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
|
|
# triple assignment in ternary operator
a,b,c = false ? [1,2,3] : [4,5,6]
a # 4
b # 5
c # 6 |
Posted: January 29th, 2012 | Author: Andrea | 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
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:
|
|
rake db:migrate:redo step=2 |
Posted: January 11th, 2012 | Author: Andrea | 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 »
Posted: November 3rd, 2011 | Author: Andrea | Filed under: git | No Comments »
I am now using git since more than a year as my primary scm. I tend to use it also against svn repo, I can so focusing on git becoming always more comfortable using it.
To be honest up to now my git use has always been quite basic, while projects I was working on never needed more that a master/develop/topic branches workflow. Recently I faced a more complex issue related to the necessary support for proper support branch that was going to diverge for a long time from the develop branch.
We have a develop branch where is done the job for new version 2.0 while we still have a stable branch where we support version 1.0. The issue comes from the fact that version 1.0 is not going to receive only simple hotfixes but will also be improved with new feature that need to be ported to version 2.0..
For the first month or so we still were able to merge the 2 branches but not they have diverged too much. Merge is not an option and so we had to find a new strategy.
Our goal was to carry all the changes made on a topic branch borned by the 2.0 branch into the same 2.0 and also to the 1.0 without merging 2.0 into 1.0. This can be solved cherry-picking any single commit from the topic branch to the 2.0 and then merge topic into 1.0. While this works fine we faced situations where the topic branch were actually getting too long and cherry picking many commits can be buggy and tricky.
Read the rest of this entry »
Posted: July 27th, 2011 | Author: Andrea | 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 »
Posted: July 2nd, 2011 | Author: Andrea | Filed under: ruby | Tags: ruby | 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 »
Posted: June 11th, 2011 | Author: Andrea | Filed under: ruby | Tags: ruby | 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
# !!!
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!!!
Posted: June 5th, 2011 | Author: Andrea | Filed under: bundler, ruby | Tags: ruby | 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 »
Posted: March 2nd, 2011 | Author: Andrea | Filed under: Uncategorized | Tags: Uncategorized | No Comments »
A couple of weeks ago I was reading a post from Raymond Camden that remembered me I completely forgot to add support to addevent() in Railo cfmap implementation. The blog post was quite interesting and showed a way to add streetview to a google map, generated by cfmap, using the Coldfusion.Map.addEvent function. Read the post here.
Of course code was not running in Railo due to the lack of the method in the Railo js library.
I made a patch and support was added in Railo 3.2.1.005/3.3.0.005.
So following the Ray example (all credits to Ray for the following code) you can run the following code in Railo and adding a streeview to your cfmap using the addEvent() function.
<script>
function init() {
ColdFusion.Map.addEvent("mainMap","click",function(overlay,overlaylnglt) {
address = arguments[arguments.length-1];
var loc = new GLatLng(address.lat(),address.lng());
panoramaOptions = { latlng:loc };
var myPano = new GStreetviewPanorama(document.getElementById("streetDiv"), panoramaOptions);
});
}
</script>
<cfmap centeraddress="Milano Italy" zoomlevel="15" name="mainMap"/>
<div id="streetDiv" style="width:500px;height:500px"></div>
<cfset ajaxOnLoad("init")>
Please note that in Railo js library are available both the Railo and Coldfusion namespaces so Railo.Map.addEvent() is equivalent as running Coldfusion.Map.addEvent().
Have fun!
Posted: February 13th, 2011 | Author: Andrea | Filed under: mongodb | No Comments »
A new update the the MongoDB Cache extension for Railo is on the extension provider.
Some bug was fixed and performance improved by a better tuning of the query that respects the cache expiration timeout. In my tests I could reach 400 interactions ( put and get of 400 items ) in in average time of 1200ms. Pretty awesome!!!!