There are more ways of constructing string literals such as %q, %Q and “here documents.” The first two are used to starte single or double-quoted strings such as; %q/general single-quoted string/ >> general single-quoted string %Q!general single-quoted string! >> general double-quoted string %Q{Seconds/day: #{24*60*60}} >> Seconds/day: 86400 The character following the “q” or “Q” is [...]
Author Archives: Avatar
Strings (Part 2)
The types of substitution can be easily seen such as; strings inside a single quote have two consecutive backslashes interpreted as one, a backslash followed by a single quote is interpreted as a single quote and so on and so forth as shown below. ‘escape using “\\”‘ >> escape using “\” ‘That\’s right’ >> That’s [...]
Strings (Part 1)
Strings are objects of class String, which are normally simple 8-bit bytes that normally hold printable characters but that is not a requirement as you would see for they could also be used to hold binary data. Created using string literals (which are sequences of characters between delimiters that themselves do some conversion of the [...]
Perl Differences in string handling
A note to Perl users, strings that contain numbers will not be automatically converted to numbers when used in an expression such as when trying to read numbers from a file like the one below does. DATA.each do |line| vals = line.split # split line, storing tokens in val print vals[0] + vals[1], ” ” [...]
Blocks as Closures (Part 3)
That’s where blocks come in, to simplify the definition and use of these buttons making maintenance simpler so we could call the specific block to which we pass on the necessary parameters or methods to attain the final outcome. Below is an example of how this can be achieved with blocks creating a class called [...]
Blocks as Closures (Part 2)
Sample Code: class PlayButton < Button def initialize super(“Play”) # invoke Button’s initialize process end def buttonSelected # do something that should be done when the start button is pressed end end bPlay = PlayButton.new This type of approach would bring about two very problematic scenarios; one, the actions that are to be done when [...]
Blocks as Closures (Part 1)
As it turns out, blocks are the ideal tool for use ion implementing user interfaces where we have a defined graphic that signifies a button and pressing that button would result in a pre-set series of processes. A simple button definition can be: bPlay = Button.new(“Play”) bPause = Button.new(“Pause”) # so on and so forth…… [...]
Blocks for Transactions
Blocks or chunks of code that can be used for transactional control which in our example would have it open a file, process it then make sure that same file is closed after processing before exiting the code. There is an automatic function that ensures this and we will discuss it further in the following [...]
Blocks and Iterators (Part 2)
Anytime the “yield” statement is called upon it calls on the chunk of code within the block executing it returning control to where the yield statement was called continuing on its way till the process is finished. That’s where the magic of Ruby is, that a block can be made to function like a method [...]
Blocks and Iterators (Part 1)
An iterator is a method of calling on a block of code which is somewhat similar to C, Java or Perl, somewhat. The usage of iterators in Ruby does something different as opposed to other languages that share similar functions. Initially a block normally appears adjacent a method or call, that call statement is written [...]