News for hackers. Stuff that matters.
Re:How it became like this? Ruby Range class by jballanc on Monday July 29, 2024 @10:06PM (#41105489)
Nice write-up, but no discussion of Ruby's Range class is complete without at least a mention of the venerable flip-flop!

Can you predict the output of the following?

    (1..20).each do |i|
      puts i if i.odd?..i.prime?
    end
Re:How it became like this? Ruby Range class by Lammy on Monday July 29, 2024 @11:38PM (#41105865)
Relevant documentation link for the uninitiated: https://docs.ruby-lang.org/en/master/syntax/control_expressi...

“The form of the flip-flop is an expression that indicates when the flip-flop turns on, .. (or ...), then an expression that indicates when the flip-flop will turn off. While the flip-flop is on it will continue to evaluate to true, and false when off.”

Re:How it became like this? Ruby Range class by cout on Monday July 29, 2024 @10:27PM (#41105581)
This is one of the few features of Ruby I've never found a use for, and not fit lack of trying.

I thought I remembered Matz once saying it would be removed, But I could be wrong. Maybe someone uses it.

Re:How it became like this? Ruby Range class by lmz on Tuesday July 30, 2024 @12:04AM (#41105956)
Re:How it became like this? Ruby Range class by pizza234 on Tuesday July 30, 2024 @02:33AM (#41106559)
The flip-flop operator is very useful to extract continuous subsets, typically, sections of (multi-line) strings, where the dev defines the delimiters - think of the `=begin` and `=end` keywords.

I've personally never used for anything else than strings, but when I do, it's very useful.

Re:How it became like this? Ruby Range class by pooriar on Monday July 29, 2024 @10:59PM (#41105709)
Same as this?

  (1..20).each do |i|
    puts i if i.odd? || i.prime?
  end
edit - upon testing, I just realized the parent's code prints 10 & 16, my code does not, so not the same.
Re:How it became like this? Ruby Range class by codesnik on Tuesday July 30, 2024 @12:44PM (#41111184)
flipflop basically has a hidden boolean variable for state. Btw, while I HAD used it, i still have no idea what's the scope of that state and when it'd reset itself.