The system() method in Ruby.
Yes, we can run any system command with the help of ruby code. We can run system(eg. shell) commands through the system()
method. Let’s see how we can use thesystem()
method in ruby.
system("mkdir new_directory")
The above line will create a new directory in the current folder. You can try this in irb
console or you can create a ruby file for testing purposes.
Now, let’s look into one more interesting hack using thesystem()
method. We can make ruby code to wait through this method. Let’s see how:
system("sleep 10")
The above code will allow ruby code execution to wait till 10 seconds. Isn’t it interesting?
Now, let’s see what system()
method return:
=> true # if command worked properly=> false # if command failed to run due to system error.=> nil # if 'command not found' error occured
That’s it, I hope you like my blog. Please express your feedback in the comments and shower some claps.
You can reach out to me at bhawsartanmay@gmail.com
Connect with me on LinkedIn
Happy coding and see you soon!
Thank You
Tanmay Bhawsar