DawnFreeze DawnFreeze - 3 months ago 20
Ruby Question

Toggle switch / T-Flip Flip

I am having trouble trying to apply a flip-flop to the following scenario. Say I have the following array:

[ 'on',
'random text',
'random text',
'random text',
'random text',
'off'
'random text',
'random text',
'on'
'random text',
'off'
'random text',
'random text',
etc...


I want a switch to flip according to one of two values (as represented by 'on' and 'off' in the array)

Any help is appreciated

Answer

With your data here is a simple flip-flop example.

x = [ 'on',
  'random text',
  'random text',
  'random text',
  'random text',
  'off',
  'random text',
  'random text',
  'on',
  'random text',
  'off',
  'random text',
  'random text'
]

x.each do |text|
  if text['on'] .. text['off']
    puts text
  end
end

#on
#random text
#random text
#random text
#random text
#off
#on
#random text
#off