Using the [] for conditional processing
The process shown below is and example of using the [] to have the program execute a condition that searches the contents of an array till a match is found. It goes from the top to the bottom to check each and every member of the array to look for a match doing the requested process if it is found and returning nothing if nothing is found.
class BookList
def [](key)
if key.kind_of?(integer)
return @Books[key]
else
for C in 0…@Books.length
return @Books[i] if key == @Book[i].name
end
end
return nil
end
end
Though the above shown method is quite detailed and goes through the array thoroughly, there is a n easier method which we will show with the next post making the whole process not only easier but process faster.
Getting Started and how to open Programs : Part 2
Save the text file with the following filename “first1.rb” and we now go into the syntax checking function and the facility provided to show the syntax check verbosely. The following code shows how code is checked for syntax correctness and returns the result of the said checking by typing the following code :
$ ruby :cw first1.rb
The “-c” flag initiates Ruby’s syntax checker while the “w” flag shows the result of the syntax checker. If there is no issue with the code being checked you get a “Syntax OK” result which means you can execute the code for there are no syntax issues. Type $ruby first1.rb on the command line and you get the result ” The concatenated line is : This is my first Ruby Program “
Getting Started and how to open Programs : Part 1
RubyonRails is similar to most programming languages where in one can use the command line in windows or a terminal in Unix based systems. All programs should be written and saves in plain text format for the compiler and interpreter to process it easily. The first program would give you a feel for the overall syntax of how the program is created and what the compiler does. Type the following code into the text file to see how it is done:
#this is a sample program
a = “This is”
b = “my first Ruby Program”
print “The concatenated line is :”
print a << b
The next post would show the continuation of the exercise.
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.
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”}
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.
The previous post using the yield statement gives us the following output which shows the ease of using blocks in programs which can be used to pass on parameters to the other parts of the same statement.
The code from the previous post gives the output:
Hi There
Hi There
Hi There
The code between the curly braces is associated to the method three times and within that yield command is called three times in succession, each time calling the code contained within the block giving the three output code in the form of the greeting. We will discuss the concepts behind the ‘yield’ statement in the next posts as we continue to build-up up our skills while aiming to use more of the simplified methods used in ruby to further shorten the code making it easier to implement and use..





