Contact iFingers

Finger Tips - a fistful of handy web notes

Monday, July 30, 2007

New Array on the Block

An Array is a container, which holds values in an ordered fashion.

(If you're using Mac OS X open up Terminal and type irb, then hit enter, to play along at home)

How to create a an Array

my_array = ['Ferrari', 'McLaren', 'Williams']

Get the value of an Array element

my_array[0]

Will return, "Ferrari"

my_array[1]

Will return, "McLaren"

my_array[2]

Will return, "Williams"

my_array.first

Will return, "Ferrari"

my_array.last

Will return, "Williams"

Arrays on the Block

A Block can loop through things. i.e:

my_array.each do |an_element|
puts an_element
end

Returns:

Ferrari
McLaren
Williams

Second Block Example

my_array.each {|an_element| puts an_element}

Returns:

Ferrari
McLaren
Williams

Block example with index

my_array.each_with_index do |an_element, index|
puts "#{an_element} is number: #{index}"
end

Returns:

Ferrari is number: 0
McLaren is number: 1
Williams is number: 2

Labels: , ,

A fast dash with the Ruby Hash

A Hash or Hash Table as it is sometimes called is used throughout Ruby and Ruby on Rails development. A Hash is a data structure that can hold keys and values. Similar to an array, but not ordered.

(If you're using Mac OS X open up Terminal and type irb, then hit enter, to play along at home)

How to create a Hash

my_hash = {:car => "McLaren", :driver => "Fernando Alonso", :engine => "Mercedes-Benz"}

Get the value of a key

my_hash[:driver]

Will return, "Fernando Alonso"

Set a new value for a key

my_hash[:driver] = "Lewis Hamilton"

Return the Hash

my_hash

Will return:

{:car => "McLaren", :driver => "Lewis Hamilton", :engine => "Mercedes-Benz"}

Add a new key value pair to the hash

my_hash[:tyres] = "Bridgestone Potenza"

Return the Hash

my_hash

Will return:

{:car => "McLaren", :driver => "Lewis Hamilton", :engine => "Mercedes-Benz", :tyres => "Bridgestone Potenza"}

Labels: , ,

Saturday, July 28, 2007

Ruby on Rails Pluralize Tool

Came across a great tool to find out the correct pluralization for Ruby on Rails apps. Check out Geoffrey Grosenbach's Pluralization Tester. Which will give you the correct pluralizations for your RoR models, controllers and database tables.

Octopussy

For example, if you wanted to build a web app to hold your collection of stuffed octopi. You could load in octopus to Geoffrey's tester and will get a table showing the right names for each object.

This is extremely useful for a Ruby on Rails noobie like me. Nice one Geoffrey!

Labels: , ,

Rails params

Working with PHP, I developed a fondness for using the URL as a means of querying my MySQL database. Often using the $_POST and $_GET commands to retrieve the names and values from the URL's query string. With Ruby on Rails we use the params method.

params

I have just had the pleasure of watching the Rails from Scratch Pt1 movie from PeepCode. They have a good section in the movie describing params, and how we can use it to create a hash to pass data to the view.

Rails by convention uses the following path in it's URL path:
/:controller/:action/:id

This makes it very easy to create links either dynamically or with static queries, and we have the params method which we can use to grab stuff from the URL query string. Here are some examples which can be pasted into a :controller and then the results will be magically available in your view.

#place this in your controller which sets the id from the URL into the @message_from_params instance variable @message_from_params = params[:id]

Then place the @message_from_params instance variable in your view:

<%= @message_from_params %>

So with a url of /machines/index/1, the message from params would be: 1.
Yet, Rails is clever enough to accept strings as well. So, /machines/index/1-Robots, we would get 1-Robots.
Get the idea? If not check out the PeepCode!

Grabbing Query Strings

So if we have a query string like this: /machines/index/robots?name=R2D2. We could then create an instance variable in our Machines Controller like this:

@name = params[:name]

Then place something like this in our view: Name: <%= @name %> and your web browser will return: "Name: R2D2".

Use params, NOT @params

According to the people that should know, it is important to avoid calling @params, as this is now deprecated. Learn More.

Labels: ,