查看: 2460|回复: 2

[已经解决] each的使用有个疑问

[复制链接]
梦石
0
星屑
1957
在线时间
410 小时
注册时间
2018-9-25
回帖
25
发表于 2020-5-30 10:15:13 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
  1. a = 1..10
  2. p a.include?(9.9)
  3. a.each {|i| p i}
复制代码
这段代码的运行结果说明9.9是在1..10里面的,但是each p出来只有整数了(试了一下for也是一样的),是each会对对象做什么处理吗?这个是只针对范围对象的特殊处理?
梦石
1
星屑
24088
在线时间
3339 小时
注册时间
2011-7-8
回帖
3862

开拓者

发表于 2020-6-5 22:33:09 | 显示全部楼层
Range类的each方法里提到(Ruby2.7.1版本):
The each method can only be used if the begin object of the range supports the succ method. A TypeError is raised if the object does not have succ method defined (like Float).

也就是说当你的Range第一项是整数的时候,调用each就会从这个整数开始数起,不断+1,直到超出范围。
熟悉rgss和ruby,xp区版主~
正在填坑:《膜拜组传奇》讲述膜拜组和学霸们的故事。
已上steam:与TXBD合作的Reformers《变革者》
* 战斗调用公共事件 *
* RGSOS 网络脚本 *
回复

使用道具 举报

梦石
0
星屑
13562
在线时间
2753 小时
注册时间
2014-10-4
回帖
705

R考场第七期纪念奖

发表于 2020-6-6 01:11:16 | 显示全部楼层
其实我觉得是有点bug的感觉..
我看了1.8.3的源码
include? 的描述是  对象是否是range的元素之一
但是具体的实现是 对象是否大于下限小于上限 (毕竟一项一项列出来效率太差了)
我感觉可以加个判断 不是同一个类就pass 就没bug了
现在这样 其实就当feature吧  完全不可比较的两个类还是会返回false的反正

而each就是不断succ 所以一定是同一类的

我感觉源码里这段介绍写的超好  
我头一次发现Range还能这样写
附上
Range
/*  A <code>Range</code> represents an interval---a set of values with a
*  start and an end. Ranges may be constructed using the
*  <em>s</em><code>..</code><em>e</em> and
*  <em>s</em><code>...</code><em>e</em> literals, or with
*  <code>Range::new</code>. Ranges constructed using <code>..</code>
*  run from the start to the end inclusively. Those created using
*  <code>...</code> exclude the end value. When used as an iterator,
*  ranges return each value in the sequence.
*     
*     (-1..-5).to_a      #=> []
*     (-5..-1).to_a      #=> [-5, -4, -3, -2, -1]
*     ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
*     ('a'...'e').to_a   #=> ["a", "b", "c", "d"]
*     
*  Ranges can be constructed using objects of any type, as long as the
*  objects can be compared using their <code><=></code> operator and
*  they support the <code>succ</code> method to return the next object
*  in sequence.
*     
*     class Xs                # represent a string of 'x's
*       include Comparable
*       attr :length
*       def initialize(n)
*         @length = n
*       end
*       def succ
*         Xs.new(@length + 1)
*       end
*       def <=>(other)
*         @length <=> other.length
*       end
*       def to_s
*         sprintf "%2d #{inspect}", @length
*       end
*       def inspect
*         'x' * @length
*       end
*     end
*     
*     r = Xs.new(3)..Xs.new(6)   #=> xxx..xxxxxx
*     r.to_a                     #=> [xxx, xxxx, xxxxx, xxxxxx]
*     r.member?(Xs.new(5))       #=> true
*     
*  In the previous code example, class <code>Xs</code> includes the
*  <code>Comparable</code> module. This is because
*  <code>Enumerable#member?</code> checks for equality using
*  <code>==</code>. Including <code>Comparable</code> ensures that the
*  <code>==</code> method is defined in terms of the <code><=></code>
*  method implemented in <code>Xs</code>.
*     
*/

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部