Error with virtual tree program ruby

时间:2015-07-28 15:56:57

标签: ruby-on-rails ruby initialization

I have just started learning ruby and have made this program following a tutorial.

I keep getting an error when trying to run and can't find an answer. The program is suppose to be able to pick fruit, count the fruit, give the height and grow.

C:\Sites\testfolder>ruby orangetree.rb
orangetree.rb:2:in `initialize': wrong number of arguments (1 for 0) (ArgumentEr
ror)
    from orangetree.rb:51:in `new'
    from orangetree.rb:51:in `<class:OrangeTree>'
    from orangetree.rb:1:in `<main>'

C:\Sites\testfolder>

Here is the program

class OrangeTree
  def initialize

  @age = 0
  @tall = 0
  @fruit = 0

  puts 'You have planted a new tree!'

  def height
    puts 'The tree is ' + @tall.to_s + 'foot tall.'
  end

  def pickAFruit
if @fruit <= 1
  puts 'There is not enough fruit to pick this year.'
else
  puts 'You pick an orange from the tree.'
  @fruit = @fruit - 1
end
end

  def countOranges
    puts 'The tree has ' + @fruit.to_s + 'pieces of fruit'
  end

  def oneYearPasses
@age = @age + 1
@tall = @tall + 3
@fruit = 0

  if dead?
    puts 'The Orange Tree Dies'
    exit
  end

  if @age > 2
  @fruit = @age*10
  else
  @fruit = 0
  end
end

private

def dead?
  @age > 5
end

end

tree = OrangeTree.new 'tree'
command = ''

while command != 'exit'
  puts 'please enter a command for the virual tree'
  command = gets.chomp
  if command == 'tree height'
    tree.height
  elsif command == 'pick fruit'
    tree.pickAFruit
  elsif command == 'wait'
    tree.oneYearPasses
  elsif command == 'count fruit'
    tree.countOranges
  elsif command == 'exit'
    exit
  else
    puts 'Cant understand your command, try again'
  end
end
end

Can anybody help?

1 个答案:

答案 0 :(得分:2)

You have some syntax errors. I have fixed them below. The syntax errors were:

  • You had an extra end on the last line (for closing the class declaration)
  • You were passing an argument to OrangeTree.new ("tree") that was unexpected. This is what the wrong number of arguments (1 for 0) error message in your question is referring to.
  • You were missing an end to close your initialize method declaration (after the puts 'You have planted a new tree!' line)

I have also fixed the indentation, which makes the code more readable and much easier to spot syntax errors like this.


class OrangeTree
  def initialize
    @age = 0
    @tall = 0
    @fruit = 0

    puts 'You have planted a new tree!'
  end

  def height
    puts 'The tree is ' + @tall.to_s + 'foot tall.'
  end

  def pickAFruit
    if @fruit <= 1
      puts 'There is not enough fruit to pick this year.'
    else
      puts 'You pick an orange from the tree.'
      @fruit = @fruit - 1
    end
  end

  def countOranges
    puts 'The tree has ' + @fruit.to_s + 'pieces of fruit'
  end

  def oneYearPasses
    @age = @age + 1
    @tall = @tall + 3
    @fruit = 0

    if dead?
      puts 'The Orange Tree Dies'
      exit
    end

    if @age > 2
      @fruit = @age*10
    else
      @fruit = 0
    end
  end

  private

  def dead?
    @age > 5
  end

end

tree = OrangeTree.new
command = ''

while command != 'exit'
  puts 'please enter a command for the virual tree'
  command = gets.chomp
  if command == 'tree height'
    tree.height
  elsif command == 'pick fruit'
    tree.pickAFruit
  elsif command == 'wait'
    tree.oneYearPasses
  elsif command == 'count fruit'
    tree.countOranges
  elsif command == 'exit'
    exit
  else
    puts 'Cant understand your command, try again'
  end
end