新手问题 [已解决] 怎么理解布尔表达式中的 range 和 flip-flop?

springwq · March 01, 2014 · Last by springwq replied at March 01, 2014 · 1610 hits

看到镐头书,有下面两行代码,实在是理解不了了。。。

  • 1

    a = (11.20).collect {|i| (i%4 == 0)..(i%3 == 0) ? i : nil}
    
    #=> [nil, 12, nil, nil, nil, 16, 17, 18, nil, 20]
    
  • 2

    a = (11.20).collect {|i| (i%4 == 0)...(i%3 == 0) ? i : nil}
    
    #=> [nil, 12, 13, 14 ,15, 16, 17, 18, nil, 20]
    

无法理解这里的 range 是怎样工作的??

(i%4 == 0)..(i%3==0) ? i : nil

搜到一些资料了, http://ruby-china.org/topics/13951 http://lilydjwg.is-programmer.com/2012/3/24/flip-flop-in-ruby-jk-latch.32635.html 基本理解了。

这是 flip-flop 语法,和 range 已经没关系了,就像是三元运算里的?和正则里的?只是同样的符号,意义完全不同

#1 楼 @jjym 恩,谢谢!

You need to Sign in before reply, if you don't have an account, please Sign up first.