Clear, concise examples - Ruby classes and objects.
Finding clear, concise examples of how to perform a simple programming task can be surprisingly difficult. "I just want a [Rails] model that doesn't actually use a database table" was an example yesterday. Such shouldn't be difficult, but authors tend to write a simple example and then decide
"While I'm at it, I could show X as well" and
"I should include Y because it's a good alternative" and
"I could be critisized for not covering Z so I'll cover it" ... and the result is an example which doesn't actually fulfill the original requirement of being clear and concise.
I fall into the same trap sometimes ...
On Thursday, I wrote an example to show how a class is defined, how an object is created within that class, and how the object is accessed. Good. And I then extended the example to show:
• how the class can be loaded from a separate file
• how we can use an array of objects
• how we can use a factory method to manufacture objects of a given type
• how we can include class variables and methods as well as object variables
• how we can extend the class to create other similar classes without repeating code
Fortunately, I saved a copy of my original (straightforward) example so that I can re-use it as a first "This is how I create and use an object in Ruby" example - it's
[here].
The extended copy (if you would like a comparison, and to move on to all the exciting stuff) is
[here], with the class code broken out into a separate file
[here].
Examples like this from our ...
Ruby Courses.
(written 2013-02-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R105 - Ruby - Classes and Objects [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
[2603] Ruby objects - a primer - (2010-01-29)
[2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
[2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
[2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
[3421] Ruby off the Rails? - (2011-09-06)
[4502] Reading and parsing a JSON object in Ruby - (2015-06-01)
R102 - Hello Ruby Objects [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
[4369] Ruby - the second rung of learning the language - (2014-12-28)
[4580] Easy example - data record to object and object to data record mapping in Ruby - (2015-11-21)
[4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
[4673] Separating detailed data code from the main application - Ruby example - (2016-05-16)
G504 - Well House Consultants - Writing Notes [4] Seeing the wood for the trees. - (2004-08-06)
[7] Writing on a Sunday - (2004-08-08)
[9] Study room - the Oxford train - (2004-08-10)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[398] Training course material - why we write our own - (2005-07-30)
[407] Theft of training material - (2005-08-09)
[797] Writing up new C / C++ notes. - (2006-07-09)
[979] Empty seats, Nodding Donkeys and buses - (2006-12-11)
[982] Notes from the white board - (2006-12-14)
[1950] Copyright of Training Notes and Web Site - (2008-12-18)
[2010] How long should a training module be? - (2009-01-27)
[2192] Copy writing - allowing for the cut - (2009-05-21)
[2414] Hello World - a good traditional start to a Java course - (2009-09-22)
[2481] Sample code with errors in it on our web site - (2009-10-29)
[2812] What is Perl? - (2010-06-15)
[2828] Sharing our programs - easy. Sharing our data - harder. - (2010-06-26)
[3152] Jargon busting - (2011-01-30)
[4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
Some other Articles
Web Frameworks - nested templatesA course is not just for a year - its for a careerFrom SalfordReally Simple RailsClear, concise examples - Ruby classes and objects.Reading and checking user inputs - first lessons - RubyWhich database should I use? MySQL v SQLiteRuby / SQLite3 example program, showing JOIN v LEFT JOINNew guest ... becoming returning guestAnnual Accounts - a big job for a small business