Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby

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 under
R111 - Ruby - Exceptions.
  [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
  [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
  [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)


Back to
Sundays - and over eating
Previous and next
or
Horse's mouth home
Forward to
Moving from scripting to Object Orientation in Python
Some other Articles
Python for loops - applying a temporary second name to the same object
Melksham to become a part of Trowbridge?
Light bulbs
Moving from scripting to Object Orientation in Python
Sorta sorting a hash, and what if an exception is NOT thrown - Ruby
Sundays - and over eating
3 digit HTTP status codes - what are they, which are most common, which should be a concern?
Ruby at both extremes of your website
Sigils - the characters on the start of variable names in Perl, Ruby and Fortran
3732 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2012: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3435_Sor ... -Ruby.html • PAGE BUILT: Tue Mar 13 06:02:37 2012 • BUILD SYSTEM: wizard