Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Does ruby have some way to execute a ruby script in tracing mode?
For example: If we execute a bash script with set -x it shows input and output of the command being executed. Is there a way to accomplish this in ruby?
Built-In Debugger
Step through your code with the built-in debugger. Invoke your script with:
ruby -rdebug /path/to/script
and step through it with s or n. See the DEBUGGER_ class for details.
If you want to run arbitrary scripts, then you can use irb.
$ irb
2.3.0 :001 > tmp = 'test'
=> "test"
2.3.0 :002 > class Tmp
2.3.0 :003?> end
=> nil
2.3.0 :004 > Tmp
=> Tmp
Every time you run a command, you get the expression's result.
Since set -x is by no means “debugging,” here goes the implementation of the same functionality in pure ruby:
ruby -e '
code = File.readlines("/path/to/file.rb")
eval code.map { |c| %Q|puts "+ #{c.strip}"| }.zip(code).join($/)
'
Related
This question already has an answer here:
Can ruby tell if it is called from an interactive shell or cron?
(1 answer)
Closed 12 months ago.
I have a Ruby script that is often kicked off as a cron job, but sometimes used from the command line. I'd like to display some update text that is only relevant in the latter case, to show progress, and is not worth logging.
What is a safe and idiomatic way for a Ruby script to detect the following conditions?
whether it was triggered in an interactive shell
whether it is being piped
Check if STDIN is a "TTY" (literally meaning teletypewriter) with IO#isatty.
$ ruby -e 'puts STDIN.isatty'
true
$ echo "no" | ruby -e 'puts STDIN.isatty'
false
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This should be surprisingly simple, but it eludes me. I'm trying to set up a simple command such that I can type: ruby myfile someparams and it will return something via stdio.
I want to include a gem, like https://rubygems.org/gems/github-linguist and pass something to it and see what it has as a response.
I'm a bit lost. Ideas?
Here's a sample script that just echoes its arguments:
#!/usr/bin/env ruby
puts "The arguments were:"
ARGV.each { |curr_arg| puts curr_arg }
Save this to foo.rb, then chmod a+x foo.rb. You can either move it to some location on your PATH, in which case you can just type foo.rb some args from anywhere, or you can run it explicitly from the current directory with either ./foo.rb some args or ruby foo.rb some args.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have written a program in ruby. The output correspondng to which is really long, a couple of thousand lines.
Is there a way I can write this output to a file and not in the shell, because the shell allows you to scroll up only till a certain point? I know that I can use file.puts in place of puts. But, I want to know if there is a command using which I can achieve this without making changes in the program? For shell scripts we can do script.sh > output.txt. Similarly for a shell command as well. But what about a ruby program? It doesn't seem to be working.
The problem is that ./program.rb > output.txt redirects the output as well as the prompts for input into the text file, so you can't see what you're doing. You have several options:
Use STDERR.puts to prompt for input, so it doesn't get caught by the redirection. This is my preferred method.
Use ARGV to pass input to your program. This is a good option if you think the program isn't too hard to use without prompts.
Use File.open to create an output file and write to it directly. Sometimes this is the most sensible option, but usually you want to do one of the first two.
If the program is asking for input, maybe you can change your program to accept input from the command line (ARGV[0] etc), then redirect the output to a file :
ruby myprogram > out.txt
This worked for a simple puts output:
foo.rb:
foo = gets.chomp
puts "Input was: " + foo
Terminal:
ruby foo.rb > test.txt
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I make calls to the command line using the back tick and variables? Something like:
myvar = "C:\Program Files"
`cd ` + myvar
Also, consider using a system() call, for clarity. Backticks are for short commands.
system allows for a visually-more-obvious open + close block formatting that befits large, or multi-line OS instructions.
See this SO Q+A
Though, if you're writing large OS scripts, put them in a shell file, check it into VCS, and exec that with a ruby one-liner.
Try this:
`cd "#{myvar}"`
Example:
$ irb --simple-prompt
>> `pwd`
=> "/home/kirti\n"
>> var = 'ruby'
=> "ruby"
>> `cd "#{var}" && pwd`
=> "/home/kirti/ruby\n"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I wont write some script in Ruby on Linux server. I need statistic from server and I'm a beginner in Ruby.
I have problem with Linux commands, because if I use exec to use Linux command, my program is fallen without error.
disks = ["sda", "sdb"]
Code:
disks.each do |disk|
puts "disk test start"
exec "smartctl -a /dev/#{disk} > /tmp/sestavy/#{disk}"
puts "disk test end"
end
Output:
[root#banan sestavy]# ruby test.rb
disk test start
[root#banan sestavy]#
Thanks
Honza
That's just what exec does: it replaces the currently running program with a new one. This is not specific to Ruby, it works the same way in the shell, in C, in pretty much any other environment.
When you use exec, it replaces the current process with what you want to execute. So it won't return to your Ruby script. See this explanation for different methods for shell execution.