Puts Doesn't Print Stuff to Console

puts doesn't print stuff to console

Instead of puts, try logger.info(). Logging in Rails is very flexible, but it does mean that you might not be able to use the simplest tools sometimes.

How can I print data using the puts statement to development.log file in rails?

Example (from inside of an controller):

def some_method
pi = 3.1514
logger.debug "Debug message"
logger.info "Info message"
logger.info "PI equals #{pi}"
end

There are 5 types of messages: DEBUG, INFO, WARN, ERROR, FATAL

I recommend reading this chapter from Rails Guides: 2.3 Sending Messages

File stream is opened but nothing is printed to console. Program doesn't seem to finish

Make sure that the coc.txt file and the read.c files are in the same folder. I executed your original code and it works fine with VS 2017 on windows 10.

printf doesn't print a string immediately

The output stream stdout is buffered by default, so if you want immediate output you'll need to flush the output stream - using fflush - or cause a newline to be printed in the printf:

printf("Starting nets allocation...");
fflush(stdout);

Or:

printf("Starting nets allocation...\n");

Note that you can also control buffering at a file pointer level using the setbuf function from stdio.h:

setbuf(stdout, NULL);

The second argument to setbuf is a buffer supplied by the caller to be used for buffering output to the stream. Passing NULL indicates that buffering is to be disabled, and is equivalent to:

setvbuf(stdout, NULL, _IONBF, 0);

which also disables buffering on the specified stream.

See docs for setbuf here.

Stop rails console from printing out the object at the end of a loop

If you don't want to disable the echo in general you could also call multiple expressions in one command line. Only the last expression's output will be displayed.

big_result(input); 0

Console keeps waiting for input right after executing without doing anything prior the gets statement in Ruby

It seems that the standard output is buffered.

Try to put at the beginning of the file (first two lines) old_sync = $stdout.sync $stdout.sync = true and a the end of the file (last line) $stdout.sync = old_sync.

The call to the IO#sync= method set the sync mode to true. This cause that all output is immediately flushed, at the end of the script we restore its value to its original old value, see Ruby documentation for details.

In summary:

old_sync = $stdout.sync # cache old value
$stdout.sync = true # set mode to true

# your scripting staff

$stdout.sync = old_sync # restore old value

If this trick works at least you know the reason for the weird behaviour. You can find some explanation also in this SO post.

Prevent ruby/rails from printing results of assignment expressions

Edit:

A version of the MR referenced below was merged to irb, but tweaked a few times since then. So to get the behavior you want it depends on the irb version:

If you have at least 1.2.0, but less than 1.2.6: the default behavior is to not print anything for assignment statements. You have to set IRB.conf[:ECHO_ON_ASSIGNMENT] = true if you want the old behavior (of always printing everything).

If you have 1.2.6, the default value for ECHO_ON_ASSIGNMENT was changed to true, and a new config value: OMIT_ON_ASSIGNMENT was added which will truncate long output for assignment statements, it defaults to true. So you have to set IRB.conf[:ECHO_ON_ASSIGNMENT] = false if you don't want anything to be printed for assignment statements. You have to set IRB.conf[:OMIT_ON_ASSIGNMENT] = false if you want the old behavior (of always printing everything).

If you have 1.2.7 or above, OMIT_ON_ASSIGNMENT was removed in favor of letting ECHO_ON_ASSIGNMENT accept true, false, or :truncate, with the default value being :truncate. A value of true will print everything for assignment statements, false will print nothing, and :truncate will print a truncated version (like setting OMIT_ON_ASSIGNMENT to true in 1.2.6).

Note that the versions above versions of the irb gem, not ruby versions (even though irb is included in ruby's standard library). You can check what version of irb you have by looking at IRB::VERSION. Very old versions of irb don't set this constant, so if it's missing, you definitely have a version lower than 1.2.0.

Original answer:

Until this pull request (or one like it) is merged, you can put the following into your ~/.irbrc file:

require 'ripper'
module IRB
class Irb
ASSIGNMENT_NODE_TYPES = [:assign, :opassign, :massign]
def assignment_expression?(line)
ASSIGNMENT_NODE_TYPES.include?(Ripper.sexp(line)&.dig(1,-1,0))
end

def output_value # :nodoc:
return if assignment_expression?(@context.last_line)
printf @context.return_format, @context.inspect_last_value
end
end

class Context
attr_accessor :last_line
def evaluate(line, line_no, exception: nil) # :nodoc:
@line_no = line_no
@last_line = line
if exception
line = "begin ::Kernel.raise _; rescue _.class; #{line}; end"
@workspace.local_variable_set(:_, exception)
end
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
end
end
end

Note that since this monkey-patches IRB::Irb and IRB::Context, there's no guarantee that it will be compatible with future releases.

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.



Related Topics



Leave a reply



Submit