ROR console output to text file
Use the private key in accessing the ROR server and insert it into filezilla.
By then login thru filezilla, hostname should be ROR host and user type should be interactive.
rails - Redirecting console output to a file
You can use override $stdout
to redirect the console output:
$stdout = File.new('console.out', 'w')
You may also need to call this once:
$stdout.sync = true
To restore:
$stdout = STDOUT
How do I save the text of puts in Ruby to a txt file?
There are ways to save the output of a script to a file without having to modify every puts
in the script.
The easiest is to route the output at the command-line using redirection. Running a script with > some_file
at the of the command will route all STDOUT to the file. Similarly, using > some_file 2>&1
will route both STDOUT and STDERR to the same file. This won't capture anything typed in at a gets
as the code waits for input though, because that won't count as program output.
If you don't mind changing your code a little, you can temporarily change the interpreter's idea of what STDOUT is by reassigning it to a file:
old_stdout = $stdout
File.open('output.txt', 'w') do |fo|
$stdout = fo
# ----
# your code goes here
puts "hello world"
# ----
end
$stdout = old_stdout
Run that, then look at the file "output.txt" and you'll see "hello world", even though we didn't print to the file-handle fo
directly, like we would normally do using fo.puts
.
There are a variety of ways of doing the same thing but they amount to pointing STDOUT or STDERR somewhere else, writing to them, then resetting them.
Typically, if we intend from the start to output to a file, then we should use a File.open
block:
File.open('output.txt', 'w') do |fo|
fo.puts "hello world"
end
The benefit of that is the file will be closed automatically when the block exits.
Ruby - how to write console input to file
The following code works for me :
begin
File.open(ARGV.shift, 'w') do |f|
while a = gets.chomp
p a
break if a == 'exit'
f.puts a
end
end
rescue SignalException
puts
puts "#Completed#"
exit
end
It should be noted that I'm using ARG.shift
instead of ARG[0]
because when using the latter gets
will access whatever is in ARG
before moving on the STDIN
. ARG.shift
removes the original argument.
Print editable to console in Ruby
There are similar questions here and here
However, the solutions there don't seem to work as expected, so it looks this is ruby version or platform dependent?
For example, this does not work for me, but also does not throw an error.
require "readline"
filename = Readline.insert_text("untitled.txt").readline("Enter a filename:")
print filename
But since it looks much better, and should work according to the documentation for ruby >= 2, I am leaving it there for now.
The following works on my system (ruby 2.3.1, OS X)
require "readline"
require 'rb-readline'
module RbReadline
def self.prefill_prompt(str)
@rl_prefill = str
@rl_startup_hook = :rl_prefill_hook
end
def self.rl_prefill_hook
rl_insert_text @rl_prefill if @rl_prefill
@rl_startup_hook = nil
end
end
RbReadline.prefill_prompt("untitled.txt")
str = Readline.readline("Enter a filename:", true)
puts "You entered: #{str}"
How to use open() in irb console to print out basic text from .txt file? #Ruby
I have created a file ex15_sample.txt in .../Ruby/zintlist/irb.
1.8.6 :082 > File.open("ex15_sample.txt")
Errno::ENOENT: No such file or directory - ex15_sample.txt
from (irb):82:in `initialize'
from (irb):82:in `open'
from (irb):82
from :0
1.8.6 :086 > Dir.getwd
=> "/.../Ruby/prod/spec"
1.8.6 :087 > Dir.chdir('../../zintlist/irb')
=> 0
1.8.6 :088 > Dir.getwd
=> "/.../Ruby/zintlist/irb"
1.8.6 :089 > File.open("ex15_sample.txt")
=> #<File:ex15_sample.txt>
1.8.6 :090 >
attempting File.open("ex15_sample.txt") I assume it opens
Within irb, usually you don't need to assume, you have an immediate answer.
1.8.6 :090 > txt = File.open("ex15_sample.txt")
=> #<File:ex15_sample.txt>
1.8.6 :091 > puts txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
=> nil
1.8.6 :092 >
How to write to file in Ruby?
The Ruby File class will give you the ins and outs of ::new
and ::open
but its parent, the IO class, gets into the depth of #read
and #write
.
Writing the output of loop into a text file from a Ruby web crawler gem
You can puts
to a file handle, almost the same as if it was STDOUT. A very simple adjustment to your code is to add a File.open
block:
require 'anemone'
File.open('report.txt', 'w') do |file|
Anemone.crawl("http://www.example.com/") do |anemone|
anemone.on_every_page do |page|
file.puts page.url
end
end
end
Related Topics
How to Convert PDF to Excel or CSV in Rails 4
Project Euler #3 in Ruby Solution Times Out
Exclude Option from Collection.Map in Ruby on Rails
Detecting Regional Settings (List Separator) from Web
How to Catch Top Level Failures on an Eventmachine Server
Rake Aborted! Undefined Method 'Migration_Error=' for Activerecord::Base:Class
Why Is My Ruby Git Script Hook Run with the Wrong $Path
Error Creating Rails Db Using Rake Db:Create
Can't Setup Ruby Environment - Installing Fii Gem Error
What's Does the [5.0] in Rails 5's Activerecord::Migration Mean
How to Convert a Ruby Bigdecimal to a 2-Decimal Place String
Svg Files in Raphael, Can They Be Used