Passing info to classes and methods

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
 
R

Rob Biedenharn

def pick_an_orange numToPick
puts 'Got to pick_an_orange, now trying to get it working.
numToPick: #{numToPick}' # debugging

Did you mean to have puts "Got ... #{numToPick}" in "double-quotes"
to interpolate the #{numToPick}. In single quotes, you'll get
literally:

Got to pick_an_orange, now trying to get it working. numToPick: #
{numToPick}

Does that help you?

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
G

Greg

Did you mean to have puts "Got ... #{numToPick}" in "double-quotes" to
interpolate the #{numToPick}. In single quotes, you'll get literally:

Got to pick_an_orange, now trying to get it working. numToPick: # {numToPick}

Does that help you?

-Rob

Thanks, I missed that I had single quotes. I think I'm learning that
double quotes are safer. For some reason Pine uses single quotes. I
mainly need to get used to reading Ruby so I can see errors.
Practice...
 
R

rgossen

Thanks, I missed that I had single quotes. I think I'm learning that
double quotes are safer. For some reason Pine uses single quotes. I
mainly need to get used to reading Ruby so I can see errors.
Practice...

I'm a newb as well so take this for what it's worth.

My understanding is that the Ruby interpreter handles single quotes
faster than double quotes, so you should use single quotes if doubles
aren't necessary. I don't know how appreciable the speed difference
is, or if that difference is worth potentially causing the kind of
problem that you encountered.
 
A

Alex Young

rgossen said:
I'm a newb as well so take this for what it's worth.

My understanding is that the Ruby interpreter handles single quotes
faster than double quotes, so you should use single quotes if doubles
aren't necessary. I don't know how appreciable the speed difference
is, or if that difference is worth potentially causing the kind of
problem that you encountered.
Measure it (that's what the Benchmark library is for). You'll be surprised.

To cut a long story short, the only difference between single- and
double-quoted strings is in the parsing. In practice, you won't notice
a difference. The most important practical use for single-quotes over
double-quotes (at least in my book) is to signal to the developer
whether there should be anything interesting inside the string, rather
than any performance considerations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top