Hashes : In Layman’s Term
Image Source: codeproject.com
One key feature of Ruby on Rails is Hashes. Now, just try to think of hashes like a PARTY. The party is a potluck occasion, naturally, each guest is required to bring an item or food. You will then need a LIST of all the guests, now this can be managed as an array. But you will also have to make a way to monitor all types of food that is being brought to the event. And to explain it further, you will want to know who is bringing the pasta, who will be bringing the fried chicken and so on. This is where Hashes come in. You will use hash to store value pairs or key pairs, this in turn will enable you to store that particular data. If for example Amanda is the “key”, and she brought cake, the variable stored and accessed by the key will be “cake”.
Creating a Model on Rails

Image Source: intertwingly.net
This is not based on a database application, so we can pretty much keep the name, which is comfortable for us. Lets take for example if we have to make a DataFile model.
C:\ruby> ruby script/generate model DataFile
- exists app/models/
- exists test/unit/
- exists test/fixtures/
- create app/models/data_file.rb
- create test/unit/data_file_test.rb
- create test/fixtures/data_files.yml
- create db/migrate
- create db/migrate/001_create_data_files.rb
Now we will create a method called save in data_file.rb model file. This method will be called by the application controller.
- class DataFile < ActiveRecord::Base
- def self.save(upload)
- name = upload['datafile'].original_filename
- directory = “public/data”
- # create the file path
- path = File.join(directory, name)
- # write the file
- File.open(path, “wb”) { |f| f.write(upload['datafile'].read) }
end
Ruby Symbols: Explained

Image Source: knowtebook.com
Many programmers old and new to ruby alike, get confused when they see Ruby symbols, sometimes or all the time. It will take a lot of getting used to, before you ever do this smoothly. A lot of us came to know about the Ruby language through Ruby on Rails projects. In Ruby on Rails, symbols are everywhere. I mean everywhere. So it is important to note and memorize the concept of symbols in Ruby.
A symbol in Ruby is an instance of the class Symbol. A symbol is defined by prefixing a colon with an identifier like “:name”, “:id” or “:user”. The Symbol class in Ruby contains one class method:
- all_symbols and instance methods id2name, inspect, to_i, to_int, to_s and to_sym.
- all_symbols - returns an array of all the symbols in Ruby’s symbol table.
- id2name - returns the string representation of the symbol,:name.id2name returns “name”.
- inspect - returns the symbol literally
- to_i - returns an integer unique for each symbol
- to_int - same as to_i
- to_s - same as id2name
- to_sym - converts the symbol to a symbol.
Necessity for a Hash Key

Image Source: www.cheat-sheets.org
The only restriction for a hash key is that it must reply to the message hash with a hash value, and the hash value for a given key must not alter. This means that certain classes (such as Array and Hash, as of this writing) can’t conveniently be used as keys, because their hash values can change based on their contents.
If you keep an external reference to an object that is used as a key, and use that reference to alter the object and change its hash value, the hash lookup based on that key may not work.
Because strings are the most frequently used keys, and because string contents are often altered, Ruby treats string keys specially. If you use a String object as a hash key, the hash will replace the string internally and will use that duplicate as its key. Any changes afterward made to the original string will not influence the hash.
If you write your own classes and use instances of them as hash keys, you need to make sure that either (a) the hashes of the key objects don’t change once the objects have been created or (b) you bear in mind to call the Hash#rehash method to reindex the hash whenever a key hash is altered.
RoR and Text Editors Part-1
Though there is no preferred editor in the coding and editing of programs which is usually left to the programmer to decide. There are a couple of purpose built editors that can be used with RoR.
Emacs, has a quite simple configuration that accompanies its use and allows simultaneously displays changes as they are made hence it�s classification as a Display Editor. It also has a nifty help function the user can evoke by the simple press of the Ctrl-H keys. The Point feature allows the definition of the position of a character as they are entered onto the keyboard. The Echo Area Feature is a segment on the lower bottom of the screen editor that shows the amount of characters on the screen for various purposes should they be needed. The Mode Line is the last line on the screen which is signified by the starting and ending with dashes on the screen. Like all editors, Emacs has a menu bar where all the options available to the user can be found making use simpler instead of having to memorize special key combinations.
Before You Start
Assuming you have even just a little background in programming with any language there are four basic programs and extensions that you need but are provided by the quick installers you’ve just downloaded to your hard drive. You have The Ruby programming language program files, the Rails part of the programming extension/platform, MySql or your database query handling system and the Apache which is a web server that you use to emulate your application’s execution on the web. Granting you have these set up properly and tested (instructions for which are included in the installer package). You can now start building your first application with RoR.
How to Install RubyGems and Rails on a Shared Web Host
by Chubs

RubyGems or Gem allows you to install extra packages and libraries for Ruby. You need Gem before installing Rails. You can get the latest version of RubyGems here.
cd /temp
wget http://rubyforge.org/frs/download.php/3700/rubygems-0.8.10.tgz
tar -zxvf rubygems-0.8.10.tgz
cd rubygems-0.8.10
To install RubyGems, you need to use Ruby to call the setup script:
ruby setup.rb
Here, you should see “Successfully built RubyGem” if it’s finished. Next, open the file “/usr/local/ruby/bin/gem” in your favorite text editor (such as vi) and edit the first line so you’ll know where to find your custom Ruby installation. Example:
#!/usr/local/bin/ruby
Now, add a few more symlinks:
cd /usr/local/bin
ln -s /usr/local/ruby/bin/erb erb
ln -s /usr/local/ruby/bin/eruby eruby
ln -s /usr/local/ruby/bin/gem gem
ln -s /usr/local/ruby/bin/gem_server gem_server
ln -s /usr/local/ruby/bin/testrb testrb
ln -s /usr/local/ruby/bin/update_rubygems update_rubygems
RubyGems is now installed. To install Rails, use:
gem install rails –remote
How to Install Ruby on a Shared Web Host
by Chubs

Create a directory for all of the software you are going to download and installed. In this example, the created directory is “temp” at the root of the files system, but you can use any name and location you like.
cd /
mkdir temp
chmod 775 temp
Now, download, configure and install Ruby. You can download the latest stable source tarball for Ruby here. Wget is used in this example:
cd temp
wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.2.tar.gz
tar -zxvf ruby-1.8.2.tar.gz cd ruby-1.8.2
Specify an install directory since there are times when you won’t have permissions to install things in normal directories. Here, we can see the “configure” script for Ruby, creating a directory inside “/usr/local” for installation:
./configure –prefix=/usr/local/ruby –exec-prefix=/usr/local/ruby
Now, you have a Makefile that you can compile:
make
make install
Your Ruby is now installed. You should try making a symbolic link to Ruby somewhere in your local path:
ln -s /usr/local/ruby/bin/ruby /usr/local/bin/ruby
List of Rails Web Hosts

Here is a list of web hosting services which support Ruby on Rails in case you might be looking for one for your website:
America:
• A2 Hosting
• AVLUX
• BILES ONLINE Ruby on Rails Hosting
• BlueHost
• Crucial Paradigm
• Dedicated Hosting
• Domain Gurus
• Hosting Rails
• JaguarPc
• SpeedyRails
• Zowes Web Hosting
Asia:
• Exaltinfo
• GVT.hk
• RailsFactory
• Web Development India
• Web Hosting Delhi
Australia:
• Anchor Systems
• Avial Web Design and Hosting
• Crucial Australia
• Hostcentral
Europe:
• 600host.net
• Blacknight Solutions
• Brightbox
• GPcom Media
• Media72 Hosting
For the complete list of Rails web hosts, visit: http://wiki.rubyonrails.org/rails/pages/RailsWebHosts
All about Web Servers

There are a lot of web servers that are compatible with Rails, but Apache and Lighttpd are said to be the most popular. Any web server that supports mod_ruby (Apache), CGI, or SCGI can be used for running Rails. You may also want to try tinkering with Lighttpd, which can rival Apache 2.0.54 in speed, by using MPM Worker on Debian 3.1 (claimed to be faster than Apache 1.x or2.x MPM Prefork). However, working with Lighttpd-1.4.10 can be very buggy so you might want to consider using Lighttpd-1.4.11 or a newer version instead. Zed Shaw’s Mongrel, a ruby based webserver that utilizes Cextensions to increase performance, is also another good option. You can run Mongrel on its own or in a cluster behind an Apache or a Lighttpd proxy.

