Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Apart from being a synonym for footwear, a ruby flip-flop also refers to a system composed of two alternating (on/off) states that can be switched between. This can be used when someone wants to loop through arrays and capture contiguous subsets between specific bounds. However, not many programming languages have utilized a mechanism like this.
The flip flop operator is one of the peculiar elements of Ruby that many people aren't even aware of. This article will show what flip-flop operators are and where to use them.
What Is the Ruby Flip Flop Operator?
Ruby's flip-flop is a range operator ('..' or '...'). It allows you to program your application's switchable on/off condition-based control flow. This is one of the many Perl concepts that made it into the Ruby language.
In Ruby, a flip-flop is used between two conditions inside a loop with the following syntax:
my_array.each do |y| # or any other loop
if (condition1)..(condition2) # two conditions with flip-flop
# perform something (body)
end
end
You can also try this code with Online Ruby Compiler
In the next step, the program waits for the first condition of the flip-flop to be true before it runs the underlying body for the first time.
When the condition stands true, the internal state of the flip-flop is triggered.
Thereon, the body executes each time. In simple words, the outer loop runs until the second condition returns true.
When the second condition comes true, the flip-flop's state becomes false. This happens just after the body executes.
Consequently, the body does not execute in upcoming iterations until the first condition becomes true again.
Example
# example of flipflop in ruby
[0,1,2,3,4,5].each do |y| #otherwise any other loop
if (y==1)..(y==3)
puts "The Flip-flop is active: #{y}"
else
puts "The Flip-flop is inactive: #{y}"
end
end
You can also try this code with Online Ruby Compiler
The Flip-flop is inactive: 0
The Flip-flop is active: 1
The Flip-flop is active: 2
The Flip-flop is active: 3
The Flip-flop is inactive: 4
The Flip-flop is inactive: 5
Explanation:
Here the loop traverses the array when the first condition (y==1) satisfies, the flip-flop is activated, and the underlying body run. Until the second condition (y==3) returns true and toggles our flip-flop state to off, the state stays active, and the body keeps running at each iteration.
Essentially, the operation comes down to the first condition, checking when to start the flip-flop and accordingly running its body. The second condition checks when to stop or pause.
It is to be noted that even after the second condition satisfies, the body will execute once before the flip-flop is deactivated. Once deactivated, the first condition should be true again so that the state is toggled and the body is running.
Types of Ruby Flip Flops
The flip-flop is a range operator. It can take two forms depending on the two-dot and three-dot ranges. They thus work in two slightly different ways.
In standard Ruby ranges, the one with two dots (..) includes both the ends of the range (first and last elements). The range with three dots, however, leaves the upper limit. However, how this difference is displayed in a flip-flop is dissimilar. In a Ruby flip-flop operator, the difference between the two-dot and three-dot range is mainly in executing or checking the two conditionals used with the operator. Let us have a look at each one of these more deeply.
Two-Dot Range (..) Ruby Flip Flop
In the opening example of flip-flops, we used the one with a two-dot range. This operator lets both the conditions in the if statement to be executed during one iteration. This can be noticed when we take an example where the second condition is also true, along with the first one. Let's take our last example and have both the conditions be true at one instant :
Example 1
# example of flipflop in ruby
[0,1,2,3,4,5].each do |y|
if (y==1)..(y==1) # if it's true .. true
puts "The Flip-flop is active: #{y}"
else
puts "The Flip-flop is inactive: #{y}"
end
end
You can also try this code with Online Ruby Compiler
The Flip-flop is inactive: 0
The Flip-flop is active: 1
The Flip-flop is inactive: 2
The Flip-flop is inactive: 3
The Flip-flop is inactive: 4
The Flip-flop is inactive: 5
Explanation:
Here we can see that when our loop reaches ' 1 ', the first if condition passes and activates the flip-flop. However, the second condition also passes immediately after, deactivating the operator's state. As a result, the enclosed block runs only once, whenever the iterator evaluates to 1, and only then.
Therefore, the output here is just '1'. This evaluation of both conditions primarily distinguishes it from the three-dot range flip-flop operator. Before that, one essential thing to note about both conditions checking in the two-dot operator. The second condition evaluation only occurs when the first condition is or has recently been evaluated as true. This simply means that if our first condition never comes true, the second condition is not checked. We'll dive into the other variant next.
Example 2
# example of flipflop in ruby
[0,1,2,3,4,5].each do |y|
if (y==-1)..(y==1) #the first condition is never true
puts "The Flip-flop is active: #{y}"
else
puts "The Flip-flop is inactive: #{y}"
end
end
You can also try this code with Online Ruby Compiler
The Flip-flop is inactive: 0
The Flip-flop is inactive: 1
The Flip-flop is inactive: 2
The Flip-flop is inactive: 3
The Flip-flop is inactive: 4
The Flip-flop is inactive: 5
As we can see above, the flip-flop never activates.
Three-Dot Range (...) Ruby Flip Flop
The three-dot range flip-flop operator only lets the checking of one if condition for one loop iteration. This doesn't affect the output of the case where both the conditions aren't simultaneously true. Let us have a look at the same example but with different if conditions:
Example 1
# example of flipflop in ruby
[0,1,2,3,4,5].each do |y|
if (y==1)...(y==3) # the three-dot operator
puts "The Flip-flop is active: #{y}"
else
puts "The Flip-flop is inactive: #{y}"
end
end
You can also try this code with Online Ruby Compiler
The Flip-flop is inactive: 0
The Flip-flop is active: 1
The Flip-flop is active: 2
The Flip-flop is active: 3
The Flip-flop is inactive: 4
The Flip-flop is inactive: 5
The output and computation wouldn't have changed even if we used the two-dot operator. The two conditions here would not be the same for any iteration.
However, if both conditions could be true, the three-dot operator would never let the second one evaluate. As a result, the flip-flop's state would be kept active, allowing the block to execute for the following iterations. We'll use the same example as usual :
Example 2
# example of flipflop in ruby
[0,1,2,3,4,5].each do |y|
if (y==1)...(y==1) #the three-dot operator; if true … true
puts "The Flip-flop is active: #{y}"
else
puts "The Flip-flop is inactive: #{y}"
end
end
You can also try this code with Online Ruby Compiler
The Flip-flop is inactive: 0
The Flip-flop is active: 1
The Flip-flop is active: 2
The Flip-flop is active: 3
The Flip-flop is active: 4
The Flip-flop is active: 5
Explanation:
When the first condition passes in the second iteration, the flip-flop activates. Then it runs the code block without checking what the second condition returns. Resulting in the flip-flop staying activated throughout. This is a difference from the two-dot operator. If the second condition is true, it would instantly deactivate the flip-flop.
However, once the first condition is true and the flip-flop is activated, only the second condition is assessed in the following iterations. This happens while completely ignoring the first one.
Using Ruby Flip Flops
It can be said that using flip-flops makes your code a little difficult to follow. However, on the other hand, it saves you a bunch of if statements and an extra state variable at the cost of comprehension. So, It is a tradeoff between understandability and conciseness.
Flip-flops are not so common for general-purpose code. So, You should ensure that the rest of the developers on your team understand what a flip-flop operator is. This is recommended before going ahead and using these all over the code. If everyone is comfortable with this, then it might be worth trying. Still, if not, the readability and easy comprehension offered by many if-statements is the better option.
While it might seem a little complex at first, you start to get the hang of it in different con after flip-flops for long enough. You can appreciate the syntactic ease and the ease of usage they offer. Because of this, these operators are commonly used for text manipulation and text processing applications.
Frequently Asked Questions
What type of operator is flip-flop in Ruby?
The flip-flop in Ruby is a range operator.
How many forms can it take?
It can take two forms depending on the two-dot and three-dot ranges.
Why are these operators commonly used?
These operators are generally used for text manipulation and text processing applications.
What are Ruby ranges?
A Ruby range is a set of values with a start and an end. The range that has .. (two dots), run from the start to end all inclusively. Whereas the Ruby range that has … (three dots), run the end value exclusively.
Conclusion
In the article, we dived in and learned about Ranges and Flip-Flops in Ruby. We also discussed its different types, what it offers, and where it’s best to use. We discovered what two-dot and three-dot ranges are and saw how they work.
Refer to our courses and read some articles on Ruby. You can also check out some other blogs on operators to follow.
Do upvote our blog to help other ninjas grow. Happy Coding!