0

I have a function that checks a software buffer for data, however if that data has not changed it is still added and sent.

So I end up getting data sent multiple times. I'm trying to check to see if my variable has changed, and if it has not I want to prevent it from sending that data again.

Here is what I've tried.

$buffer = fldigi.add_to_buffer(msg)

if $buffer
   fldigi.send_buffer()
 else
   puts "Debug(buffer): Data has not changed"
end
mleko
  • 11,650
  • 6
  • 50
  • 71
Corrosive
  • 86
  • 1
  • 11

3 Answers3

1

You might try wrapping $buffer in an object and then following the Observer Pattern. Any changes to $buffer must go through the wrapper object's methods. Observers can then subscribe to the change notification events emitted by the $buffer wrapper and act accordingly such as fldigi.send_buffer().

kmorris511
  • 16,392
  • 7
  • 28
  • 29
0

There's no way to know whether a variable has changed without tracing the execution of the program. Variables aren't objects in Ruby, you can't tell them to do something, you can't ask them questions, you can't pass them as arguments, you can't return them as values, you can't assign them to variables, you can't call methods on them.

There's only two things you can do with a variable: dereference it and assign to it.

Assignment is the only way to change the variable, therefore you need to trace assignments to the variable to figure out whether or not it was changed. Note, however, that the standard Ruby TracePoint API does not offer the necessary information (probably because variables might get optimized away by the compiler).

It is probably going to be much easier to just check whether some object has changed rather than whether some variable has changed.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • *There's only two things you can do with a variable: dereference it and assign to it.* Wrong. By the way, I'm still waiting for you answer to my comment in this thread: http://stackoverflow.com/questions/25418096/argument-error-with-initialize#comment39690151_25418096 – 7stud Aug 23 '14 at 18:00
-1
prev_val = nil

loop do
  new_val = fldigi.add_to_buffer(msg)

  if new_val == prev_val
    puts "Debug(buffer): Data has not changed"
  else
    fldigi.send_buffer()
  end

  prev_val = new_val
end
7stud
  • 46,922
  • 14
  • 101
  • 127
  • 3
    Hey, your post showed up in the Low Quality Posts queue. Might want to complete the answer by adding details and addressing the question. – Unihedron Aug 23 '14 at 17:23
  • I think this is close.. however the variable is not always nil. Sometimes the data it's grabbing is "randomtext1" this may not change for 20 loops. Later it might be "randomtext5". so unfortunately I can't compare it to nil each time or it'll only run send_buffer() once. – Corrosive Aug 23 '14 at 17:24
  • @Corrosive, the code does not compare the data to nil every time. That only happens for the first piece of data--and you always want to send the first piece of data. – 7stud Aug 23 '14 at 17:52
  • @7stud ... ah alright I see what it's doing now. I'll give this another try. – Corrosive Aug 23 '14 at 17:54