Archive for the 'Basics' Category
Comments (Part 1)
Their use seems elementary but ask any well-seasoned programmer and they too use them. It allows you to understand complex functions or to explain the operation you’re going to do or just about what ever you would want to include in your programs as long as they are treated as comments. They also make great tools when troubleshooting/debugging programs for sometimes, the long days and nights do translate into a lot of code, some of which look almost all the same. Ruby reminds me of a documentation program we had included with previous programming languages such as C and even Cobol which has an add-on or function with the compiler which dissects the program and documents everything.
Comments are off for this postConverting Classes in Ruby
The two basic numeric data types in ruby floats and integers are treated in several classes or methods so we have to be very clear on which is which and how methods work on them. As discussed the standard mathematical operators treat numbers differently depending on the method they were presented a sample of which is shown below:
String#to_i : This method/operation converts String to Integer
String#to_f : This method/operation converts String to Float
Float#to_i : This method/operation converts Float to Integer
Float#to_s : This method/operation converts Float to String
Integer#to_f: This method/operation converts Integer to Float
Integer#to_s: This method/operation converts Integer to String
Ruby Mathematics
The language supports the two number types, integers (whole numbers) and floating point numbers(fractions). For integers, there are instances of Class Fixnum and Bignum and floating point numbers there are instances of class float.
RoR as any programming languages can perform all forms of mathematical operations. It gives reflects integers when two integers are added and vice versa with floating point numbers which will give a floating point number when a fraction is divided by an integer. These operations are treated as intact methods along witht he various mathematical processes are also treated as methods just the same which gives them the properties of an operator of sorts. Narray as the name suggests, is a package that functions to handle arrays of large sizes. Other ruby libraries such as Vector and matrix are very strong extensions of C which can handle statistical information.
Yield statement Continued
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..
Comments are off for this postUsing 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”}
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.
Comments are off for this postRoR 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.
Comments are off for this postGetting 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 ”
Comments are off for this postGetting 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.
Comments are off for this postNext 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.
Comments are off for this post








