1

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

default
  • 11,485
  • 9
  • 66
  • 102
KKK
  • 1,085
  • 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
  • 158,662
  • 42
  • 215
  • 303
23tux
  • 14,104
  • 15
  • 88
  • 187
  • 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
sjain
  • 23,126
  • 28
  • 107
  • 185