IO streams inconsistencies in different versions of ruby - ruby

I am trying to create a way to mirror all three standard (input,out and error) streams of a ruby program to different files.
I was able to do that for output stdout and error stderr streams.
However there is inconsistencies between different ruby version on how they handle IO streams.
Bottom line is
How do I handle stream duplication in older versions of ruby (point 2) ?
Issue Summary
Older ruby versions don't support variadic arguments while new version does, that can be handled.
On same older versions, even if I try to work around it by join ing arguments, stream contents are duplicated.
Variadic arguments issue
In ruby 2.1.x and ruby 2.2.x, $stdout accepts only single arguments.
So for those version of ruby
$stdout.write("Test") # works
while this doesn't work
$stdout.write("Hello","Test")
ArgumentError: wrong number of arguments (2 for 1)
from (pry):6:in `write'
However for ruby version 2.5.x and 2.6.x variable parameters works
$stdout.write("Hello")
$stdout.write("This","works", "as", "well")
Duplication of stream content, if I add version specific code to handle stream parameters, older ruby version end up duplicating the stream content.
Older ruby version
driver.rb:4 in `puts` --> This is it driver.rb:4 in `puts` -->
while on
New ruby versions
driver.rb:4 in `puts` --> This is it
Here is the code
reox.rb
require_relative 'reox_io.rb'
require_relative 'reox_file.rb'
class Reox
DEFAULT_RECORDING_PATH = "recordings"
def initialize(recording_path)
#recording_paths = recording_path || DEFAULT_RECORDING_PATH
end
def self.record
input_log,output_log,error_log = setup_files
$stdout = ReoxIO.new(STDOUT,output_log)
$stderr = ReoxIO.new(STDERR,error_log)
yield
end
private
def self.setup_files
["input.log","output.log","error.log"].map do |file_name|
self.setup_file(file_name)
end
end
def self.setup_file(file_name)
file = ReoxFile.open(File.join(__dir__,file_name),"a")
file.sync = true
file
end
end
reox_file.rb
class ReoxFile < File
end
reox_io.rb
# Insipired from https://stackoverflow.com/a/6407200
require_relative 'reox_file.rb'
class ReoxIO
$LOG_SEPARATOR = " --> ".freeze
def initialize(*streams)
#streams = streams
#verbose = true
end
def write(*content)
#streams.each do |stream|
if #verbose and stream.is_a? ReoxFile
stream.write([caller[2],$LOG_SEPARATOR,*content].join)
else
stream.write(*content)
end
end
end
def flush
#streams.each(&:flush)
end
def close
#streams.each(&:close)
end
end
driver.rb
require_relative 'reox.rb'
Reox.record do
puts "This is it"
end

Related

Upgade to Ruby 3.1 breaks code when using CSV class from standard library

I'm upgrading a Project written for JRuby 1.7 (corresponding on the language level to Ruby 1.9) to JRuby 9.4 (corresponding to Ruby 3.1.0). In this code, we have
require 'csv'
....
CSV.parse(string, csv_options) { .... }
where string is of class String and csv_optionsis of class Hash. This statement produces, when run under the new Ruby version, the error
ArgumentError:
wrong number of arguments (given 2, expected 1)
I found in the Ruby docs the following difference in the definition of parse:
Old version:
def parse(str, options={}, &block)
New version
def parse(str, **options, &block)
I understand that in the new Ruby, I would have to invoke parse as
CSV.parse(string, **csv_options) {....}
However, I would like to keep the code compatible for both versions, at least for some transition period, but the old JRuby does not understand **variable (I would get a syntax error, unexpected tPOW).
Is there a way to write the invocation of CSV.parse in such a way, that it preserves the original semantics and can run under Ruby 1.9 and Ruby 3.1? Currently the best solution for this problem which I can think of, is to write something like turning the block argument into a proc and writing
if RUBY_VERSION < '2'
CSV.parse(string, csv_options, &myproc)
else
# Without the eval, the compiler would complain about
# the ** when compiled with Ruby 1.9
eval "CSV.parse(string, **csv_options, &myproc)"
end
which looks pretty awful.
Not sure exactly what you are passing as csv_options but all versions can handle this using an a combination of implicit Hash/kwargs. e.g.
CSV.parse(string, col_sep: '|', write_headers: false, quote_empty: true) { ... }
If this is not an option then you going to need to patch the CSV class to make it backwards compatible e.g.
csv_shim.rb
# Shim CSV::parse to mimic old method signature
# while supporting Ruby 3 kwargs argument passing
module CSVShim
def parse(string, options={}, &block)
super(string, **options, &block)
end
end
CSV.singleton_class.prepend(CSVShim)
Then you can modify as:
require 'csv'
require 'csv_shim.rb' if RUBY_VERSION > '2.6.0'
#...
CSV.parse(string, csv_options) { .... }

Suppress warning messages in Thor CLI app

I've built a Thor CLI app that uses a number of external gems. When I run it I get warning messages from those gems cluttering my output- how can I suppress this?
Clarification: I want to suppress the warning messages only, but still receive the standard output for my app, including errors and puts results.
For example, when I use these same gems in my Sinatra app, I don't get all the warning messages emanating from the gem code like I do with Thor.
EXAMPLE (Derived from http://willschenk.com/making-a-command-line-utility-with-gems-and-thor/)
require 'thor'
require 'safe_yaml'
module Socialinvestigator
class HammerOfTheGods < Thor
desc "hello NAME", "This will greet you"
long_desc <<-HELLO_WORLD
`hello NAME` will print out a message to the person of your choosing.
HELLO_WORLD
option :upcase
def hello( name )
greeting = "Hello, #{name}"
greeting.upcase! if options[:upcase]
puts greeting
end
end
end
In this case, because we're requiring the safe_yaml gem, every time we run a command we'll get the following warnings in our output:
/usr/local/lib/ruby/gems/2.3.0/gems/safe_yaml-1.0.4/lib/safe_yaml.rb:28:
warning: method redefined; discarding old safe_load
/usr/local/Cellar/ruby/2.3.0/lib/ruby/2.3.0/psych.rb:290: warning:
previous definition of safe_load was here
/usr/local/lib/ruby/gems/2.3.0/gems/safe_yaml-1.0.4/lib/safe_yaml.rb:52:
warning: method redefined; discarding old load_file
/usr/local/Cellar/ruby/2.3.0/lib/ruby/2.3.0/psych.rb:470: warning:
previous definition of load_file was here
We're using a number of different gems and getting a whole array of warnings that are cluttering our output...
Firstly, you could potentially submit a Pull request and suppress the message in the dependency or raise an issue asking the gem developers to sort it out for you
Otherwise, this is something I have used before - It's probably from somewhere on SO (or the internet in general) but I can't remember where...
So you basically wrap the noisy method from the dependency with a silence method which simply pushes the STDOUT to a StringIO object and then back again to STDOUT...
require 'stringio'
def silence
$stdout = temp_out = StringIO.new
yield
temp_out.string
ensure
$stdout = STDOUT
end
out = silence { run_dependency_method }
puts out # if in dev mode etc...
Ruby has a global variable to define the verboseness of the output. nil means no warnings, false is the "normal" mode and true is extra verbose (adds some additional runtime information, equivalent to running ruby --verbose):
def without_warnings
verboseness_level = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = verboseness_level
end
# Doesn't show warnings about overwriting the constant
without_warnings do
Foo = 42
Foo = 'bar'
end
# Still shows normal output
without_warnings { puts 42 }
# Still shows and throws errors
without_warnings { raise 'wtf' }
If you have control over how the program is ran, you can instead use ruby's -W flag with respective values 0, 1 or 2 (in this case ruby -W 0).
#Yarin,
Following up on #Ismail's answer, you could extend this solution by overloading the new "temporary" StringIO inside the silence method to filter out messages that contain the word "warning".
Here's an example:
require 'stringio'
class FooIO < StringIO
def puts s
super unless s.start_with? "WARN"
end
end
def silence
$stdout = temp_out = FooIO.new
yield
temp_out.string
ensure
$stdout = STDOUT
end
def foo
puts "INFO : Hello World!"
puts "WARN : Oops.. This is a warning..."
puts "ALRT : EVERYONE IS GOING TO DIE!!!"
end
out = silence { foo }
puts out

Mocha expect failing with Thor 0.19.1

After upgrading Thor from 0.18.1 to 0.19.1, I'm seeing a weird failure of Mocha (using test-unit)
/gems/2.1.0/gems/test-unit-2.5.5/lib/test/unit/ui/console/testrunner.rb:395:
in `output': unexpected invocation: #<IO:0x7fd6c986ab58>.puts() (Mocha::ExpectationError)
unsatisfied expectations:
- expected exactly once, not yet invoked: #<IO:0x7fd6c986ab58>.puts('1.0.0')
from rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/test-unit-2.5.5/lib/test/unit/ui/console/testrunner.rb:389:in `nl'
Code:
def version
say VERSION
end
Test:
def test_should_print_version
$stdout.expects(:puts).with(VERSION)
App::CLI.start %W(version)
end
Interestingly, $stdout.expects(:print).with(VERSION + "\n") works without issues. I'm using ruby 2.1.1p76
It seems that the first exception is due to puts being called and the second is due to it not being called. Should I be using expect in a different way?
I worked around the issue by redirecting $stdout to an instance of StringIO as suggested here
require 'stringio'
module Kernel
def capture_stdout
out = StringIO.new
$stdout = out
yield
return out
ensure
$stdout = STDOUT
end
end
Updated test:
def test_should_print_version
#$stdout.expects(:puts).with(VERSION)
out = capture_stdout { App::CLI.start %W(version) }
assert_match VERSION, out.string
end

How to log everything on the screen to a file?

I use one rb file with rufus/scheduler on Windows. The script is executed on a comupter start up and it runs in a cmd window.
How can I log everything that ruby outputs to the screen to a file? I still want to be able to see the output on the screen. So I want the logging on top of current behaviour.
Windows 7 64 bit
ruby 1.9.3p194 (2012-04-20) [i386-mingw32]
If you just want the script to send output to the file instead of the console use IO#reopen to redirect stdout and stderr.
def redirect_console(filename)
$stdout.reopen(filename,'w')
$stderr.reopen(filename,'w')
end
redirect_console('/my/console/output/file')
If you need to direct to one or more output streams, use a proxy object and method_missing to send to them
class TeeIO
def initialize(*streams)
raise ArgumentError, "Can only tee to IO objects" unless streams.all? { |e| e.is_a? IO }
#streams = streams
end
def method_missing(meth, *args)
# HACK only returns result of first stream
#streams.map {|io| io.send(meth, *args) }.first
end
def respond_to_missing?(meth, include_all)
#streams.all? {|io| io.respond_to?(meth, include_all) }
end
end
def tee_console(filename)
tee_to = File.open(filename, 'w')
tee_to.sync = true # flush after each write
$stdout = TeeIO.new($stdout, tee_to)
$stderr = TeeIO.new($stderr, tee_to)
end
tee_console('/my/console/output/file')

How can I read Bytes from a File in Ruby 1.9?

In Ruby 1.9 the File and IO libraries were changed -- they now seem to always interpret the data as encoded strings (e.g. UTF-8), and the returned values seem to be always strings.
I need to read a file in Ruby 1.9 byte by byte, without any modification or interpretation of the data.
I want to read byte sequences, not encoded strings.
Any tips on how to best do this?
I had a similar problem in a gem I wrote. Here's the relevant code:
(you don't need the require statements)
# ==============================================================================
# Loading Libraries and Stuff needed for Ruby 1.9 vs 1.8 Compatibility
# ==============================================================================
# the idea here is to define a couple of go-between methods for different classes
# which are differently defined depending on which Ruby version it is -- thereby
# abstracting from the particular Ruby version's API of those classes
if RUBY_VERSION >= "1.9.0"
require "digest/md5"
require "digest/sha1"
include Digest
require 'fileutils' # replaces ftools
include FileUtils::Verbose
class File
def read_bytes(n) # returns a string containing bytes
self.bytes.take(n)
end
def write_bytes(bytes)
self.syswrite(bytes)
end
def get_byte
self.getbyte # returns a number 0..255
end
end
ZEROBYTE = "\x00".force_encoding(Encoding::BINARY) unless defined? ZEROBYTE
else # older Ruby versions:
require 'rubygems'
require "md5"
require "sha1"
require 'ftools'
def move(a,b)
File.move(a,b)
end
class String
def getbyte(x) # when accessing a string and selecting x-th byte to do calculations , as defined in Ruby 1.9
self[x] # returns an integer
end
end
class File
def read_bytes(n)
self.read(n) # should use sysread here as well?
end
def write_bytes(bytes)
self.write(bytes) # should use syswrite here as well?
end
def get_byte # in older Ruby versions <1.9 getc returned a byte, e.g. a number 0..255
self.getc # returns a number 0..255
end
end
ZEROBYTE = "\0" unless defined? ZEROBYTE
end
IO has the binmode method (calling it is setting it), which disables newline and encoding conversion. The File class inherits this method.

Resources