T
Thufir
I'm getting keyboard input successfully, but I'm getting a string. I
need to convert it to a number. But ruby is a dynamic language?
What's happening please?
(Did I mention that this list is awesome?)
Here's what I have:
C:\code>
C:\code>
C:\code>dir
Volume in drive C has no label.
Volume Serial Number is 0491-510F
Directory of C:\code
11/03/2007 12:15 PM <DIR> .
11/03/2007 12:15 PM <DIR> ..
11/03/2007 11:42 AM 827 Creature.rb
11/03/2007 11:35 AM 122 doFactorial.rb
11/03/2007 11:58 AM 449 Dragon.rb
11/03/2007 11:34 AM 114 factorial.rb
11/03/2007 11:32 AM 353 Hello.rb
11/03/2007 12:17 PM 71 input.rb
11/03/2007 12:25 PM 283 makeDragon.rb
7 File(s) 2,219 bytes
2 Dir(s) 28,967,956,480 bytes free
C:\code>
C:\code>type Dragon.rb
require 'Creature'
class Dragon < Creature
life 1340 # tough scales
strength 451 # bristling veins
charisma 1020 # toothy smile
weapon 939 # fire breath
def toString()
print "\n\n\nThis Dragon\n"
print "-------------------"
print "\nLife:\t\t"
print life
print "\nStrength:\t"
print strength
print "\nCharisma:\t"
print charisma
print "\nWeapon:\t\t"
print weapon
print "\n\n\n\n"
end
end
C:\code>
C:\code>type Creature.rb
# The guts of life force within Dwemthy's Array
class Creature
# Get a metaclass for this class
def self.metaclass; class << self; self; end; end
# Advanced metaprogramming code for nice, clean traits
def self.traits( *arr )
return @traits if arr.empty?
# 1. Set up accessors for each variable
attr_accessor *arr
# 2. Add a new class method to for each trait.
arr.each do |a|
metaclass.instance_eval do
define_method( a ) do |val|
@traits ||= {}
@traits ||= {}
@traits[a] = val
end
end
end
# 3. For each monster, the `initialize' method
# should use the default number for each trait.
class_eval do
define_method( :initialize ) do
self.class.traits.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
end
end
# Creature attributes are read-only
traits :life, :strength, :charisma, :weapon
end
C:\code>
C:\code>type factorial.rb
def fact(n)
if n <= 1
1
else
n * fact(n - 1)
end
end
C:\code>
C:\code>type makeDragon.rb
require 'Dragon'
require 'factorial'
aDragon = Dragon.new
aDragon.toString
number=0
print "\n\n\nthe Dragon will calculate a factorial now."
puts "\nenter an integer "
number=gets
# print "\n\n\n" + number + "\n\n"
print "Dragon replies\n\n"
print fact(number)
C:\code>
C:\code>
C:\code>makeDragon.rb
This Dragon
-------------------
Life: 1340
Strength: 451
Charisma: 1020
Weapon: 939
the Dragon will calculate a factorial now.
enter an integer
9
Dragon replies
../factorial.rb:2:in `<=': comparison of String with 1 failed
(ArgumentError)
from ./factorial.rb:2:in `fact'
from C:/code/makeDragon.rb:16
C:\code>
C:\code>
thanks,
Thufir
need to convert it to a number. But ruby is a dynamic language?
What's happening please?
(Did I mention that this list is awesome?)
Here's what I have:
C:\code>
C:\code>
C:\code>dir
Volume in drive C has no label.
Volume Serial Number is 0491-510F
Directory of C:\code
11/03/2007 12:15 PM <DIR> .
11/03/2007 12:15 PM <DIR> ..
11/03/2007 11:42 AM 827 Creature.rb
11/03/2007 11:35 AM 122 doFactorial.rb
11/03/2007 11:58 AM 449 Dragon.rb
11/03/2007 11:34 AM 114 factorial.rb
11/03/2007 11:32 AM 353 Hello.rb
11/03/2007 12:17 PM 71 input.rb
11/03/2007 12:25 PM 283 makeDragon.rb
7 File(s) 2,219 bytes
2 Dir(s) 28,967,956,480 bytes free
C:\code>
C:\code>type Dragon.rb
require 'Creature'
class Dragon < Creature
life 1340 # tough scales
strength 451 # bristling veins
charisma 1020 # toothy smile
weapon 939 # fire breath
def toString()
print "\n\n\nThis Dragon\n"
print "-------------------"
print "\nLife:\t\t"
print life
print "\nStrength:\t"
print strength
print "\nCharisma:\t"
print charisma
print "\nWeapon:\t\t"
print weapon
print "\n\n\n\n"
end
end
C:\code>
C:\code>type Creature.rb
# The guts of life force within Dwemthy's Array
class Creature
# Get a metaclass for this class
def self.metaclass; class << self; self; end; end
# Advanced metaprogramming code for nice, clean traits
def self.traits( *arr )
return @traits if arr.empty?
# 1. Set up accessors for each variable
attr_accessor *arr
# 2. Add a new class method to for each trait.
arr.each do |a|
metaclass.instance_eval do
define_method( a ) do |val|
@traits ||= {}
@traits ||= {}
@traits[a] = val
end
end
end
# 3. For each monster, the `initialize' method
# should use the default number for each trait.
class_eval do
define_method( :initialize ) do
self.class.traits.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
end
end
# Creature attributes are read-only
traits :life, :strength, :charisma, :weapon
end
C:\code>
C:\code>type factorial.rb
def fact(n)
if n <= 1
1
else
n * fact(n - 1)
end
end
C:\code>
C:\code>type makeDragon.rb
require 'Dragon'
require 'factorial'
aDragon = Dragon.new
aDragon.toString
number=0
print "\n\n\nthe Dragon will calculate a factorial now."
puts "\nenter an integer "
number=gets
# print "\n\n\n" + number + "\n\n"
print "Dragon replies\n\n"
print fact(number)
C:\code>
C:\code>
C:\code>makeDragon.rb
This Dragon
-------------------
Life: 1340
Strength: 451
Charisma: 1020
Weapon: 939
the Dragon will calculate a factorial now.
enter an integer
9
Dragon replies
../factorial.rb:2:in `<=': comparison of String with 1 failed
(ArgumentError)
from ./factorial.rb:2:in `fact'
from C:/code/makeDragon.rb:16
C:\code>
C:\code>
thanks,
Thufir