Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pretty print to a variable instead of STDOUT?

Tags:

ruby

I'd like to use Ruby's pp to "print" to a variable instead of to STDOUT. How is that done?

like image 412
Suan Avatar asked Jan 02 '13 18:01

Suan


People also ask

How do you assign a print to a variable?

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 .

How do I print a variable in stdout?

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.

How do you overwrite stdout in python?

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.


1 Answers

require 'pp'
result = PP.pp(some_variable, "")
like image 121
Jeremy Roman Avatar answered Sep 17 '22 22:09

Jeremy Roman