せかいや

いまいるここを、おもしろく http://sekai-in-the-box.appspot.com/

【Ruby】標準入力、ファイル入力出力、をテンプレで整理

 
標準入力、ファイル入力出力、いつもなんだっけって思うから、整理しました。

foo = File.open("foo.txt",'a+') do |file|
  while line = file.gets
    line += "dayo"
  end
end



__END__

File::basename('/hoge/piyo')   #<= piyo
File::dirname('/hoge/piyo')    #<= /hoge

__END__

foo = File.open("foo.txt",'w') do |file|
  5.times{|i| file.puts i}
end

#追加書き込み
foo = File.open("foo.txt",'a') do |file|
  5.times{|i| file.puts i}
end


__END__

File.open("test1.txt") do |file|
  p file.readlines   #<= fileのスキャンが終了するため後ろのfile.getsはnilとなる
  while line = file.gets
    p line
  end
end

__END__

p File.open("test1.txt").read

__END__

while line = STDIN.gets
    break if line.chomp == "exit"
    p line
end

__END__

m = ARGV.first.to_i
n = ARGV.last.to_i
p m
p n

__END__