Ruby Range in Boolean expressions
来源:互联网 发布:淘宝的差评多久会没有 编辑:程序博客网 时间:2024/05/21 22:32
以下技术应用于最优质的水果的鲜果篮
Copy from: http://www.sitepoint.com/forums/showthread.php?402694-Ranges-in-Boolean-Expressions
Ruby maintains the true/false state of the boolean expression when using a range... it switches back and forth between the start and end value of the range with each state change. I think it's easiest to explain by stepping through the process using the example below:
Code:
a = (11..20).collect {|i| (i%4 == 0)...(i%3 == 0) ? i : nil} a » [nil, 12, 13, 14, 15, 16, 17, 18, nil, 20]
We start by passing the value 11 into the code block:
11 is passed in, (11%4 == 0) is False, so the state does not change... it remains false, so nil is returned.
12 is passed in, (12%4 == 0) is True, so the state CHANGES to true.. the value of 'i' is returned
Since the state has now changed, ruby switches to the end value in our range when evaluating the boolean expression
13 is passed in, now we use the end expression of the range: (i%3 == 0). Since (13%3 == 0) is False, no change is made to the state. However, Ruby remembers that the state was ALREADY true, so it STILL treats it as true, and returns the value of 'i'
14 is passed in, (14%3 == 0) is false, so again, no state change. The boolean is still considered true, and the value of i is returned
15 is passed in, (15%3 == 0) is True, so the state changes (since it was already true, it now changes to false). Ruby still returns the value of i before completing the loop. However, on the next iteration, it switches back to the first expression in the range - (i%4 == 0)
16 is passed in, (16%4 == 0) is True also, so the state changes yet again (it was false, so now it's back to true!) the value of i is returned and again we switch to the end value of our range when evaluating the next iteration
and so it goes... flipping back and forth between the start and end value of the range with each state change
When you use the 2 dot range in a boolean expression, ruby will test the start and end value of the range during the SAME iteration IF the first expression of the range returns true... phew... so in effect it changes state twice in one iteration, while the 3 dot range I used above can only change state once with each iteration.
- Ruby Range in Boolean expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean Expressions
- Boolean expressions
- 14.16 Boolean expressions
- pku 2106 Boolean Expressions
- poj 2106 Boolean Expressions
- poj 2106.Boolean Expressions
- POJ 2106 Boolean Expressions
- POJ 2106 Boolean Expressions
- POJ2106 Boolean Expressions
- 递归之 Boolean Expressions
- Boolean Expressions POJ
- poj 2106 Boolean Expressions
- mongodb开机自启动失败
- windows 下tomcat服务化
- 《卓有成效的管理者》笔记,第一章 卓有成效是可以学会的
- POJ数学强化
- POJ2253--Frogger--最小生成树Kruskal
- Ruby Range in Boolean expressions
- 关于修改Mysql root密码
- 数据结构(清华严蔚敏)03.asf
- delphi中的窗体事件详解
- iphone下实现图片的缩放和居中显示
- 一只小菜鸟的胡思乱想
- 程序员面试求职100题(11-20)
- 安装Ubuntu 12.10 进度条最后卡在update-notifier-common不动了 解决办法
- 在java中生成二维码,并直接输出到jsp页面