Teaching myself Ruby

Since Ruby is the new LISP, I’ve decided to use it to implement the brains behind ferrety.

To prove that it does work, here’s my test output with debugging info turned on:

SavageChicken 20% ./ferrety.rb
name: Test1
variability: 20
started: 1312586729
mood: happy
20% variability and a d100 roll of46
20% variability and a d100 roll of14
New mood is scared.
20% variability and a d100 roll of74
20% variability and a d100 roll of55
20% variability and a d100 roll of17
New mood is hurt.
20% variability and a d100 roll of35
20% variability and a d100 roll of97
20% variability and a d100 roll of36
20% variability and a d100 roll of45
20% variability and a d100 roll of13
New mood is sleepy.

And here’s the Ruby code:

#!/usr/bin/env ruby

# TODO these need to be made into command line parameters
$Debug = true
$Delay = 2

class Ferrety
	attr_accessor :name, :mood, :posture, :face, :eyes, :started, :posturesArt, :facesArt, :eyesArt, :variability

	def initialize(name, mood = :happy, variability = 20)
		@name        = name
		@mood        = mood
		@started     = Time.now.to_i
		@variability = variability
	end

	def to_s
		"Ferrety " + @name + $Debug ? ", started at #{@started}" : ""
	end

	def dump_data
		instance_variables.each do |variable|
			variable = variable.sub('@','')
			puts variable + ": " + send(variable).to_s + "\n"
		end
	end

	# Personality
	def think
		sleep($Delay)
		
		# Will the mood change?
		change = rand(100)
		puts "#{@variability}% variability and a d100 roll of#{change}\n" if $Debug 
		if change < @variability
			# mood changes

			case @mood
			when :content
				dest_moods = [ :happy, :sleepy, :affectionate, :mischeivous, :playful, :antsy ]
			when :happy
				dest_moods = [ :content, :lonely, :playful, :antsy, :scared, :hurt, :excited, :affectionate, :mischeivous, :tired ]
			when :lonely
				dest_moods = [ :happy, :sad, :antsy, :scared, :angry, :hurt, :affectionate, :sleepy ]
			when :sad
				dest_moods = [ :happy, :lonely, :scared, :angry, :hurt, :affectionate, :sleepy ]
			when :scared
				dest_moods = [ :sad, :lonely, :antsy, :angry, :hurt, :affectionate, :mischievous, :sleepy ]
			when :antsy
				dest_moods = [ :happy, :playful, :angry, :excited, :affectionate, :mischievous ]
			when :playful
				dest_moods = [ :happy, :content, :antsy, :scared, :angry, :excited, :affectionate, :mischievous, :tired, :sleepy ]
			when :angry
				dest_moods = [ :sad, :lonely, :antsy, :hurt, :excited, :mischievous, :tired ]
			when :hurt
				dest_moods = [ :lonely, :scared, :angry, :sleepy ]
			when :sleepy
				dest_moods = [ :happy, :content, :sad, :lonely, :scared, :affectionate ]
			when :affectionate
				dest_moods = [ :happy, :content, :playful, :antsy, :excited, :mischievous, :tired, :sleepy ]
			when :excited
				dest_moods = [ :happy, :playful, :antsy, :scared, :angry, :affectionate, :mischievous, :tired ]
			when :mischievous
				dest_moods = [ :happy, :sad, :lonely, :playful, :antsy, :scared, :angry, :excited, :affectionate, :tired ]
			when :tired
				dest_moods = [ :happy, :content, :sad, :lonely, :scared, :hurt, :affectionate, :sleepy ]
			end
			@mood = dest_moods[rand(dest_moods.size)]
			puts "New mood is #{@mood}.\n" if $Debug
		end
	end
end

# Test code
ferrety = Ferrety.new("Test1")
ferrety.dump_data

# do 10 loops of think
(1..10).each do
	ferrety.think
end
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: