Issue #5400 has been updated by Joey Zhou.


Magnus Holm wrote:
> Nobody knows them. Nobody uses them. Let's just get rid of flip-flops, shall we?

Well, the flip-flop behavior is useful, so it should not be removed.

However, I agree that the syntax is a bit confusing.

Flip-flop in Ruby is not so powerful as in Perl (http://perldoc.perl.org/perlop.html#Range-Operators).

It seems like expression leading to true or false, but it cannot be assigned to a variable, and often be treated as a range literal.

So maybe we can get rid of the ".." syntax, instead, introduce a class to do the same thing.

I've implemented a simple class FlipFlop, which simulates the behavior of flip-flop in Perl.

-- begin --

class FlipFlop

  def initialize(test_right_same_time=false)
    @bool = false
    @sequence_num = 0
    @same_time = test_right_same_time
  end
  
  def rewind
    initialize(@same_time)
  end
  
  def test(condition_left,condition_right)
    if @bool == false and condition_left
      @sequence_num = 1
      @bool = true
      if @same_time == true and condition_right
        @sequence_num = 1.0
        @bool = false
      end
      return true
    elsif @bool == true and not condition_right
      @sequence_num += 1
      return true
    elsif @bool == true and condition_right
      @sequence_num += 1.0
      @bool = false
      return true
    else # @bool == false and condition_left == false
      @sequence_num = 0
      return false
    end
  end

  def true?
    @bool
  end

  def value
    @sequence_num
  end

  def end?
    @sequence_num.is_a? Float
  end

end

-- end --


flipflop = FlipFlop.new

# take only line3 ~ line5 from a chunk of lines (from /begin/ to /end/)
lines = DATA.readlines.select do |line|
  t = flipflop.test(line =~ /begin/, line =~ /end/)
  t and flipflop.value.between?(3,5)
end

p lines # ["04end(x)\n", "09(x)\n", "10(x)\n", "11(x)\n", "17(x)\n", "18(x)\n", "19end(x)\n"]

__END__
01
02begin
03
04end(x)
05
06
07begin
08
09(x)
10(x)
11(x)
12end
13
14
15begin
16
17(x)
18(x)
19end(x)
20begin

----------------------------------------
Feature #5400: Remove flip-flops in 2.0
http://redmine.ruby-lang.org/issues/5400

Author: Magnus Holm
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0


Nobody knows them. Nobody uses them. Let's just get rid of flip-flops, shall we?


-- 
http://redmine.ruby-lang.org