1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyで単純なtailを書いてみた

Posted at

Qiita初投稿〜:v:

Rubyの勉強の為に、単純なtailをRubyで書いてみた。

プログラム

tail.rb
def tail(lineNo, fileName)
    File.open(fileName) do |file|
        lines = file.readlines
        lines.drop(lines.size - lineNo).each do |line|
            puts line
        end
    end
end

tail(ARGV[0].to_i, ARGV[1])

File.readlinesでファイルの内容を全行読み込んで、
Enumerable.dropを使って最終行から指定した行までの配列を作ってる。

実行結果

$ ruby tail.rb 5 ChangeLog
indent-tabs-mode: t
tab-width: 8
change-log-indent-text: 2
end:
vim: tabstop=8 shiftwidth=2

環境情報

Ruby

$ ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [x86_64-darwin17.4.0]

Mac OS X

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.13.3
BuildVersion:   17D102
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?