# Passing info to classes and methods

**URL:** <https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670>\
**Category:** Ruby\
**Created:** [May 9, 2007, 3:11am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670 "2007-05-09T03:11:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 9, 2007, 3:11am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/1 "2007-05-09T03:11:19Z")

</div>

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.

`
#!/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
`

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

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 9, 2007, 3:14am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/2 "2007-05-09T03:14:19Z")

</div>

In message [2007050818053316807-email@linkLINEcom](mailto:2007050818053316807-email@linkLINEcom), Greg writes:

> I want to pass numToPick to pick\_an\_orange via ‘tree.pick\_an\_orange  
> user\_input.to\_i’ but it’s not happening.

Well, what does happen? Do you get nothing?

What happens if you just write “tree.pick\_an\_orange 5”?

-s

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 9, 2007, 3:48am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/3 "2007-05-09T03:48:23Z")

</div>

On May 8, 2007, at 9:10 PM, Greg wrote:

> 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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 9, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/4 "2007-05-09T20:01:09Z")

</div>

On May 9, 12:22 am, Greg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Did you mean to have puts “Got … #{numToPick}” in “double-quotes” to  
> > 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.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 10, 2007, 2:50am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/5 "2007-05-10T02:50:13Z")

</div>

rgossen wrote:

> > > interpolate the #{numToPick}. In single quotes, you’ll get literally:  
> > > 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.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [May 9, 2007, 8:25am UTC](https://www.ruby-forum.com/t/passing-info-to-classes-and-methods/103670/6 "2007-05-09T08:25:44Z")

</div>

On 2007-05-08 18:47:50 -0700, Rob B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> 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…
