Feature #5400

Remove flip-flops in 2.0

Added by Magnus Holm over 2 years ago. Updated about 1 year ago.

[ruby-core:39915]
Status:Open
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:Next Major

Description

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

History

#1 Updated by Joey Zhou over 2 years ago

Magnus Holm wrote:

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

I disagree. The flip-flop syntax is obscure, but very useful, especially in text manipulation.

For example, I want to fetch some chunks of lines :

DATA.each_line do |line|
print line if (line =~ /begin/)..(line =~ /end/)
end

END
0a
1begin
2c
3end
4e
5f
6begin
7end
8i
9j

this will print:

1begin
2c
3end
6begin
7end

flip-flop syntax comes from Perl, the Perl idiom looks like "print if /begin/../end/;" or "print if 5..8;" (which means print line5 to line8).

Perl idiom is implicit, not so easy to read, Ruby idiom is a little hard to write.

I'm afraid Rudy didn't take advantage of flip-flops, I try to write in this way:

DATA.readlines.select {|line| (line =~ /begin/)..(line =~ /end/) } # error

it seems that ruby treat (line =~ /begin/)..(line =~ /end/) as a range object, that is not what I mean.

Maybe it's difficult for the parser to distinguish the range token ".." and flip-flop token "..", Perl use the same token because it has "context", but Ruby hasn't.

#2 Updated by Joey Zhou over 2 years ago

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(testrightsametime=false)
@bool = false
@sequence
num = 0
@sametime = testrightsametime
end

def rewind
initialize(@same_time)
end

def test(conditionleft,conditionright)
if @bool == false and conditionleft
@sequence
num = 1
@bool = true
if @sametime == true and conditionright
@sequencenum = 1.0
@bool = false
end
return true
elsif @bool == true and not condition
right
@sequencenum += 1
return true
elsif @bool == true and condition
right
@sequencenum += 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?
@sequencenum.isa? 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

#3 Updated by Yusuke Endoh over 2 years ago

Hello,
I'm one of the few users of flip-flop.

W, H = 44, 54
c = 7 + 42 * W
a = [0] * W * H
g = d = 0
f = proc do |n|
a[c] += 1
o = a.map {|z| " :#"[z, 1] * 2 }.join.scan(/.{#{W * 2}}/)
puts "\f" + o.map {|l| l.rstrip }.join("\n")
sleep 0.005
d += 1 - 2 * ((g = 1 << n) >> n)
c += [1, W, -1, -W][d %= 4]
end
1024.times do
!!(!!(!!(!!(!!(!!(!!(!!(!!(true...
f[0])...f[1])...f[2])...
f[3])...f[4])...f[5])...
f[6])...f[7])...f[8])
end

Sorry for off-topic :-)

I have no objection to deletion, but I'm just curious.
Why do you want to delete it aggressively?

--
Yusuke Endoh mame@tsg.ne.jp

#4 Updated by Peter Aronoff over 2 years ago

On Tue Oct 04 2011 @ 4:43, Magnus Holm wrote:

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

As someone who came to Ruby from Perl, I also use/like the flip-flop
operator.

Having said that, maybe a better question than "Who likes the feature?" or
"Who doesn't?" is "What's bad enough about flip-flop that merits removal?"
Does it cause some performance problem or lead to wrongheaded programming?
(Not rhetorical: I genuinely don't see what would make it worth removing.)

Thanks, Peter
--
[D]igital information lasts forever--or five years, whichever comes first.
"Ensuring the Longevity of Digital Information", Jeff Rothenberg

#5 Updated by Andrew Grimm over 2 years ago

I'll be a little sad if the flip flop operator is removed, but it is a Perlism, and Ruby is gradually getting rid of Perlisms. I suspect it won't be around in 100 years time, and I've heard that flip-flops aren't mentioned in the Ruby specification.

When I mentioned flip-flops (and Rubinius' failure to support them) in my talk at RubyKaigi 2011, the response of some was "What's the flip flop operator?"

If the feature is removed, how will Ruby treat existing code that uses the flip-flop operator?

Will it convert it into a literal range, and raise an ArgumentError? (false)..(true) raises an ArgumentError

Or will it explain that flip-flops are no longer supported?

#6 Updated by Yukihiro Matsumoto over 2 years ago

Hi,

Under the current plan, I am not going to remove flip-flop from 2.0,
since we are not going to made incompatible changes anytime soon. We
have to wait until 3.0.

                        matz.

In message "Re: [Ruby 1.9 - Feature #5400] Remove flip-flops in 2.0"
on Sat, 8 Oct 2011 11:51:50 +0900, Andrew Grimm andrew.j.grimm@gmail.com writes:

|I'll be a little sad if the flip flop operator is removed, but it is a Perlism, and Ruby is gradually getting rid of Perlisms. I suspect it won't be around in 100 years time, and I've heard that flip-flops aren't mentioned in the Ruby specification.
|
|When I mentioned flip-flops (and Rubinius' failure to support them) in my talk at RubyKaigi 2011, the response of some was "What's the flip flop operator?"
|
|If the feature is removed, how will Ruby treat existing code that uses the flip-flop operator?
|
|Will it convert it into a literal range, and raise an ArgumentError? (false)..(true) raises an ArgumentError
|
|Or will it explain that flip-flops are no longer supported?

#7 Updated by Magnus Holm over 2 years ago

I have no objection to deletion, but I'm just curious.
Why do you want to delete it aggressively?

Yusuke Endoh mame@tsg.ne.jp

I just want to get rid of complexity in the language.

Currently, flip-flops aren't well known for Rubyists, so I don't feel
comfortable of using them in code. I fear that it won't be readable.
And if you don't know them, it's easy to confuse them for literal
ranges, which makes it even more confusing. If it had a distinct
syntax, you would at least realize that you don't know about them, now
you would go "what? a Range literal is always true, no?".

#8 Updated by Yui NARUSE over 2 years ago

  • Project changed from ruby-trunk to CommonRuby
  • Target version deleted (Next Major)

#9 Updated by Yui NARUSE over 2 years ago

  • Project changed from CommonRuby to ruby-trunk

#10 Updated by Yusuke Endoh about 2 years ago

  • Target version set to Next Major

#11 Updated by Boris Stitnicky over 1 year ago

For Endo's sake, please don't remove this jewel, bring it to perfection, somehow :))))

#12 Updated by Koichi Sasada about 1 year ago

  • Category set to core
  • Assignee set to Yukihiro Matsumoto

Also available in: Atom PDF