2012-11-04 47 views
5

尝试使用新的LearnStreet在线教程来学习Ruby。停留在LearnStreet Ruby培训。简单的Ruby代码

试图通过他们的Q &系统获得帮助,但似乎没人回答他们。

“你现在可以实现撤!帐户对象,其中 需要一个参数量,并降低由指定 量的平衡?定义方法后,继续前进,从账户中提取100元钱 的方法并检查天平。“

是对问题和我的

“提示1所述的代码@balance = @balance两个提示 - 量从 @balance减少量

提示2然后,调用该方法撤销帐户对象上 - ! account.withdraw(100)“

我的尝试是

def 

account.widthdraw! 

@balance = @balance - amount 

end 

account.withdraw!(100) 

任何想法我失踪?

回答

3

“你现在可以实现撤!帐户对象,由指定的量需要一个参数量和降低的平衡?定义方法后,继续前进,从账户中提取100美元的方法并检查天平。“

一步一个时间:

  • “你现在可以实现退出帐户对象的方法

    class Account 
        def withdraw! 
        end 
    end 
    
  • 这需要一个参数量...

    class Account 
        def withdraw!(amount) 
        end 
    end 
    
  • 并减少了balan ce按规定的金额?

    class Account 
        def withdraw!(amount) 
        @balance = @balance - amount 
        end 
    end 
    
  • 定义方法后,继续前进,从账户中提取100元钱,并检查平衡“

    account = Account.new 
    account.withdraw!(100) 
    
2

我想你会想要这样的东西。

class Account 

    def withdraw! amount 
     @balance -= amount 
    end 

end 
+1

为了澄清@ user1739696,对于大多数情况,“@balance = @balance - amount”相当于“@balance - = amount”。通过'def account.with在特定的情况下,draw!'会起作用,但这种情况并不是真的。另外,你似乎错过了这件大事,就是'金额'参数。 (对不起@alex,不想试着回答你的问题) –

+2

@JimDeville我很感谢澄清,谢谢。我不是Ruby专家,因此您的反馈很有价值。 – alex

0

这是这个问题的答案:

def account.withdraw!(amount) 
    @balance = @balance - amount 
end 
account.withdraw!(100)