Ruby input/output

From wikinotes
Revision as of 23:42, 7 June 2020 by Will (talk | contribs) (→‎files)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

printing

puts (print with newline)

puts "hello"

print (print w/o newline)

print "a", "b", 12

printf

printf "num: %5.2f  name: %s", 1.123, "alex"
#> "num: 1.12  name: alex"

input

line = gets
puts line

stdin/stdout/stderr

ARGF  # stdin

files

File.open('/var/tmp/file.txt', 'a') do |fd|
  fd.write("foo")
end

Dir.exist?('/directory')   # directory exists
File.exists?('/file.txt')  # file exists