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 [...]
Tag Archives: Sample Code
The ‘if’ Statement
As stated in the past post, we aim to simplify further the code we have just created with the following shorter version of the same program. 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 Further shortening the code we have [...]
The [] method
We now go through a process which implements the code on the [] method we just showed where we will take a string, searches for the book with the same title. The method would go through the whole table one at a time to get a match as shown with the following: class BookList def [...]
Append
Using the append command/method, we can add an element to the end of the table which in turn returns a reference to the current BookList object.The deleteFirst and deleteLast command allows us to delete the first and last records respectively in the following form/s: For Append: class BookList def append(aBook) @books.push(aBook) self end end For [...]
Blocks – Example
Below, we find sample code which uses block to return the values of the Fibonacci number Series. def fibMax(max) a1, a2 =1,1 # variables that are assigned parallel values while a1
Container Creation
To initialize a container we will do the same steps showing code to support the things that have to be done. We create a table where we can add elements to the contents delete objects from either the beginning or the last entries and display an entry using the index or content on the index. [...]