Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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 as below, or enter http://melksh.am/nnnn for individual articles
R118 - Ruby Sources and Resources
  [550] 2006 - Making business a pleasure - (2006-01-01)
  [2610] Cheat Sheet - what do you need for Ruby on Rails? - (2010-01-31)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [3426] Automed web site testing scripted in Ruby using watir-webdriver - (2011-09-09)
  [3622] Loading Ruby classes - where does Ruby look? - (2012-02-24)
  [3783] Load path, load and require in Ruby, and a change from 1.8 to 1.9 - (2012-06-24)

R111 - Ruby - Exceptions.
  [1875] What are exceptions - Python based answer - (2008-11-08)
  [2615] String to number conversion with error trapping in Ruby - (2010-02-01)
  [2620] Direct access to object variable (attributes) in Ruby - (2010-02-02)
  [2622] Handling unusual and error conditions - exceptions - (2010-02-03)
  [3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
  [3260] Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23)
  [3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
  [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
  [4675] Exceptions in Ruby - throwing, catching and using - (2016-05-17)

R107 - Collections (Arrays and Hashes) in Ruby
  [991] Adding a member to a Hash in Ruby - (2006-12-16)
  [2291] Collection objects (array and hash) in Ruby - (2009-07-16)
  [2606] Sorting arrays and hashes in Ruby - (2010-01-30)
  [2618] What are Ruby Symbols? - (2010-02-02)
  [2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
  [4502] Reading and parsing a JSON object in Ruby - (2015-06-01)


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
4759 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, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3435_Sor ... -Ruby.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb