Flip Flop
class Array
def ff(start, finish, inclusive: true)
a,b = inclusive ? [0,-1] : [1,-2]
select {|i| i if (i == start)..(i == finish)}[a..b]
end
end
names = %w(Otto Arne Peter START Kapris Oregano END Sune Fetto)
p names.ff('START', 'END') # ["START", "Kapris", "oregano", "END"]
p names.ff('START', 'END', inclusive: false) #["Kapris", "Oregano"]
I was trying to figure out if there was a way to make the flip-flop operator exclusive. Didn’t find much of anything.. so this is what I came up with.
Just a first try… a lot of holes in this solution.
(flip-flop op is useful in some situations.. example b’low)
conf = <<~CONF.split "\n"
# this is a test
# here are the settings
This part is about nothing
values = 37
sound = off
user: ior
animal = Donkey
home = Fältflaskevägen 8
---
Some more shit
doktorer = %w(alban mengele oetker no)
user: agneta
animal = Fylltratt
home = Saknadensvägen 318B
---
limits = [none]
signing off
end-of-file
CONF
conf.each {|row|
if row[/^user:/]..row[/^---/]
puts '==> %s <============' % $1.upcase if row =~ /user:\s*(\w+)/
puts '%s => %s' % [$1, $2] if row =~ /^(\w+)\s*=\s*(.*?)$/
end
}
=begin OUTPUT
==> IOR <============
animal => Donkey
home => Fältflaskevägen 8
==> AGNETA <============
animal => Fylltratt
home => Saknadensvägen 318B
=end