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:
What You Should See
$ ruby ex1.rb
I like typing this.
This is fun.
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:
ex1.rb:4: syntax error, unexpected tCONSTANT, expecting $end
puts "This is fun."
[no error this time around]
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