ESS多版本 【TA】【ESS】05 范围和遍历

TAAAAAAA

天王
管理成员
2024/06/16
273
4
38
1,270
5: Ranges and for-loops 范围和遍历

There’s yet another data type to introduce now, and that’s Range. A Range is, as the name suggests, a range of numbers with a start and an end. If you want to make a range with the number 1, 2, 3 and 4, you have two options:

有一种数据类型叫做范围,我们来看下面的这个例子:


You first type a number, 1 in this case (can also be negative). If you then write down two dots, you’re including the number written down after the dots. If you write down three dots, you’re excluding the number written down after the dots. So 1..4 starts at 1 and includes up to 4. 1...5 starts at 1 and goes up to 5, but doesn’t include it.

You can also have negative ranges.
-7..-3 (-7, -6, -5, -4, and -3)
-2..2 (-2, -1, 0, 1, and 2)

You can’t have ranges go from big to small, though. You can’t use Floats either.
2..-2 is empty. Just like an empty array, [], this is empty. Nothing.
1.1..1.3 is also empty. It won’t work when you try to do anything with it (regardless of using parentheses)

同样的,也支持负数的范围,比如说-2..2,就是指从-2到2,并且包括2。

Naturally, ranges also work with variables.

我们来看下面这个使用变量来作为范围的两个端点的例子:


The parentheses don’t make a difference, as long as they’re valid.
 

TAAAAAAA

天王
管理成员
2024/06/16
273
4
38
1,270
Iterating through ranges 通过范围进行迭代

If you want to take each number in a range and do something with it (printing it out, for instance), you’d use for-loops. These use the keyword for and iterate over every number in a range. This for keyword also has to be “closed” with end.

现在我们终于讲到迭代了。
关于迭代,不同的人有不同的称呼,有些人叫迭代,有些人叫遍历,有些人叫循环,叫什么并不重要,重要的是你要知道它到底是怎么个东西。
我个人觉得最最简单的说法的话,迭代其实就是对一些数据做处理。
说到“一些”,其实你应该就想到数组了,数组里就有一些数据,以及想到前面讲的范围,范围里也有一些数据。
接着是关于“处理”,我们其实可以根据不同的条件对这些“一些”数据进行处理,也就是说,并不是所有的数据都必须做一样的处理。
举个例子,我们可以对符合a条件的数据做A处理,符合b条件的数据做B处理,等等。

在原作者的教程中,原作者是使用for(对于)进行迭代的,现在的ESS已经全面不用for进行迭代了。
现在我们使用each(每一个)进行迭代,使用each是Ruby 1.9及以后的版本所推荐使用的迭代方式,因为它提供了更清晰的代码块定义,更好的可读性和一致性,并且能更自然地扩展到其他的可迭代对象,最后,each的写法是更加现代的和Rubyic(Ruby语言化)的。
在以后的例子中,我都会简要的补上使用each的写法。

我们来看下面的例子:


What this does is take every number in 1..3, which are 1, 2, and 3, and it does i = the number. It then runs the code inside the for-loop, which is print i in our case.

Important to note is that for-loops don’t introduce a new scope, which means the value of i will be overwritten in the loop.


At the last iteration of the for-loop, it essentially does i = 3. This isn’t reversed, so i becomes 3 outside of the loop, too. This is because it’s the same scope. Look at it like this:
 

TAAAAAAA

天王
管理成员
2024/06/16
273
4
38
1,270
Iterating through arrays with ranges 通过范围迭代数组

The array ["one","two","three"] has indexes ranging from 0 to 2. We’d write this as 0..2 or 0...3. The size of the array is 3. If we wanted to setup a range that corresponds with an array’s indexes, we’d start at 0, because that’s where array indexes start, and then we have to use either ..2 or ...3. Since array.size would give us 3, it’s easiest to use ...3.


Again, the parentheses don’t make any difference.

The range 0...3 contains 0, 1, and 2 - the same indexes as in the array. This means that if we iterate through this 0...3 range and use them as indexes in the array, we get every single element in the array.


We usually skip the extra variable for the range.


And that’s your typical array loop!
 

TAAAAAAA

天王
管理成员
2024/06/16
273
4
38
1,270
Iterating through enumerables 通过枚举迭代

本小结直接跳过,因为前面的小结已经全部说过了。

Another way to iterate over an array which may be more practical at times, is to iterate over it directly rather than using indexes. Imagine this setup:


We assign a new variable called e and set it to the first element of the a array, which is “one”. Since that’s now a new variable, if we change its value, the value of the first element in the array won’t change - only that new variable will.

If we take this concept and use it inside a for-loop, we get something a little bit like this:


What we’re doing here is iterating through each element directly. First it does e = “one”, then e = “two”, and then e = “three”, rather than the indexes.

You can change the value of e, too. It just won’t affect the array being iterated through.


However, if you were to do this with indexes, you are actually changing the array:
 

TAAAAAAA

天王
管理成员
2024/06/16
273
4
38
1,270
Calling methods on ranges 在范围中调用方法

本小结直接跳过。
本小结说的是1..4.include?(2)和(1..4).include?(2)的区别。
作者说前一种写法是错的,但是实际上前一种写法也是可以的。
这一行代码的意思是检查2是否在1到4(包括4)内。

There’s just one method on ranges that’d be useful to know for now, and that’s include?(n). Just like with arrays, it will return true if n is included in the range, and false if not.


You have to be careful when calling a method on a range though. If you were to type something like this:


...It’ll raise an error. Internally, it’s seen like so:


...And there isn’t a method called include? on Fixnums.
So to make sure you’re actually calling the method on the range and not on a Fixnum, you should surround the range with parentheses.


This is one of the only instances where parentheses with ranges really matter.

本章完。
 

在线成员

现在没有成员在线。

最新帖子

论坛统计

主题
540
消息
2,458
成员
3,089
最新成员
未白镇——诺亚