0

I am using open4::background to open a process on the shell. open4::background allows me to use any class that implements <<, each, read, or to_s as a handle to stdx.

I am currently using a String, but every time a write occurs on stdout, it replaces the old value. Is there a simple class I can use to make new write append instead of replace, considering the acceptance of any class that implements certain string-like functions?

I am new to Ruby and I am just hoping to plug this part in. If anyone can contribute or point me to a simple existing class for this I'd appreciate it.

There is a thread explaining this with a sample implentation here: http://www.ruby-forum.com/topic/151316 but I think that is a bit too complex for what I'm looking to do now. As a Ruby n00b, I'd feel more comfortable if someone else could massage that sample for me.

Thanks.

EDIT:

Per Phrogz's request, here is what I want to be able to do:

app_str = AppendedString
app_str = 'jeff'
app_str = 'walls'
puts app_str
# should display "jeffwalls"
jeffcook2150
  • 4,028
  • 4
  • 39
  • 51
  • I don't understand what you want; please modify your question to include sample code that you would run and the output you would like to get already. – Phrogz Jan 27 '12 at 19:06
  • We have certain assumptions of behavior for operators, and redefining their expected behavior can lead to very hard to debug and manage code. In some cases implementing or overriding `+` or `-` makes sense, but I can not see any case where modifying `=` would make sense, especially since we already have `+`, `+=` and `<<` available to us that provide similar functionality. – the Tin Man Jan 27 '12 at 20:09

2 Answers2

1
app_str = AppendedString
app_str = 'jeff'
app_str = 'walls'

This would require overriding the assignment operator. Unfortunately, Ruby doesn't permit overriding the assignment operator.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
1

What you want is to use StringIO to capture all the contents as a string for you.

(As @John says, repeated assignment you can't catch, but if you just want to accumulate all the values created by << then this will do it.)

Phrogz
  • 296,393
  • 112
  • 651
  • 745