Some gems from last week ...
1. Ruby's
begin and
rescue clauses allow you to catch anything exceptional that could derail your program. But did you know that you can also add an
else clause to be run
only in the event of there being no exception? There's an example from
last week's Ruby course which may be found
[here].
2. Many languages allow you to provide a
callback (see
"What is a callback?") to sort in your own way, but if you're effectively sorting a hash based on the values it contains, you've usually got to declare or have the hash global in some way. In Ruby, you can pass in a reference to the hash as part of the callback call.
P.S. You CANNOT sort a hash. You're really sorting an array of the keys, and then cherry picking the members in the resultant order
Passing in:
for ipv in counter.keys.sort { |a,b| byvalue a,b,counter }
To the function that compares:
def byvalue (a,b,datasource)
return datasource[b] - datasource[a]
end
This is from the same example as the one above ...
3. Yes - I know that the word "gems" in Ruby has been used to refer to libraries and modules which you can download and install on top of / within your Ruby. I used the
watir-webdriver gem for web site testing last week - see
[here] - and there are many more.
(written 2011-09-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R111 - Ruby - Exceptions. [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
[3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
[3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
[3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
[2622] Handling unusual and error conditions - exceptions - (2010-02-03)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
[2615] String to number conversion with error trapping in Ruby - (2010-02-01)
[1875] What are exceptions - Python based answer - (2008-11-08)
R118 - Ruby Sources and Resources [3783] Load path, load and require in Ruby, and a change from 1.8 to 1.9 - (2012-06-24)
[3622] Loading Ruby classes - where does Ruby look? - (2012-02-24)
[3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)
[550] 2006 - Making business a pleasure - (2006-01-01)
R107 - Collections (Arrays and Hashes) in Ruby [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
[3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
[3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
[2618] What are Ruby Symbols? - (2010-02-02)
[2606] Sorting arrays and hashes in Ruby - (2010-01-30)
[2291] Collection objects (array and hash) in Ruby - (2009-07-16)
[991] Adding a member to a Hash in Ruby - (2006-12-16)
5703
Some other Articles
Python for loops - applying a temporary second name to the same objectMelksham to become a part of Trowbridge?Light bulbsMoving from scripting to Object Orientation in PythonSorta sorting a hash, and what if an exception is NOT thrown - RubySundays - and over eating3 digit HTTP status codes - what are they, which are most common, which should be a concern?Ruby at both extremes of your websiteSigils - the characters on the start of variable names in Perl, Ruby and Fortran