Tag Archives: Sample Code

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 [...]

Posted in Development, Programming Basics | Also tagged | Comments closed

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 [...]

Posted in Development, Programming Basics | Also tagged | Comments closed

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 [...]

Posted in Development, Programming Basics | Also tagged | Comments closed

Simplifying the code

We now check if the book titles are deleted from the first and end of the list which should give us ‘nil’ when and if the list becomes void of contents. list.deleteFirst >> Books: ABDC–Author1 (1) list.deleteFirst >> Books: EFGH–Author2 (2) list.deleteLast >> Books: MNOP–Author1 (1) list.deleteLast >> Books: IJKL–Author3 (3) list.deleteLast >> nil We [...]

Posted in Development, Programming Basics | Also tagged , | Comments closed

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 [...]

Posted in Development, Programming Basics | Also tagged | Comments closed

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

Posted in Development, Programming Basics | Also tagged | Comments closed

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. [...]

Posted in Development, Programming Basics | Also tagged | Comments closed