构建适当的类并传递类方法

时间:2014-07-15 22:49:34

标签: ruby class

class Health 

  def initialize()
    @hydration=hydration
  end

  def hydration

    puts"Amount of Water drunk(in oz):"
    x=gets.chomp    
    if #{x}>=20
            puts"Good job! keep on it"
    elsif #{x} >=(10...20)  
      puts"Could do better"
    else 
      puts"Not healthy. Go get hydration"
    end
  end

  drinks=Health.new()
  puts drinks.hydration
end

我是ruby的新手,我想要实现的是提示。我在puts drinks.hydration上得到了“语法错误,意外的输入结束,期待关键字_end”

1 个答案:

答案 0 :(得分:2)

我认为您的当前代码不会出现任何语法错误。为了使其工作,您需要删除if语句中的注释。初始化语句也是多余的,因为你在puts drinks.hydration中明确调用了方法水合作用。

class Health
  def hydration

    puts "Amount of Water drunk(in oz):"
    x=gets.chomp.to_i
    if x>=20
      puts "Good job! keep on it"
    elsif (10...20).include? x
      puts "Could do better"
    else
      puts "Not healthy. Go get hydration"
    end
  end
end
drinks=Health.new()
puts drinks.hydration