像这样:
puts "Today my ostrich laid #{@eggs_laid - @stillborn - @wolves_ate} valid eggs."
如果这是我唯一需要这个特定计算的地方,那么制作一个valid_eggs函数是否有任何意义?
我如何让我的代码反映出来?我应该考虑将4号作为素数吗?
我有这样的事情:
array = [
hash1 = {"marco"=>"polo", "girth"=>"skinny", "onion"=>true},
hash2 = {"darco"=>"johnson", "girth"=>"wide", "onion"=>false},
hash3 = {"flarco"=>"kiwi", "birth"=>"noble", "onion"=>false}
]
Run Code Online (Sandbox Code Playgroud)
其中一个且只有一个onion是true在任何给定的时间.
我想一个表达式或函数返回值(即名hash1,hash2持有其哈希)onion是目前true.我怎样才能做到这一点?
我有两个类变量,@ticks并且@dings,这两个我要开始在1.
其它功能偶尔添加各种数额@ticks,并@dings通过将增加1每次@ticks达到或超过 (@dings * 100) + (@dings * 10).
我想dings += (number of times met or surpassed)在任何导致@ticks达到或超过当前值(@dings * 100) + (@dings * 10)一次或多次的操作之后立即执行.
这是一个合适的解决方案?
def increment_ticks
while @ticks >= (@dings*100) + (@dings*10)
@dings += 1
end
end
Run Code Online (Sandbox Code Playgroud)
我只是在increment_ticks每次@ticks添加时调用(或increment_ticks在所有添加的函数中添加适当的位置@ticks).