加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

坚持使用LearnStreet Ruby培训.简单的Ruby代码

发布时间:2020-12-17 03:15:12 所属栏目:百科 来源:网络整理
导读:尝试使用新的LearnStreet在线教程学习 Ruby. 试图通过他们的Q A系统获得帮助,但似乎没有人回答他们. “Can you now implement the withdraw! method on account object,which takes one parameter amount and reduces the balance by the specified amount?
尝试使用新的LearnStreet在线教程学习 Ruby.

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

“Can you now implement the withdraw! method on account object,which
takes one parameter amount and reduces the balance by the specified
amount? After defining the method,go ahead and withdraw 100 dollars
from the account and check the balance.”

这个问题和我得到了两个提示

” Hint 1 The code @balance = @balance – amount reduces the amount from
@balance.

Hint 2 Then call the method withdraw! on the account object –
account.withdraw!(100). “

我的尝试是

def

account.widthdraw!

@balance = @balance - amount

end

account.withdraw!(100)

我缺少什么想法?

解决方法

“Can you now implement the withdraw! method on account object,which takes one parameter amount and reduces the balance by the specified amount? After defining the method,go ahead and withdraw 100 dollars from the account and check the balance.”

一步一步来:

>“你现在可以在帐户对象上实现withdraw!方法吗?

class Account
  def withdraw!
  end
end

>需要一个参数量…

class Account
  def withdraw!(amount)
  end
end

>并减少指定金额的余额?

class Account
  def withdraw!(amount)
    @balance = @balance - amount
  end
end

>在定义方法后,继续从账户中提取100美元并检查余额.“

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读