Morton Goldberg
5/8/2007 3:38:00 AM
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.
>>> I've driven myself crazy trying to debug my script, stalled in
>>> BBEdit, so went to Terminal, but after putting in all kinds of
>>> puts to figure out what was going on, tried my demo TextMate and
>>> it ran better. In all cases exactly the same file.
>>> Thanks for any clues.
>> It would help if you'd post your script, what answers you
>> expected, and what answers you got.
>
> Thanks for answering. Here it goes:
>
> #!/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
>
> TextMate response. I answered 'year' to each request for input:
> RubyMate r6354 running Ruby r1.8.6 (/usr/local/bin/ruby)
>>>> OrangeTree.post.rb
>
> Congratulations, you planted an orange tree. In a few years it will
> start bearing fruit. 0 Type "year" to grow your tree.
> countLoop = 1
> Height: Got to ageOneYear. Age: 1
> Your 1 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 2
> Height: Got to ageOneYear. Age: 2
> Your 2 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 3
> Height: Got to ageOneYear. Age: 3
> Your 3 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 4
> Height: Got to ageOneYear. Age: 4
> Age: 4. Place holder until get 1..3 working.
> NoMethodError: undefined method `+' for nil:NilClass
> method gets
> in stdin_dialog.rb at line 6
> method gets
> in stdin_dialog.rb at line 13
> at top level
> in OrangeTree.post.rb at line 58
> ==END TextMate response
>
> Terminal response. All 'year' are my responses. I stopped after two
> none reponses.
> new-host-2:~ xxxxxx$ ruby -v
> ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.9.0]
> new-host-2:~ xxxxxx$ ruby "/Volumes/share/Greg/Ruby++/
> OrangeTree.post.rb"
> Congratulations, you planted an orange tree. In a few years it will
> start bearing fruit. 0 Type "year" to grow your tree.
> countLoop = 1
> year
> Height: Got to ageOneYear. Age: 1
> Your 1 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> year
> year
> ===End Terminal
>
> BBEdit output. I only Cmd-R to run the script. I was never asked
> for input.
>
> Congratulations, you planted an orange tree. In a few years it will
> start bearing fruit. 0 Type "year" to grow your tree.
> countLoop = 1
> Height: Got to ageOneYear. Age: 1
> Your 1 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 2
> Height: Got to ageOneYear. Age: 2
> Your 2 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 3
> Height: Got to ageOneYear. Age: 3
> Your 3 year old tree is too young to bear fruit yet. Type "year" to
> grow your tree.
> countLoop = 4
> Height: Got to ageOneYear. Age: 4
> Age: 4. Place holder until get 1..3 working.
> countLoop = 5
> Height: Got to ageOneYear. Age: 5
> Age: 5. Place holder until get 1..3 working.
> <SNIP-SNIP>
> countLoop = 100
> Height: Got to ageOneYear. Age: 100
> Something went wrong. Something went wrong.
> ===END BBEdit
> TextMate was close, but I expected values for @heightInches to
> increase with each iteration.
> I would like it to work in BBEdit, but realize BareBones support
> for this is weak. But at least Terminal should get it right. I own
> BBEdit, so am reluctant to buy TextMate, particularly since it has
> significant features I would like missing.
> Thanks for any help.
> I hope this isn't a double posting. My original was via Usenet,
> then I found the Google Groups and tried to post there, but I am
> not sure I am a member there. This version may be somewhat
> different as I had to recreate it after realizing Google Groups
> wasn't working for me.
There seems to be too problems with your code.
1. You use @height in one place where really want @heightInches.
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