G
Greg
Newbie here. Two steps forward and one back. I'm working through Pine's
book and maybe am jumping into areas beyond my knowledge. Some of you
have seen an earlier version of this script. I'm trying to pass a value
to a method in a class and the value isn't getting there.
<code>
#!/usr/bin/env ruby
class OrangeTree
MSG_GROW = "Type \"year\" to grow your tree."
MSG_PICK = "Type a number to pick some oranges. "
EXIT_TXT = "Something went wrong."
def initialize # we have one tree, does it need a name?
@heightInches = 0 # at year zero inches, work out feet and inches later
@age = 0
@fruit = 0
numToPick = 0
puts "Congratulations, you planted an orange tree. In a few years it
will start bearing fruit. #{@age} " # Age only for debugging. Can take
it out.
puts MSG_GROW
end
def ageOneYear
# gain in height, , lose old fruit, grow more fruit each year (after
say four years), die (at 30),
# Height: #exponential, just for the heck of it.
@age += 1
@heightInches = @heightInches + 1
puts 'Height: ' + @heightInches.to_s
if @age>3 : @orange = 100 end # Make a more sophisticated number of
fruit method
# puts "Got to ageOneYear. Age: #{@age}"
case( @age )
when (1..3) : puts("Your #{@age} year old tree is too young to
bear fruit yet. #{MSG_GROW}" )
# when (4..29) : puts("It's Tuesday. Age: #{@age}. Place holder
until get 1..3 working." )
when (4..29) : puts("Your tree is #{@age} years old.
#{MSG_PICK} or #{MSG_GROW}" )
when (30) : puts("Your tree was very fruitful, but it reached
old age and died.")
break # maybe this will do it for killing the tree.
else puts( " Something went wrong. #{EXIT_TXT}" )
end
puts # for blank line
end # def ageOneYear
def height
# returns the height
end
def pick_an_orange numToPick
puts 'Got to pick_an_orange, now trying to get it working. numToPick:
#{numToPick}' # debugging
if @fruit < numToPick then ("You tried to pick more oranges than were
on the tree, so you picked #{@fruit} oranges. #{MSG_GROW} ")
# @fruit is negative, but I don't think there's a reason to reset it
else
@fruit = @fruit - numToPick
puts ("You picked #{numToPick} oranges. You have #{@fruit} left.
You can #{MSG_PICK} or #{MSG_GROW}" )
end
puts # for blank line
end # pick_an_orange
end # class OrangeTree
# here we go
countLoop = 0
tree = OrangeTree.new # assume we need to initialize it. Does it need a name?
while countLoop < 31
countLoop += 1
puts "countLoop = #{countLoop}" # debugging
user_input = gets.chomp
if user_input == 'year'
tree.ageOneYear
elsif (1..100).include?(user_input.to_i) # need to convert string
to integer
tree.pick_an_orange user_input.to_i
else puts('Don\'t be greedy, don\'t try to pick more than 100 oranges')
end
end
</code>
I want to pass numToPick to pick_an_orange via 'tree.pick_an_orange
user_input.to_i' but it's not happening.
Thanks for any ideas
book and maybe am jumping into areas beyond my knowledge. Some of you
have seen an earlier version of this script. I'm trying to pass a value
to a method in a class and the value isn't getting there.
<code>
#!/usr/bin/env ruby
class OrangeTree
MSG_GROW = "Type \"year\" to grow your tree."
MSG_PICK = "Type a number to pick some oranges. "
EXIT_TXT = "Something went wrong."
def initialize # we have one tree, does it need a name?
@heightInches = 0 # at year zero inches, work out feet and inches later
@age = 0
@fruit = 0
numToPick = 0
puts "Congratulations, you planted an orange tree. In a few years it
will start bearing fruit. #{@age} " # Age only for debugging. Can take
it out.
puts MSG_GROW
end
def ageOneYear
# gain in height, , lose old fruit, grow more fruit each year (after
say four years), die (at 30),
# Height: #exponential, just for the heck of it.
@age += 1
@heightInches = @heightInches + 1
puts 'Height: ' + @heightInches.to_s
if @age>3 : @orange = 100 end # Make a more sophisticated number of
fruit method
# puts "Got to ageOneYear. Age: #{@age}"
case( @age )
when (1..3) : puts("Your #{@age} year old tree is too young to
bear fruit yet. #{MSG_GROW}" )
# when (4..29) : puts("It's Tuesday. Age: #{@age}. Place holder
until get 1..3 working." )
when (4..29) : puts("Your tree is #{@age} years old.
#{MSG_PICK} or #{MSG_GROW}" )
when (30) : puts("Your tree was very fruitful, but it reached
old age and died.")
break # maybe this will do it for killing the tree.
else puts( " Something went wrong. #{EXIT_TXT}" )
end
puts # for blank line
end # def ageOneYear
def height
# returns the height
end
def pick_an_orange numToPick
puts 'Got to pick_an_orange, now trying to get it working. numToPick:
#{numToPick}' # debugging
if @fruit < numToPick then ("You tried to pick more oranges than were
on the tree, so you picked #{@fruit} oranges. #{MSG_GROW} ")
# @fruit is negative, but I don't think there's a reason to reset it
else
@fruit = @fruit - numToPick
puts ("You picked #{numToPick} oranges. You have #{@fruit} left.
You can #{MSG_PICK} or #{MSG_GROW}" )
end
puts # for blank line
end # pick_an_orange
end # class OrangeTree
# here we go
countLoop = 0
tree = OrangeTree.new # assume we need to initialize it. Does it need a name?
while countLoop < 31
countLoop += 1
puts "countLoop = #{countLoop}" # debugging
user_input = gets.chomp
if user_input == 'year'
tree.ageOneYear
elsif (1..100).include?(user_input.to_i) # need to convert string
to integer
tree.pick_an_orange user_input.to_i
else puts('Don\'t be greedy, don\'t try to pick more than 100 oranges')
end
end
</code>
I want to pass numToPick to pick_an_orange via 'tree.pick_an_orange
user_input.to_i' but it's not happening.
Thanks for any ideas