On 2007-05-07 20:37:31 -0700, Morton Goldberg <m_goldberg / ameritech.net> said:

> On May 7, 2007, at 10:05 PM, Greg wrote:
> 
>> On 2007-05-07 18:16:38 -0700, Tim Hunter <TimHunter / nc.rr.com> said:
>> 
>>> Greg wrote:
>>>> A newbie here trying to develop one of Pine's tutorial scripts.  Ruby v1.8.6
>>>> I get different results from my script in Terminal, TextMate, and  
>>>> BBEdit. How is this possible?
>>>> TextMate seems to be the closest to what I'd expect. I'd buy it  if the 
>>>> undo's were better. Plus I already own BBEdit. I realize  BBEdit 
>>>> doesn't profess to support Ruby script running that much,  but I 
>>>> expected Terminal to be consistent.
>> 
>> #!/usr/bin/env ruby
>> 
>> class OrangeTree
>> 
>> MSG_GROW = "Type \"year\" to grow your tree."
>> MSG_PICK = "Type a number to pick some more fruit. "
>> EXIT_TXT = "Something went wrong."
>> 
>> def initialize # we have one tree, does it need a name?
>> 	@heightInches = 0 # at year zero
>> 	@age = 0
>> 	@fruit = 0 # inches, work out feet and inches later
>> 	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
>> 	 	@heightInches = @heightInches +1
>> 	 	puts 'Height: ' + @height.to_s
>> 	@age += 1
>> 		# @age = @age + 1
>> 	 	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(" Age: #{@age}. Place holder until get 1..3  
>> working." )
>> 			 	when (30) :    puts("Your tree was very fruitful, but it  reached 
>> old age and died.")
>> 				else puts( " Something went wrong. #{EXIT_TXT}" )
>> 			end
>> 	end # def ageOneYear
>> 	
>> 	def height
>> 		# returns the height	  	end
>> 	
>> 	def pick_an_orange
>> 	 puts 'Got to pick_an_orange, but haven\'t defined yet'
>> 	  # reduce this year count by one
>> 	end
>> 	
>> end # class OrangeTree
>> 
>> # here we go countLoop = 0
>> tree = OrangeTree.new # assume we need to initialize it. Does it  need a name?
>> 
>> while countLoop < 100
>> 	countLoop += 1
>> 	puts "countLoop = #{countLoop}"  # debugging.
>> 	if gets.to_s=='year' 			puts 'In the gets.to_s if clause' #  
>> debugging. 			tree.ageOneYear 		
>> 	end
>> 	
>> 	gets case 		when 'year' : tree.ageOneYear # , y as shortcut?
>> 		when (1..100) : tree.pick_an_orange
>> 		else puts('Don\'t be greedy, don\'t try to pick more than 100  oranges')
>> 	end
>> 	
>> end # while
>> 
> 
> There seems to be two problems with your code.
> 
> 1. You use @height in one place where really want @heightInches.

Duh. Thanks.

> 2. Your case statement in the your while loop just doesn't do what  you 
> think it should. And actually it would be hard make a case work  in the 
> situation you have set up.
> 
> Here is a modification of your code that I think will do what you  were 
> trying to achieve:
> 
> <code>
> #!/usr/bin/env ruby
> 
> class OrangeTree
> 
>     MSG_GROW = "Type \"year\" to grow your tree."
>     MSG_PICK = "Type a number to pick some more fruit. "
>     EXIT_TXT = "Something went wrong."
> 
>     def initialize
>        @heightInches = 0
>        @age = 0
>        @fruit = 0
>        puts MSG_GROW
>     end
> 
>     def ageOneYear
>        @heightInches = @heightInches +1
>        puts 'Height: ' + @heightInches.to_s # <= not '@height'
>        @age += 1
>        # @age = @age + 1
>        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(" Age: #{@age}. Place holder until get  1..3 
> working." )
>        when (30) :    puts("Your tree was very fruitful, but it  
> reached old age and died.")
>        else puts( " Something went wrong. #{EXIT_TXT}" )
>        end
>     end
> 
>     def height
>        # returns the height
>     end
> 
>     def pick_an_orange
>        puts 'Got to pick_an_orange, but haven\'t defined yet'
>        # reduce this year count by one
>     end
> 
> end
> 
> countLoop = 0
> tree = OrangeTree.new
> 
> while countLoop < 100
>     countLoop += 1
>     puts "countLoop = #{countLoop}"
>     # case statement not really appropriate for this loop.
>     # Note: gets always returns a string terminated with a newline.
>     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
>     else puts('Don\'t be greedy, don\'t try to pick more than 100  oranges')
>     end
> end
> </code>
> 
> Regards, Morton

No it's not working like I want. But I don't understand why case isn't 
appropriate. But your method works right and mine doesn't, so I don't 
think I'll worry about it now.

One question. How is
>  elsif (1..100).include?(user_input.to_i)
working? I understand it up through "elsif(1..100)" Is the user_input 
changed to integer before the elsif is evaluated. What does the 
include? do? (The don't be greedy part was just placeholder until I 
worked out the whole orange picking and growing logic.)

BTW, the while loop seemed like a crude work around to keep asking for 
inputs. I would like it to stop at 30 in the class (at least that seems 
more elegant as that is where the age decisions are made).

Thanks again. Lots to learn (and remember).