Veerasundaravel's Ruby on Rails Weblog

October 26, 2010

String to Boolean conversion in Ruby

Filed under: Ruby — Tags: , , , , , , — Veerasundaravel @ 11:33 pm

I was working in a file processing task, I expected to test an input as Boolean or not. But my input can as ‘false’ or false or 0.

Then I searched for  Ruby built-in conversion method, but no success. So I build a custom method to convert the string representation into a Boolean object. Lets build a kernel module with Boolean method.

module Kernel

def Boolean(string)
return true if string== true || string =~ (/(true|t|yes|y|1)$/i)
return false if string== false || string.nil? || string =~ (/(false|f|no|n|0)$/i)
raise“invalid value for Boolean: \”#{string}\””)


Lets check the same in irb :

>> Boolean(true)
>> true

>> Boolean(false)
>> false

>> Boolean(‘false’)
>> false

>> Boolean(‘f’)
>> false

October 22, 2010

Testing Restful Routes from Console

Filed under: Ruby On Rails — Tags: , , , , , , , — Veerasundaravel @ 10:28 pm

When me and my friend discussed about advanced Restful routings concept, he asked about testing the route specification in rails console. Yes that kind of testing will help more on testing.

After surfing internet, he found the available option for it. Here I described that feature with some basic example.

Lets starts with sample rails app generation:

rails routes_testing
cd routes_testing

Then create your required models and controllers. Lets say we are going to create a controller named categories.

Now open the routes.rb files under the config folder of your application. Let’s navigate down to the block below:

# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products

Let’s insert the following simple route right underneath the previous code:

map.resources :categories

So we finished our routes specification. Lets move to our Rails console.

ruby script/console

To test our route on the command line, the first thing we need to do is include the ActionController::UrlWriter module:

>> include ActionController::UrlWriter

And from here all of our route helpers (*_path and *_url) are available from the console.

>> categories_path
=> “/categories”
>> new_category_path
=> “/category/new”
>> edit_category_path(:id => 1)
=> ”/category/1/edit”
>> edit_category_path(:id => 1, :site_id=>2)
=> ”/category/1/edit?site_id=2”

Hence all of your routes specification can be nested via Rails Console very easily.

And alternatively, you can use the ActionController::Integration::Session object that is available at the console with the name “app”:

>> app.categories_path
=> “/categories”

October 7, 2010

Useful Rails3 Commands – Cheatsheet

Some of useful Rails3 Commands and their descriptions are listed below:

rails new ApplicationName – Create a new application
rails generate/g model ModelName – Creates a model with the specified model_name
rails generate/g controller ControllerName – Creates a controller with the specified controller_name
rails generate/g migration MigrationName – Creates a migration with the specified migration_name
rails generate/g scaffold ModelName ControllerName – A shortcut for creating your controller, model and view files etc.
rails destroy controller ControllerName – Destroys the created controller and its related file. Same way you can destroy your model, migration files etc.
rails server/s – start ruby server at http://localhost:3000
rails plugin install PluginName – Installs the specified plugin.
rails console/c – Opens the rails console for the current RAILS_ENV
rails dbconsole/db – Opens the DB console for the current RAILS_ENV
rails performance – For benchmarkering and profiling your application.

rake –tasks – Lists all available rake tasks
rake db:create – Create the database defined in config/database.yml for the current RAILS_ENV
rake db:drop – Drops the database for the current RAILS_ENV
rake db:migrate – Migrate the database through scripts in db/migrate folder.
rake db:reset – Drops and recreates the database from db/schema.rb for the current environment.
rake db:rollback – This will run the down method from the latest migration.
rake db:schema:dump – Create a db/schema.rb file that can be portably used against any DB supported by AR
rake doc:app – Build the RDOC HTML Files
rake db:sessions:create – Creates a sessions migration for use with CGI::Session::ActiveRecordStore
rake gems – List the gems that this rails application depends on
rake gems:install – Installs all required gems for this application.
rake log:clear – Truncates all *.log files in log/ to zero bytes
rake rails:freeze:gems – Lock this application to the current gems (by unpacking them into vendor/rails)
rake routes – Print out all defined routes in match order, with names.
rake test – Run all unit, functional and integration tests
rake test:functionals – Run tests for functionalsdb:test:prepare / Run the functional tests in test/functional
rake test:integration – Run tests for integrationdb:test:prepare / Run the integration tests in test/integration
rake test:units – Run tests for unitsdb:test:prepare / Run the unit tests in test/unit
rake tmp:clear – Clear session, cache, and socket files from tmp/

gem list – Lists all the installed gems.
gem install GemName – Installs the specified gem in to your machine.
gem uninstall GemName – Uninstalls the specified gem from your machine.
gem server – present a web page at http://localhost:8808/ with info about installed gems.

Also Refer:

Useful Rails Commands & Cheatsheet
Useful SVN and Git commands – Cheatsheet

Older Posts »

%d bloggers like this: