Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would a Ruby flip-flop be useful?

Tags:

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.

like image 770
kajaco Avatar asked Jul 10 '09 18:07

kajaco


People also ask

Which flip flop is most widely used and why?

Then to overcome these two fundamental design problems with the SR flip-flop design, the JK flip Flop was developed. This simple JK flip Flop is the most widely used of all the flip-flop designs and is considered to be a universal flip-flop circuit.

Why flip flops are required?

In general, n flip flops can represent 2n states, much in the same way as an n-bit number can represent 2n values. Thus, the number of binary bits needed to represent all of the states will equal the number of flip flops needed to implement the state machine.

Why is it called flipflop?

All you need to do to figure out why we call them "flip-flops" is to walk around in a pair of them for just a little while. Because of how they're made, the rubber soles slap against the bottom of your feet as you walk, making a flip-flop, flip-flop sound.


2 Answers

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.

like image 88
James Thompson Avatar answered Oct 13 '22 22:10

James Thompson


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.

like image 21
Adam Lukens Avatar answered Oct 13 '22 21:10

Adam Lukens