I’ve recently discovered a little know feature of Rake that I would like to share.
Everyone who ever used Rails knows about rake – stuff like rake db:migrate has become automatic for a lot of us. A lot of people have also written their own rake tasks. While default Rails tasks don’t take parameters you sometimes want to make a task that has them.
The usual way to do it is to use environment variables – something like this:
rake db:migrate ENV=production
And then you can access those parameters in the task like this:
desc 'my task' task :my_task => :environment do puts ENV[:ENV] end
While this works, it feels a bit hacky – environment variables were not meant to be used like that.
Turns out there is a cleaner solution.
desc 'parametrized task' task :ptask, [:param1, :param2] => [:environment] do |t, params| puts params[:param1] puts params[:param2] end
What happens here is that we used a special rake syntax that defines parameters for the task – that’s what the first array is about. The second array is for the dependencies.
To invoke this task you do:
The extra nice thing about this approach is that the parameters become part of task declaration. If you run rake -T you will see the task listed with all its parameters, so you don’t have to remember them in your head. Another cool thing is that the parameters defined in this way propagate through dependencies. For instance:
desc 'another task' task :atask, [:param1, :param2] => [:ptask, :environment] do |t, params| puts params[:param1] puts params[:param2] end
If you now call: rake atask[1,2] both the atask and the ptask will get the parameters as needed.
Amazing little project that allows you to create and host your own gems: https://github.com/cwninja/geminabox
#install the gem gem install geminabox # create config.ru file mkdir gems; cd gems vim config.ru #config.ru content require "rubygems" require "geminabox" Geminabox.data = "/var/geminabox-data" # …or wherever run Geminabox #save # create gems directory mkdir -p /var/geminabox-data/gems chown 777 /var/geminabox-data/gems -R # run the server rackup config.ru # That's it J
Go to http://127.0.0.1:9292 to see your own gems server
Allows "streaming" new and popular bookmarks, as well getting only bookmarks by specific tags.
I’ve made a simple gem for loading recent bookmarks and most popular tags for them, so if you are interested in an example of how to use those feeds you can at my code at:
Makes creating ruby micro benchmarks easier than ever.
And once you are done, it’s super easy to share the benchmark with the community:
$ gem install bench_press
$ bench_press --publish my_benchmark.rb
The website is here: http://rubybenchmark.com/
Just another awesome idea
How lazy you need to be to not include this simple line of code?
The error handling and error messages take more space than that…
The demo is a must see