See the Troubleshooters.Com Bookstore.
#!/usr/bin/ruby |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./loop.rb |
NOTE There are actually two versions of the elipses operator, the three period version as shown previously, and the two period version. The two period version is inclusive. In other words, 1...3 means 1 up to but not including 3, while 1..3 means one through 3. By using the appropriate version of the elipses operator you can save having to code convoluted end conditions. |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./loop.rb |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb |
#!/usr/bin/ruby presidents = ["Ford", "Carter", "Reagan", "Bush1", "Clinton", "Bush2"] for ss in 0...presidents.length print ss, ": ", presidents[-ss -1], "\n"; end |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb |
#!/usr/bin/ruby |
#!/usr/bin/ruby |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./loop.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./test.rb
|
puts (my_array.collect {|word| word.capitalize})Whereas do/end bind more loosely, like this:
puts (my_array.collect) do |word| word.capitalize} endNote that the latter represents a syntax error anyway, and I've found no way to coerce do/end into doing the right thing simply by using parentheses. However, by assigning the iterator's results to a new array, that array can be used. It's one more variable and one more line of code. If the code is short, use braces. If it's long, the added overhead is so small a percentage that it's no big deal:
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./test.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./loop.rb
|
#!/usr/bin/ruby |
#!/usr/bin/ruby |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
METHOD |
ACTION |
ARGUMENT |
RETURNS |
push |
Appends its argument to the end
of the array. |
Element(s) to be appended to end
of the array. |
A string consisting of the
concatination of all non-nil elements in the array AFTER the action was
taken. |
pop |
Returns the last element in the
array and deletes that element. |
None. |
The last element of the array. |
shift |
Returns the first element of the
array, deletes that element, and shifts all other elements down one
location to fill its empty spot. |
None. |
The first element in the array. |
unshift |
Shifts all elements of the array
up one, and places its argument at the beginning of the array. |
Element(s) to be prepended to
start of array. |
A string consisting of the concatination of all non-nil elements in the array AFTER the action was taken. |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./array.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hash.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hash.rb
|
#!/usr/bin/ruby -w
|
[slitt@mydesk ~]$ ./test.rb |
Notice that often a simple <=> command does not suffice, and you actually need to write your own function to establish collation order. Simply write a function taking two arguments (a and b) that returns 1 when a is superior to b, -1 when a is inferior to b, and 0 when they are equivalent. |
Method | What it does | Synonyms |
has_key?(key) | Tests whether the key is present in the hash. | include?(key), key?(key) and member?(key) |
has_value?(value) | Tests whether any element of the hash has the value, returning true or false. | value?(value) |
index(value) | Returns the key for an element with the value. I don't know what happens if multiple elements have that value. | |
select {|key, value| block} => array | Returns an array of key/value pairs for which block evaluates true:h.select {|k,v| v < 200} |
|
empty? | Returns True if no key/value pairs | |
inspect | Return contents of the hash as a string | |
invert | Returns a new hash with keys and values switched. | |
length | How many key/value pairs does it have? | size() |
sort {| a, b | block } => array | ||
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
mystring.capitalize |
Title case. Returns new string
equal to mystring except that the first letter of every word is
uppercase |
mystring.capitalize! |
Title case in place. |
mystring.center(mynumber) |
Returns a new string mynumber
long with mystring centered within it. If mynumber is already less than
the length of mystring, returns a copy of mystring. |
mystring.chomp |
Returns a new string equal to
mystring except any newlines at the end are deleted. If chomp has an
argument, that argument serves as the record separator, replacing the
default newline. |
mystring.chomp! |
Same as chomp, but in place.
Equivalent of Perl chomp(). |
mystring.downcase |
Returns new string equal to
mystring but entirely lower case. |
mystring.downcase! |
In place modifies mystring,
making everything lower case. |
mystring.reverse |
Returns new string with all
characters reversed. IOWA becomes AWOI. |
mystring.reverse! |
Reverses mystring in place. |
mystring.rindex(substring) |
Returns the subscript of the last occurrence of the substring.
Like index except that it
returns the last instead of first occurrence. This method actually has
more options, so you might want to read the documentation. |
mystring.rjust(mynumber) |
Returns a copy of mystring,
except the new copy is mynumber long, and mystring is right justified
in that string. If mynumber is smaller than the original length of
mystring, it returns an exact copy of mystring. |
mystring.split(pattern, limit) |
Returns a new array with parts
of the string split wherever pattern was encountered as a substring. If
limit is given, returns at most that many elements in the array. |
mystring.strip |
Returns a new string that is a
copy of mystring except all leading and trailing whitespace have been
removed. |
mystring.to_f |
Returns the floating point
number represented by mystring. Returns 0.0 if it's not a valid number,
and never raises exceptions. Careful! |
mystring.to_i | Returns an integer represented
by mystring. Non-numerics at the end are ignored. Returns 0 on invalid
numbers, and never raises exceptions. Careful! |
mystring.upcase |
Returns a new string that's an
uppercase version of mystring. |
mystring.upcase! |
Uppercases mystring in place. |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./string.rb
|
NOTE
This section assumes you understand the concept of regular expressions. If you do not, there are many fine regular expression tutorials on the web, including this one on my Litt's Perls of Wisdom subsite. |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./regex.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./regex.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./regex.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./regex.rb
|
#!/usr/bin/ruby |
#!/usr/bin/ruby
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
[slitt@mydesk slitt]$ ./hello.rb
|
beginIn the preceding, variables mySyntaxError and myStandardError are local variables to store the contents of global variable $!, the exception that was raised.
# attempt code here
rescue SyntaxError => mySyntaxError
print "Unknown syntax error. ", mySyntaxError, "\n"
# error handling specific to problem here
rescue StandardError => myStandardError
print "Unknown general error. ", myStandardError, "\n"
# error handling specific to problem here
else
# code that runs ONLY if no error goes here
ensure
# code that cleans up after a problem and its error handling goes here
end
begin
# attempt code here
rescue
puts $!
if EscNotPressed()
print "Reload the CD, or press ESC\n"
retry
else
puts "User declined to retry further"
end
end
raise if age < 65
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
raise "Must be 65 or older for Medicare"
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
raise RangeError, "Must be 65 or older for Medicare", caller
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
myException = MedicareEligibilityException.new(name, age)creates an instance of class MedicareEligibilityException whose instance variables contain the person's name and age for later reference. Once again, this is very contrived, but it illustrates some of the flexibility of exception handling:
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb |
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby
|
[slitt@mydesk slitt]$ ./hello.rb
|
system "stty", '-icanon', 'eol', "\001";Terminal I/O is pretty simple in Ruby. So is file I/O...
int = STDIN.getc
system "stty", 'icanon', 'eol', '^@'; # ASCII null
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
profit = revenue - expenseIn the preceding, profit, revenue and expense are all objects of class Float. The minus sign (-) is not a Ruby operator -- it's a method of the Float class. In the C language, the minus sign would be an operator supplied by the language, but in Ruby it's just a method of the Float class. Incidentally, a plus sign method is implemented in class Fixnum integers, where once again it adds the value, and in the String class, where it concatinates strings.
/* THE REPORT VARIABLE */ |
The
REPORT structure kept
track of the current position of the print head (y and x), the number
of lines on a page (pglength), and the file to which to write the
output (the file was usually a printer device). All this information
remained persistent in the report structure. The report structure was manipulated by a function called atyxpr(),. To print a string at a specific line and column, the programmer specified the string to print and the y and x coordinates (row and column) at which to start printing the string. Also specified was the report structure. If the row and column were specified as both being 0, atyxpr() printed the string at the current print head position, as if the print was done by a simple printf(). If the row was the same as the current printhead row but the column was farther out, atyxpr() printed spaces until the printer head was in the desired place, and then the string was printed. If the desired row was below the current printhead position, atyxpr() printed linefeeds to get to the desired row, printed spaces to get to the desired column, and then printed the string. If the desired row was above the current printhead position, that meant that it needed to be printed on the next page, so a formfeed was issued, then enough linefeeds to get to the desired row, then enough spaces to get to the desired column, and then the string was printed. |
class MyclassThe preceding class would not error out, but it does nothing other than tell the name of its class:
end
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby
|
[slitt@mydesk slitt]$ ./hello.rb
|
#!/usr/bin/ruby |
[slitt@mydesk slitt]$ ./hello.rb
|
|
|