Archive for February, 2008
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.
No commentsConverting 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..
No commentsUsing 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.
No commentsUsing 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.
No comments






