#!/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 |
#!/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 |