Next step : Creating the database for the filename application
The next phase or step would be to create a database for the application to use. Make sure the MySql engine is running and in the command window type “mysql -u root -p” and press enter and another enter for the password when prompted for there has not been any defined password yet. You are now logged into the engine as the root user and proceed to create the database by entering the following command “create database filename_development”. Also type in “grant all on filename_development.* to ‘ODBC’@'localhost’ this tells windows to grant access to a user named ODBC so you avoid an error when you try to access the said database from the command prompt. We next tackle the creation of tables that would allow the database to store the information we send it.
Database manipulation
The database has been initialized, so now we get down to business of defining the fields in that database and include a field that we would call a foreign key which allows the establishment of the one to many relationship between the tables. This part of the process requires quite a bit of background of database creation, manipulation and handling. It also requires knowledge regarding the workings of data types. So assuming you do not have much knowledge in such areas do some more reading to give you a better understanding of what how and why they are there. The ROR developers might say that it is very easy to do and use ROR but without the background on logic formulation, data types and manipulation as well as database handling the rest of the posts which would tackle ever hardening topics and operations would be very difficult to make sense of.
Don’t get me wrong, we just want to make life easier for you and not have you not understanding anything at all.
Next Step : Creating Empty Applications
Now that all has been set-up and is working, you should be able to create empty apps which is merely defining the application using rails which automatically creates all the necessary directories that are needed. Open a command prompt window and go to the directory where it was installed and proceed into the rails_apps directory. Type in ‘rails filename’ and rails creates all the necessary directories you would be needing for the development process. Don�t get freaked out when you see the multitude of directories for they will be filled out by “RAILS” and not by you manually. The next post will discuss some of the nuances of those dizzying directories to take some of the fog over them out.
Simplifying the previous program with modifiers
As said in the past post, there is an easier way of doing the stuff we did in the last program which would be very helpful when coding thousands of line of code when you do end up building your own programs is ruby.
class BookList
def [](key)
if key.kind_of?(integer)
result = @Books[key]
else
result = @Books.find { |aBooks| key == sBooks.name}
end
return result
end
end
Simplifying the code further by using the ‘if’ statement as a modifier it becomes a shorter easier to attain the same results as with the first program:
class Booklist
def [](key)
return @Books[key] if key.kind_of?(Integer)
return @Books.find { |aBooks| aBooks.name == key }
end
end
The use of the ‘find’ command in Ruby is simply a call to a function that is executed and it can be compared to a block call in many other languages such as Perl, C++ or Java.
Using the ‘yield’ statement
It might be almost similar but relatively different in a big way for blocks may appear only in the source adjacent to a method call which means it should be written on the same line as the method’s last parameter and it is not implemented once it is encountered but, Ruby rather remembers the context by which the block of code appears then enters the method. Within the method itself, the block of code may be called as if it were a block in itself by using the ‘yield’ statement. After the block of code has been executed, control returns immediately right after the call to the yield statement. Sample use of ‘yield’:
def threeTimes
yield
yield
yield
end
threeTimes {puts “Hi There”}
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
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
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.





