When would a Ruby flip-flop be useful?
Asked Answered
H

3

21

I think I understand how a flip-flop works thanks to a tutorial, but the example there is contrived just for teaching. Can anyone give an example of how you have actually used or would use a flip-flop?

I'm looking for a real-world application, not just another demonstration. What problems can this tool solve?

The link used to be http://vision-media.ca/resources/ruby/ruby-flip-flop-or-range-operators, but seems to be spam this days.

Hobie answered 10/7, 2009 at 18:46 Comment(0)
P
20

Here's an example (taken from a rubycentral.com article) where you print out only certain lines from a file:

file = File.open("ordinal")
while file.gets
    print if ($_ =~ /third/) .. ($_ =~ /fifth/)
end

This assumes that you have a file with the following contents:

first
second
third
fourth
fifth
sixth

The program will only print out:

third
fourth
fifth

The idea is that it the value is true until the left-hand event happens, and then stays true until the right-hand event happens. If used properly this can be a nice piece of syntactic sugar, but you need to be careful to make things readable.

Papandreou answered 10/7, 2009 at 18:48 Comment(6)
-1: untested code. What do you think this is, perl? you need print if ($_ =~ /third/) .. ($_ =~ /fifth/) (I know it's not your code, but there's no need to continue spreading code that uses deprecated features).Uncork
What is the actual use for this? When might I need to print the 3rd through 5th lines from a file (enough times to make it worth coding)?Hobie
Forget about 3 and 5. It's about extracting a section of the file delimited by a begin tag and an end tag. I hope you see better why that's a real world example.. Now the equivalent non-flip-flop version would involve adding variable for the state, more if statements: just more complex code.Pleach
Wow, now i'm seeing my ruby's object orientated nature is so powerful.. using ranges in ways like this. Just amazing!Dahlberg
… this isn’t a range. You completely misunderstood the flip-flop operator. It’s the same piece of syntax, that happens to mean a completely different thing in this context. (Nobody ever said Ruby’s syntax was simple: j.mp/cu0eFz - and that’s extremely incomplete!)Bechtel
I used something very similar twice last week to capture sections in a file between two regex.Tainataint
C
10

I'd like to add to James' answer with some concrete examples. I've used this operator for pulling out sections of text based on regular expressions.

I was writing a tool that involved running commands on a remote server through Net::SSH. This particular server had the annoying habit of printing a MOTD regardless of whether the session was a login session or not. This resulted in getting a lot of garbage back when I ran a command and retrieved the output. As I didn't have a lot of sway in the server setup, I created a small script that printed out a delimiter, ran the program, and then printed another delimiter. The output looked something like this.

Welcome to Server X!

+----------------------------------------------------------------------+
| Use of this server is restricted to authorized users only. User      |
| activity may be monitored and disclosed to law enforcement personnel |
| if any possible criminal activity is detected.                       |
+----------------------------------------------------------------------+

----------------------------------------------
    Setting up environment for user Adam. 
----------------------------------------------

>>>>>>>>>>>>>>>>>>>>>>>>>
Program Output
<<<<<<<<<<<<<<<<<<<<<<<<<

The flip-flop operator was a useful shortcut to pull out just the section of code with the output I needed. I used a regex that matched the 25 greater-thans ">" to start the match, and 25 less-thans "<" to end the match.

output.each { |line| puts line if line[/^>{25}/] .. line[/^<{25}/] }

Output

>>>>>>>>>>>>>>>>>>>>>>>>>
Program Output
<<<<<<<<<<<<<<<<<<<<<<<<<

Most examples I've seen have involved pulling chunks of data out of a file or arrays based on regular expressions. Some other examples that come to mind are pulling out git merge conflicts, certain records from legacy flat file systems (like structs written to file), and log files.

Basically, any time you'd need to pull out sections based on beginning and ending data rather than just an individual line's content. It's a bit more complex than just a simple regex, but less complex than writing a parser.

Circumscissile answered 30/1, 2013 at 20:9 Comment(0)
E
4

Odd/even line highlighting in HTML tables with many rows would seem to be a valid use-case.

I've written something not as elegant as the above several times in the past when rendering tables in Rails.

Erotic answered 18/7, 2011 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.