Ruby: How to run "dpkg-reconfigure" inside code? - ruby

In my Ruby code, I am trying to call "dpkg-reconfigure" but it won't show.
#!/usr/bin/env ruby
`dpkg-reconfigure ca-certificates`
The program just hangs up without GUI interface.
Thank you for the hint.

Use system, instead of capturing the output using backticks.
system "dpkg-reconfigure ca-certificates"

Related

Ruby script equivalent to the bash `set` builtin's `-x` flag?

When a bash script is running, the set -x command can be used to print all commands and output as they're being executed.
I'm writing a Ruby script. Is there a way to also have this print the commands and output as they're being executed?
TL;DR
While interpreted, Ruby parses and runs code very differently from languages like Bash or Tcl, so there's no built-in way to do exactly what you want. You'll have to use a debugger or REPL to get something that approximates what you're trying to do, but it won't really be the same as using flags like -x or -v in Bash. An external or IDE-based debugger will probably come closest, though.
A Couple of Options
There is no built-in way to do this, as Ruby is not really a line-by-line interpreted language in the same way as Bash or Tcl. While Ruby is generally considered an interpreted language, it actually uses a tokenizer and parser to generate code that runs on a virtual machine such as YARV or GraalVM. You do have a couple of options, though:
Use the -d flag or set $DEBUG to a truthy value in your code, and then do some level of introspection based on whether the debug flag is enabled. For example:
# 1 is printed because $DEBUG is truthy
$ ruby -e 'BEGIN { $DEBUG = true }; puts 1 if $DEBUG'
1
# nothing is printed because $DEBUG is falsey
$ ruby -e 'puts 1 if $DEBUG'
Please note that Ruby 3.0.3 and 3.1.0 seem to have an issue with the -d flag, so the first example uses a BEGIN statement to set the value of the flag inside the program.
Use the debug gem (now standard with Ruby 3). You can either step through the code with rdbg and use the list command liberally, or (if you're clever) script a series of list commands on specific lines using the ~/.rdbgrc file.
Use an external debugger, with or without rdbg. Note that the new debugger supports IDE-based debugging (e.g. with RubyMine or VS Code) and remote debugging, but setting up IDE or remote debugging is likely a topic outside the scope of a reasonable SO answer.
Use irb or pry with the debugger of your choice, which usually gives you a number of ways to inspect source code, frames, expressions, variables, and so on, although you need to run from an on-disk file rather than a REPL to access some of the functionality you may be looking for.
For the most part, if you're not using an IDE or a debugger, you will generally need to rely on return values in a REPL or Kernel#pp statements in your code to inspect return values as you go along. However, short of a debugger or REPL that supports listing methods or lines of code on request, you'll either need to use external tools to solve whatever problem you're trying to solve via this approach another way.
Other Options
If you use pry, the pry-rescue gem along with pry-stack_explorer will allow you to automatically trigger a REPL session that allows you to traverse up and down the stack if you hit an exception without requiring you to start your session in the REPL or explicitly call binding.pry. On supported versions of Ruby, this can be very useful, especially since Pry supports a show-source -l command that will do something similar to what you want (at least interactively), although the line numbers may not be what you expect if the code is entered directly in the REPL rather than loaded from a Ruby program on disk.

Ruby on Windows: Require is not recognized as an internal or external command

I'm trying to use Guard to monitor changes in AsciiDoctor file. Here is Guardfile from official docs:
require 'asciidoctor'
guard 'shell' do
watch(/^mydoc\.adoc$/) {|m|
Asciidoctor.convert_file m[0]
}
end
It works for me. But now, I'm trying to launch the same things without creating Guardfile - i.e. I want simply write these commands in Windows cmd.exe.
But when I write require 'asciidoctor' command prompt gives me an error:
'require' is not recognized as an internal or external command, operable program or batch file.
Well, I know that such error messages are often have something with Windows %Path% environment variable. But I don't understand how to fix it in this particular case.
Ruby and DOS Batch are two completely different programming languages that have absolutely nothing to do with each other. You simply cannot expect an interpreter for DOS Batch to be able to run Ruby code and vice versa. (Especially considering that Ruby didn't even exist when CMD.EXE was written, so how could CMD.EXE possibly know how to interpret Ruby code?)
You need to run Ruby code in a Ruby interpreter (or use a Ruby compiler to compile it to something that you have an interpreter for).

How to read from terminal in lazarus on ubuntu

Lazarus can run bash scripts and commands. How to get the output of an executed command as string and later use it, for example print it with ShowMessage? Thanks!
Summary:
use the tprocess class from unit process, that allows to trap console output using pipes.
for straightforward cases use the Runcommand helper functions (also in process, wrap tprocess for simple cases)
Be aware that while you see console output as one stream, in fact there might be two (stdout and stderr)

Ruby - How to use -r switch with ruby command line tool

I was trying to figure out how to work the command line switch -r.
My understanding is that the code is typed out as follows:
ruby -r*nameOfRequired*
I am finding that this is not the case. When I type out the above and press enter, the terminal expects an "end of input syntax" and does not continue.
What am I missing? Does there need to be a space in between the switch and the name of the required file?
Please and thank you!
EDIT:
I am currently reading "The Well Grounded Rubyist" by David A. Black, and I came up with this question while reading the section on command line switches.
Having said that, I created a "test.rb" file, containing:
puts Date.today
Then, in the terminal, I typed out:
ruby -r date
I thought this would 'require' the date module, and then enable me to run the "test.rb" file, using ruby test.rb (given that I am in the correct directory).
Instead, the terminal cursor moves to a newline, expecting more input. Let me know if I need to clarify anything else. Thanks!
If you just type ruby -rmodule, then Ruby will load the module and wait for you to type the main program that requires that module.
If you just want to run the module and do nothing else, you can do do rubyfull-path-to-module without the -r, or ruby -rmodule -e exit, or ruby -rmodule </dev/null, or similar.
In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.
Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.
Try this:
ruby -rdate test.rb
According to ruby -h:
-rlibrary require the library, before executing your script
Without giving your script file path, it read the script from stdin.
Try following (You can omit script file path when you give -e command):
ruby -r**nameOfRequired** -e ""

Calling ruby script from groovy

I have a groovy script and I need to call a ruby script.
I would like to pass arguments to Ruby script and would like to capture the output from Ruby script to use it in Groovy Script. Can somebody suggest how I can do this? I tried Process.execute(). It works for other dos commands but not for cmd /c ruby test.rb.
Since a ruby file isn't a batch file you don't need to use cmd to execute it. You could do
Process.execute("ruby.exe test.rb")
Assuming ruby.exe is on your path. Another option depending on your requirements may be to use JRuby which will allow you to run ruby code on the JVm and integrate nicely with Groovy.

Resources