Putting the results of pp (or anything outputted to console) into a string
string_value = a.pretty_inspect
#pretty_inspect
also comes along when you first require 'pp' - See: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/Kernel.html#method-i-pretty_inspect
If you want the version that is outputted to the irb
console that is
string_value = a.inspect
and doesn't have any requires necessary.
Redirect console output to string in Java
If the function is printing to System.out
, you can capture that output by using the System.setOut
method to change System.out
to go to a PrintStream
provided by you. If you create a PrintStream
connected to a ByteArrayOutputStream
, then you can capture the output as a String
.
Example:
// Create a stream to hold the output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
// IMPORTANT: Save the old System.out!
PrintStream old = System.out;
// Tell Java to use your special stream
System.setOut(ps);
// Print some output: goes to your special stream
System.out.println("Foofoofoo!");
// Put things back
System.out.flush();
System.setOut(old);
// Show what happened
System.out.println("Here: " + baos.toString());
This program prints just one line:
Here: Foofoofoo!
Intercepting all puts and print statements from a Ruby program
Easy. By default puts
and print
output to the $stdout
I/O channel. Reassign $stdout
to a different file handle and output of those commands will go to the new channel.
Change where $stdout
points.
File.open('spool.out', 'wb') do |fo|
$stdout = fo
puts 'hello world'
$stdout = STDOUT
end
Save that to a file and run it. You should see a file appear called "spool.out" containing "hello world".
It's not necessary to wrap everything in a File.open block. All that's important is you reassign $stdout
to a file handle, then reset it later, so it could also be done like this:
$stdout = File.open('spool.out', 'wb')
puts 'hello world'
$stdout.close
$stdout = STDOUT
At startup, a Ruby script has access to a number of different global variables and constants: $stdout
will be the same as STDOUT
, and $stderr
will be the same as STDERR
.
See "Does Ruby use $stdout for writing the output of puts and return?" and "Putting the results of pp (or anything outputted to console) into a string" for more information.
Nicely formatting output to console, specifying number of tabs
There is usually a %10s
kind of printf
scheme that formats nicely.
However, I have not used ruby at all, so you need to check that.
Yes, there is printf with formatting.
The above example should right align in a space of 10 chars.
You can format based on your widest field in the column.
printf ([port, ]format, arg...)
Prints arguments formatted according to the format like sprintf. If the first argument is the instance of the IO or its subclass, print redirected to that object. the default is the value of $stdout.
How to Save Console.WriteLine Output to Text File
Try this example from this article - Demonstrates redirecting the Console output to a file
using System;
using System.IO;
static public void Main ()
{
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Redirect.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine ("Cannot open Redirect.txt for writing");
Console.WriteLine (e.Message);
return;
}
Console.SetOut (writer);
Console.WriteLine ("This is a line of text");
Console.WriteLine ("Everything written to Console.Write() or");
Console.WriteLine ("Console.WriteLine() will be written to a file");
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine ("Done");
}
Is there a way to print a console message with Flutter?
print()
is probably what you are looking for. Here's some more info on debugging in flutter.
C# console app finishing with code 0 instead of providing the output it's supposed to calculate
Console.WriteLine(String, Object)
method signature requires the string include a format character, which you don't have.
If you want to use String interpolation, then that would look like
Console.WriteLine($"The future value of your investment is: {futureCapital}");
Related Topics
Create Custom HTML Helpers in Ruby on Rails
Is Ruby Really an Interpreted Language If All of Its Implementations Are Compiled into Bytecode
How to Get the Last SQL Query Performed by Activerecord in Ruby on Rails
Net::Ssh Sudo Command Hangs After Entering Password
Windows/Ruby/Rails Install --- .Cannot Load Such File -- SQLite3/Sqlite3_Native Windows
Problems Setting a Custom Primary Key in a Rails 4 Migration
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Bundle Install Issue with Libv8 and Rails
Combining Multiple Named Scopes with Or
Ruby Cannot Load Such File - Active_Support/Core_Ext/Object/Blank
Sort a Collection of Objects by Number (Highest First) Then by Letter (Alphabetical)
Railstutorial - Chapter 8.4.3 - Test Database Not Clearing After Adding User in Integration Test