1

I'ld like to overwrite the system() function. Is that possible?

default
  • 10,975
  • 8
  • 63
  • 100
KKK
  • 1,075
  • 11
  • 33
  • 1
    Why would you do that? What do you want to achieve? – 23tux Jan 15 '13 at 14:28
  • You could "monkeypatch" it, symply by defining it, as commentor 23tux explains. But you [really don't want to overwrite the system(). Really not](http://stackoverflow.com/a/4471202/73673). – berkes Jan 15 '13 at 14:32
  • This is a bad thing to do. That you ask if you can shows you probably don't know why you shouldn't. – the Tin Man Jan 15 '13 at 14:32

2 Answers2

3

Sure, you can overwrite nearly everything in Ruby (whether useful or not):

system "ls /" # returns "/etc /var...", normal behaviour

def system args
  puts args
end

system "ls /" # returns "ls /"
the Tin Man
  • 155,156
  • 41
  • 207
  • 295
23tux
  • 13,039
  • 12
  • 81
  • 171
  • I don't know your project, but if you want to use it global: Before you are calling system the first time ;) In an initializer for example, or in your startscript or whatever – 23tux Jan 15 '13 at 14:58
-1

If it doesn't matter to use system at all then you can use backticks. The backticks execute the command and return the output as a string.

You can then assign the value to a variable like so:

output = `ls`
p output
My God
  • 22,686
  • 26
  • 99
  • 179