I'd like to use Ruby's pp
to "print" to a variable instead of to STDOUT. How is that done?
To assign the output of the print() function to a variable:Remove the call to the function and assign the argument you passed to print() to the variable. The print() function converts the provided value to a string, prints it to sys. stdout and returns None .
To write to stdout, use (%stdout) as the special device. But, if you're writing to stdout, just use = or print (if you don't want a newline). To actually use writestring and closefile , Put the file first, then dup it, then put the string on the stack.
Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within the print statement as print(string, end = "\r") . This returns the next stdout line to the beginning of the line without proceeding to the next line.
require 'pp'
result = PP.pp(some_variable, "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With