Ruby
Building an application to track expenses in one hour using GeoChat
Whenever one of our team member travels to another country we tell her to track expenses like food, transportation and accomodation so that later that money can be refunded. Each of us does it in a different way: we write it in a notes application in a cellphone, or in a text file in our [...]
Install the RMagick gem in the painless way with Homebrew
Introduction
Disclaimer: this section is just happy talking, if you want to get right to the beef, skip it and go right to the beef.
Yesterday was a sad day for me. It wasn’t meant to be like that.
I began the day with the intention of setting up a development environment for this amazing RoR application I’ll [...]
Quick way to test many values in ruby
Many times we end up having similar tests: same logic but different inputs and expected outputs. For example, suppose we need to test our brand new sqrt function:
test "sqrt 1" do
assert_equals 1, sqrt(1)
end
test "sqrt 4" do
assert_equals 2, sqrt(4)
end
test "sqrt 9" do
assert_equals 3, sqrt(9)
end
Here we are dupplicating code, since “assert_equals [...]
Deleting children with accepts_nested_attributes_for in Rails
On the previous post I wrote a few lines on the basic usage of the accepts_nested_attributes_for method in rails models. I strongly recommend reading that post before this one if you haven’t. Although there is a standard way for deleting items, there is few information on how to deal with them on the model or [...]
Handling children with accepts_nested_attributes_for in Rails
Rails makes it easy to build HTML forms associated to a certain model. Simply using the form_for instruction on the view, writing a simple update method in the controller and setting validation logic on the model, makes standard CRUD operations incredibly easy to code. Since version 2.3, Rails also provides a convenient way of dealing [...]
String concatenation in ruby
I just found out the difference between += and << when used for a string.
irb(main):001:0> str = 'chan'
=> "chan"
irb(main):002:0> str.object_id
=> 69952758899780
irb(main):003:0> str += 'ged'
=> "changed"
irb(main):004:0> str.object_id
=> 69952758848800
irb(main):005:0> str << ', now not'
=> "changed, now not"
irb(main):006:0> str.object_id
=> 69952758848800
So I think that basically you should never use += for strings, unless you really want to hurt the [...]
Ruby-Jump plugin for gedit
I searched the internet for a gedit plugin that would allow me to press a key and jump (navigate) to the file where a ruby class is defined. I couldn’t find one so I wrote one (I know about Geany but I couldn’t find that functionality there either).
Usage
You need to have the FileBrowser plugin installed [...]
Build Xml files in Ruby
If you have to generate xml in ruby I suggest take a look at builder gems. It has really little overhead to generate xml. Take a look to this example:
require 'Builder'
xm = Builder::XmlMarkup.new( :target => $stdout , :indent => 2 )
xm.instruct!
xm.people do | p |
p.person(:firstname => "brian", :lastname => "cardiff") do | a [...]