Ruby input/output: Difference between revisions

From wikinotes
 
 
(2 intermediate revisions by the same user not shown)
Line 32: Line 32:
</source>
</source>
</blockquote><!-- stdin stdout stderr -->
</blockquote><!-- stdin stdout stderr -->
= file-like objects =
<blockquote>
<syntaxhighlight lang="ruby">
fd = StringIO.new("a\nb\nc")
fd.seek(0)
fd.readlines
</syntaxhighlight>
</blockquote><!-- file-like objects -->
= pipes =
<blockquote>
<syntaxhighlight lang="ruby">
r_pipe, w_pipe = IO.pipe
</syntaxhighlight>
</blockquote><!-- pipes -->


= files =
= files =
Line 41: Line 57:


Dir.exist?('/directory')  # directory exists
Dir.exist?('/directory')  # directory exists
File.exists?('/file.txt')  # file exists  
File.exists?('/file.txt')  # file exists
</source>
</source>
</blockquote><!-- files -->
</blockquote><!-- files -->

Latest revision as of 15:49, 25 January 2023

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

file-like objects

fd = StringIO.new("a\nb\nc")
fd.seek(0)
fd.readlines

pipes

r_pipe, w_pipe = IO.pipe

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