Ruby: divisible by 4

房东的猫 提交于 2019-12-03 01:10:23
Sergio Tulentsev

Try this:

if i % 4 == 0

This is called the "modulo operator".

There's also modulo, which allows you to do

420.modulo(4).zero?

There's nothing stopping you doing that with %, but it looks weird:

420.%(4).zero?

This is always a good conversation starter:

if (i & 3).zero?
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!