Showing posts with label rubyonrails. Show all posts
Showing posts with label rubyonrails. Show all posts

Saturday, May 16, 2009

Lower Case, Upper Case and Capitalize in Ruby

Ruby has some very straightforward methods to change the case of your strings.

Here are some of them:

"HoMer".downcase # returns homer
"lisa".upcase # returns LISA
"margie".capitalize # returns Margie
"BaRt".swapcase #returns bArT

Just a quick one! :)

Find this article also on definenull.com.

Search an Array in Ruby

If you want to search for a value in an array in Ruby you could do something like this:


array = ['Bart','Lisa','Maggie']
array.include?('Homer') # returns false
array.include?('Lisa') # returns true

Another possibility is to do this:


array = ['Bart','Lisa','Maggie']
array.index 'Homer' # returns nil
array.index 'Lisa' # returns 1

Both methods should work for searching an array.

For more information check the Ruby API for the Array Class.


Cheers,

Dani


Find this article also on definenull.com.

Thursday, April 9, 2009

Using forms in Ruby on Rails

I found this great guide on how to use forms in Ruby on Rails.
It is quite complete and it comes with a lot of examples on how to create HTML form entries using Ruby on Rails.

Thursday, April 2, 2009

Running tests in the command line on Ruby on Rails

I normally run the test suite for my Ruby on Rails projects using the Netbeans test interface.

Unfortunately this interface is only useful to run the whole suite. It does not offer (as it does for Java) the possibility to run a single test class or a single test from a test class.

When I need to this I just go to the command line and type:

ruby test/functional/accounts_controller_test.rb

But what if I need to run one single test from this test class?
No problem, it is actually very easy. Just type:

ruby test/functional/accounts_controller_test.rb --name test_should_create_account

That's it!

You can also find this article on definenull.com.

Tuesday, March 3, 2009

Change Default Date Format in Ruby on Rails

Just a quick and simple hint!

If you need to set the default formatting of dates throughout your whole application in Ruby on Rails, just add the following line to your "<your application>/config/environment.rb"

my_date_formats = { :default => '%d/%m/%Y' }

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_date_formats)

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_date_formats)


Note 1: Rails uses this :default format when attempting to write dates do the database, so be sure that your DBMS is able to use this new format

Note 2: you will need to restart your server so the changes will be effective.

Find this article also on Define Null.

Tuesday, February 10, 2009

Format Currency in Ruby on Rails

I was looking for a way to display currency data coming from the database as a long. I found out that Ruby on Rails has a buil-in Module Helper method called
ActionView::Helpers::NumberHelper:number_to_currency which allows one to format a number according to some options:


  • :precision - Sets the level of precision (defaults to 2).

  • :unit - Sets the denomination of the currency (defaults to "$").

  • :separator - Sets the separator between the units (defaults to ".").

  • :delimiter - Sets the thousands delimiter (defaults to ",").

  • :format - Sets the format of the output string (defaults to "%u%n").


Using this method, we could write something like this:


<%=h number_to_currency @transaction.value, :unit => 'R$', :separator => ",", :delimiter => "." %>

This would work fine, but in this solution we are breaking the DRY (Don't Repeat Yourself) rule, because I would need to set the same parameters in every place I was displaying currency values.

A better solution would be to have a helper method which I could use in every piece of code where I need a currency. So I came up with the solution below, which consists in writing my own helper method in app/helpers/application_helper.rb:

module ApplicationHelper

number_to_currency(number,:delimiter => ".", :unit => "R$ ",:separator => ",")
end
end

Now, in my templates I should write something like this:

<%=h real_currency @transaction.value %>


Ain't that easier?


Find this article also on Define Null.