Monday, October 31, 2011

Exercise 1: A Good First Program: Learn Ruby The Hard Way: Practicum

The whole point of the first exercise (Exercise 0, as befits a computer-centric topic; all arrays start at 0 :) ), was to install the gedit text editor, figure out how to work with it,  run some sample commands in irb, and navigate around the file system and directories. Again, for those of us who were using systems back in the early 80's or earlier, this interaction was the only way to deal with a computer (graphical interfaces would come later). It's good for people to get familiar with these tools because the book is focused on using these tools specifically, and not relying on anything that gives additional help or adds any wrappers.

The first real coding exercise is a simple one, and it deals with one object and the parameters for that object. This program has the user enter a number of "puts" statements. They may all look the same, but some are just a little bit different. Let's take a closer look at the exercise "by the book" (note, all pictures will enlarge if you click on them).

- Type the following into a single file named ex1.rb. This is important as Ruby works best with files ending in .rb.

- Then in Terminal run the file by typing:

ruby ex1.rb

What You Should See

$ ruby ex1.rb
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing.
I'd much rather you 'not'.
I "said" do not touch this.
[yep, I have a match :)]

If you have an error it will look like this:

 ruby ex1.rb
ex1.rb:4: syntax error, unexpected tCONSTANT, expecting $end
puts "This is fun."
[no error this time around]

Extra Credit

The exercises have "extra credit" scenarios, and yes, as I am trying to do these by the book, I will print my extra credit ideas (and yes, you can snicker at them all you want to, or write back to me how lame my approaches are, it's totally cool :) ).

- Make your script print another line.

- Make your script print only one of the lines.

- Put a '#' (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.

[See the next to last image and I've added a bunch of octothorpe characters explaining what they do and when they were put in and why. Long story short, they prevent a line from being interpreted by the ruby interpreter. They are good for entering in comments or stopping certain lines from being run.]

So yeah, pretty master of the obvious kind of stuff, huh? The funny thing is, most people do really well this early in the game because, well, it's basic and it's easy and they get bored really quickly. The early going is very simple. Misleadingly so. It's kind of like skiing. Learning how to snowplow is quick and basic and you can learn how to do it in a few minutes and ski the bunny hill with no problems. All feels perfectly normal, until you get on the intermediate slope and realize "oh wow, this suddenly got a lot tougher!" Coding is the same. Don't get complacent in the very early stages, it's deceptively easy, but many important fundamental practices take root here. Pay attention to them and the jump to later programming is not so painful or so abrupt. Work through the simple examples and pay closer attention to why you are being asked to look at these super simple examples.
Post a Comment