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: , ,

Friday, July 27, 2007

Railscasts - free Ruby on Rails screencasts

In my ongoing quest to master Ruby on Rails, I found another great resource to augment my limited knowledge - Railscasts.com.

Thrice weekly, Ryan Bates hosts a new podcast focused on a small aspect of Ruby on Rails. Short and sweet these Railcasts are a fantastic way to get to grips with Ruby on Rails, either by just watching them all, or to answer and help solve specific problems.

Highly recommended.

I discovered Railscasts from the Rails Forum, so I thought it would be a good idea to mention this too, so, there you have it.

Labels: , , ,

Wednesday, July 25, 2007

Ruby on Rails Beginners Tip Sheet

If, like me, you are getting up to speed with Ruby on Rails. I am sure you are doing everything you can to reinforce your learning. I am a bit of a slow learner, but am gradually coming to terms with Rails, with the help of video tutorials, .pdfs and the internet. I fell upon the Tip Sheet for Beginners from the Rails Wiki. The Tip Sheet is a useful tool for helping to remember the core concepts of Ruby on Rails.

Labels: , , ,

Friday, July 20, 2007

Ruby, Ruby, Ruby, Ruby


No, I am not going to explore the lyrics of the Kaiser Chiefs latest sing-a-long. Instead I am going to explore something that is more RoR, than Rock and Roll. I have for the last week or so been exploring Ruby on Rails. Chances are you are already developing on it. But, if you too are thinking about building your own Rails web apps, here are some useful resources to getting to know Ruby on Rails.

RubyonRails.org

The 'Mecca' for all Rails freaks. Ruby on Rails was created by David Heinemeier Hansson, from 37 Signals. Rails was created in 2004, and was built using Ruby, an open-source scripting language, created by Yukihiro "matz" Matsumoto, from Japan and released in 1995. (Mental Note: Must get a longer name for myself!) The Ruby on Rails site is a natural place to start, when delving into this constrained but natural platform.

Ruby, Tie me up.

The idea of constraints might be a little offputting, but in practice, I am finding these "guides" and conventions extremely useful. It only just occured to me what the "Rails" moniker means, well I haven't confirmed this yet, but to me it can be taken literally. Your application benefits, just like the mythical Ruby train as it steams out of the Ruby station from these fixed tracks. Without the rails, the Ruby engine could lose it's way, plough through a bunch of html pedestrians or flatten the litte MySQL kid playing with his select statements. Conventions and constraints are what has made Rails my new scripting platform. Sure, I know enough PHP to do some damage, but the structured platform and conventions, makes it very powerful and surprisingly natural.

Learning Ruby on Rails

http://www.rubyonrails.org/docs will point you in the right direction, in your quest to become a master of Ruby and it's dominatrix, Rails. I really had a lot of fun learning Ruby with help from the poignant, cartoon foxes. Have purchased the pdfs of Agile web development with Rails and when I get my act together will have a few Gordon Ramsay like Rails Recipes that I can serve up to my visitors.

Ruby on Rails Video Learning

I am a subscriber to the VTC movie learning who have a good Rails title and also have spent the last few days learning RoR with my new Guru, Kevin Skoglund, and his fantastic Rails video, Ruby on Rails Essential Training from Lynda.com.

Anyway, I'll leave you with the following Ruby workout:

Repeat this phrase 100 times on your blackboard/aurally/mentally.

"Don't Repeat Yourself
Don't Repeat Yourself
Don't Repeat Yourself
Don't Repeat Yourself
Don't Repeat Yourself
Don't Repeat Yourself
Don't Repeat Yourself
"...

Hang on a minute, "Ruby, am I missing the point here?"

Labels: , , , ,