8/1000: babyDragon.rb | undefined method `+’ for nil:NilClass (NoMethodError)

Houston, we’ve got a bug…

the following is the result of a command line run of babyDragon.rb

HAKEON-MacBook-Pro:ruby HAKEON$ ruby babyDragon.rb

babyDragon.rb:52: warning: found = in conditional, should be ==

What would you like to name your dragon?


Ted is born.

You feed Ted.

babyDragon.rb:87:in `passage_of_time': undefined method `+’ for nil:NilClass (NoMethodError)

from babyDragon.rb:32:in `feed’

from babyDragon.rb:118:in `<main>’

and this is the code

class Dragon

def initialize name
@name = name
@asleep = false
@stuff_in_belly = 10 #he is full
@stuff_in_intestive = 0 #he does not need to go

puts “#{@name} is born.”

def feed
puts “You feed #{@name}.”
@stuff_in_belly = 10

def walk
puts “You walk #{@name}.”
@stuff_i_intestine = 0

def put_to_bed
puts “You put #{@name} to bed.”
@asleep = true
3.times do
if @asleep
if @asleep
puts “#{@name} snores, filling the room with smoke.”
if @asleep = false
puts “#{@name} wakes up slowly.”

def toss
puts “You toss #{@name} up into the air.”
puts “He giggles, which singes your eyebrows.”

def rock
puts “You rock #{@name} gently.”
@asleep = true
puts “He briefly dozes off…”
if @asleep
@asleep = false
puts “…but wakes when you stop.”


def hungry?
@stuff_in_belly <= 2

def poopy?
@stuff_in_intestine >= 8

def passage_of_time
if @stuff_in_belly > 0 || @stuff_in_intestine < 10
@stuff_in_belly = @stuff_in_belly – 1
@stuff_in_intestine = @stuff_in_intestine + 1
if @asleep
@asleep = false
puts “He wakes up suddenly!”
puts “#{@name} is starving! In desperation, he ate YOU!”
exit #terminates the program

if hungry?
if @asleep
@asleep = false
puts “He wakes up suddenly!”
puts “#{@name}’s stomach grumbles…”

if poopy?
if @alseep
@asleep = false
puts “He wakes up suddenly!”
puts “#{@name} does the potty dance.”

puts “What would you like to name your dragon?”
name = gets.chomp
pet = Dragon.new name

# Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby)
# (Kindle Locations 3137-3142). Pragmatic Bookshelf. Kindle Edition.

7/1000: YAML & emacs

This is another Chris Pine exclusive, but this time I wrote the code via emacs… yes, I expect applause… thank you

# YAML is not actually part of the Ruby core (it is its own thing, and many other languages can use YAML),
# but it is part of the standard distribution. What does that mean? Well, when you install Ruby,
# you install YAML, too. But if you want to actually use YAML, you’ll need to import it into your program.
# This is really easy, though, with the require method.

# Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby)
# (Kindle Locations 2543-2546). Pragmatic Bookshelf. Kindle Edition.

require ‘yaml’

test_array = ["No success like excess.",
"If you can't make it good, make it big.",
"If you can't make it big, make it red."]

test_string = test_array.to_yaml

filename = ‘greatArtAdvice.txt’

File.open filename, ‘w’ do |f|
f.write test_string

read_string = File.read filename

read_array = YAML::load read_string

puts(read_string == test_string)
puts(read_array == test_array)

6/1000: pineQuote.rb | Saving and Loading for Grown-ups

# (a quote from Chris)
# Now that you’re good and afraid, let’s get to it.
# A file is basically just a sequence of bytes.
# A string is also, ultimately , just a sequence of bytes.
# This makes saving strings to files pretty easy, at least conceptually.
# (And Ruby makes it pretty easy in practice.)
# Here’s a quick example where we save a simple string to a file and
# then read it back out again.
# Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby)
# (Kindle Locations 2508-2511). Pragmatic Bookshelf. Kindle Edition.

#variables for a text file and for a string
filename = ‘PineQuote.txt’
test_string = ‘Now that you’re good and afraid, ‘ +
‘let’s get to it.’

#save to file. the ‘w’ is for write-access to the file
File.open “filename”, “w” do |f|
f.write test_string
end #ruby closes filename with the end

read_string = File.read(“filename”) #load to variable read_string
puts(read_string == test_string) #test open/close/load algo

Github | hakeon/pinteresting

As I process through Mattel Griffel’s OneMonthRails course, my work will be publicly available via github repository hakeon/pineresting.  There are a few repositories under my account, all from various efforts to learn this stuff.  It’s funny, but I keep repeating the basics, sometimes in this language – some times in that, but it is always valuable to experience the fundamentals over and over again, from many sources.  Though of course the goal is to get out of the novice (nob) camp… someday…

One Month Rails | No Degree, No Problem

So tip-o-the-hats to Josh Kemp and Mattan Griffel for my basic, getting started guides.  In fact, I am following Josh Kemp’s roadmap as best I can and so far his recommendations are solid.  Today, I started Mattan Griffel’s One Month Rails course and I must say he has made the frustrating chore of installing rails pretty easy.  If anything, that’s the first hurdle.

It’s only fair to link to Josh’s book so go here and buy, No Degree, No Problem if programming is your dream to a better life but you don’t have a CS degree.

5/1000: landMass.rb | A Recursion Program

From Mr. Chris Pine’s wonderful instructional text “Learn to Program (Facets of Ruby):

“Let’s say we have an 11×11 world (represented as an array of arrays…basically just a grid)
and that we want to find the size of the continent in the middle (that is, the continent
of which tile (5,5) is a part). We don’t want to count any land tiles belonging to any of
the other continents. Also, as in Civilization III, we’ll say that tiles touching only at
the corners are still considered to be on the same continent (since units could walk along
diagonals). But before we get to the code, let’s solve the problem in English first. My
initial plan was to look at every tile on the map, and if that tile is a land tile on the
continent I’m looking for, we add 1 to the running total. The problem , though, is how do
we know whether a land tile is on the same continent as some other land tile? There are
ways to solve this problem, but they all seemed to be too messy; either I was keeping
track of lots of information I didn’t feel like I needed or I seemed to be doing the same
computation over and over again. But then I thought, hey, two tiles are on the same
continent if you can walk from one to the other. (That was essentially the operating
definition of continent in Civilization III.) So that’s how the code should work! First,
you count the spot you are standing on (duh); in this case, that means tile (5,5). Then,
you send out eight little guys, one in each direction, and tell them to count the rest of
the continent in that direction. The only rule is that no one can count a tile that
someone else has already counted. When those eight guys return, you add their answers to
your already-running total”

Pine, Chris (2009-03-28). Learn to Program (Facets of Ruby) (Kindle Locations 2109-2120).
Pragmatic Bookshelf. Kindle Edition.

# for ease of visual recognition:
M = ‘land’
o = ‘water’

world =[[o,o,o,o,o,o,o,o,o,o,o],

puts ”
puts “To calculate the land mass of your world,”
puts “please enter a positive integer for location x.”
while x < 0
puts “That’s not a positive integer… try again.”
puts “Please enter a positive integer for location y.”
while y < 0
puts “That’s not a positive integer… try again.”

def landMass world, x, y
if world[y][x] != ‘land’ # if it’s not land, we don’t count it
return 0
# count the first tile
world[y][x] = ‘counted land’
# now accumulate land mass (i.e. size) via recursion of the landMass method
size = size + landMass(world, x-1, y-1)
size = size + landMass(world, x, y-1)
size = size + landMass(world, x+1, y-1)
size = size + landMass(world, x-1, y)
size = size + landMass(world, x+1, y)
size = size + landMass(world, x-1, y+1)
size = size + landMass(world, x, y+1)
size = size + landMass(world, x+1, y+1)
puts “The land mass of your world is ” + landMass(world,x,y).to_s + “.”
puts ”