100 Hours of Testing Practice - 22 of 100 (Getting classy)

It was too easy, therefore I already know I'm missing a lot. Having said that, creating the Ruby Class I've just put together felt like the ones I wrote in Java a looong while ago, so maybe I have some ingrained learning I wasn't aware of ;p

The benefits of writing magic and slightly more complex code, verging on programmes instead of scripts, utilising the power of a Class or two instead of a Method or dozen is blatantly obvious. Well, at least when it's blatantly obvious I guess. In my view methods feel quite fixed and are very good for carrying out routine/batch tasks. You may need them several times and that's fine, call a method as needed, you can even change the values that the method takes to get some flexibility.

But what if you're doing something and would like the stuff in that method to be used a few times, at the same time? We might want to produce some data sets and then do something clever with them. We could call the method, populate some arrays, call another method that does the work then store the results. It all stars to get messy perdy quick.

.

Classes for teh w1n!

Classes are a beast unto themselves and powerful even in their simplest form. With a class you can define an object with certain characteristics and behaviours (via methods). You can instantiate it a few times (create an instance) and easily have a set of objects with similar characteristics and behaviours, that can exhibit them in different ways at the same time.

As always the first glitch was what to create a class about, living here in Málaga we have Puerto Marina a mile or so away near Benalmádena. Boats seemed a good topic, having a quick think about what to call the class I started writing code.

.

Class Boat

The first step is to declare a class and name it. naming starts with an uppercase letter. Now classes in Java have 'setters' and 'getters' (¡myutaytorz!) and Ruby is no different, except it's a bloomin' lot easier to set up.

Here we can see the class Boat being declared, inventive eh? ;) Next we see a great little Ruby wonderment the attr_accessor along with the accessor names. Those things with the @ next to them are instance variables, these are variables that will hold values unique to the instance (of the class) that we bring into life. Because they're hidden inside the instance we need a way to access them. Instead of whole bunch of setter getter declarations we use the attr_accessor instead.

When we then want to create an instance of our class we need to initialise some values for it, that's the next thing we do, with a special method called initialize that takes the values shown in the (brackets). To ensure our class knows what values to assign to the instance variables it contains we then do a little setting by showing @name = name, etc. where name is given in the parenthesis next to the method name.

Set-up some instances

Now we have our class to use as a template for each instance of it we want to set-up, we need to set-up some instances of it, here's what I came up with for our boats.

Here we see instances of the class, three of them for different boats in a catalogue perhaps. Each instance is assigned to a local variable and instantiated by using Boat.new, then declaring what values apply to each of the attributes we have for the class. You don't have to have values for everything either, some can be left blank. That makes sense, perhaps an attribute for a car, person, building, bank account, music track or whatever just doesn't apply.

Great, but this will all run and we'll see exactly nothing. If this is a catalogue then we probably want people to pick from it, so let's give them a way to do that. We need to consider that if they pick one of many options - what happens per option? We'll use a case statement to define a rule for each choice.

This gives us a way to get user input, via our previously used gets.chomp.downcase and hold the input value in the local variable called boat_wanted. Our case statement can then be told what to do when a certain input is received.

I ran this and Woops, I got ahead of myself. We'll still see nothing because a method for display_catalogue_item hasn't been declared, Ruby had no clue what I meant by"for sailboat run the display_catalogue_method". Thick as a plank these computers!

Let's display_catalogue_item

There are a number of places to put the display_catalogue_item method but directly in the Class was where I felt it belonged best.

A little problem

I wanted to print out the price with the £ symbol next to it. Entering that symbol directly within the code saw Ruby throw a tantrum. This is a really good example (on and on I go about this..) of how code explodes just because of a simple issue.

In the case of below, line 20 (see image above) had £ in there when what I needed was #{163.chr} instead.

Tut tut Ruby. Even with that fix the output was still not formatted correctly.

Various searches suggested I needed to add # coding: iso-8859-1 at the start of the script. I may have misunderstood this as it did squat piddly poo pah, it could of course be the way a CMD window handle's it. Either way, one to follow up later, it's not a killer for now ;)

.

As always, grab the classStudy.rb file here or from my Github repo at Script-Bucket.

.

Today's music was all David Gray, which for some reason always reminds me of Morrowind. I think I used to build levels while listening to this music. Ehh...good times. Funny how the brain works.

Views: 164

Add a Comment

You need to be a member of Software Testing Club - An Online Software Testing Community to add comments!

Join Software Testing Club - An Online Software Testing Community

Comment by sunil on February 10, 2012 at 11:16

great stuff ,thanks for sharing

Comment by Mark Crowther on February 3, 2012 at 16:13

Ooh, good to know. Bug fix added to the Github repo :D

Comment by phil kirkham on February 3, 2012 at 16:00

Thats for when you're displaying them in browsers, not on the command line which is boring old ASCII

http://ascii-table.com/ascii-extended-pc-list.php

Comment by Mark Crowther on February 3, 2012 at 15:52

Hiya Phil - interesting, thanks for sharing that, works on my system ;}

I got the numbers from various 8859-1 tables on the web, where 156 isn't even shown..hmm.

E.g. http://www.w3schools.com/tags/ref_entities.asp and http://en.wikipedia.org/wiki/ISO/IEC_8859-1

What's that mean then... Ruby isn't using 8859-1, the tables are out of date, something else?

Comment by phil kirkham on February 3, 2012 at 15:41

Where did you get 163 from ? I put 156 in and it prints out a £ - with no #coding needed at the top of the file.

 

To get the chars I did a simple loop

$i = 129;
while $i < $255  do
 #  puts("Inside the loop i = #$i #{$i.chr}" );
   $i +=1;
end

 

which showed all the characters and voila, there was the pound

Adverts

© 2017   Created by Rosie Sherry.   Powered by

Badges  |  Report an Issue  |  Terms of Service