LoginSignup
1
1

More than 5 years have passed since last update.

全部のファイルを結合するrubyプログラム

Last updated at Posted at 2017-09-20

rubyサンプルプログラム採集家です

rubyとruby on rails書籍のサンプルプログラム配布サイトの一覧

python書籍のサンプルプログラム配布サイトの一覧

こういうのでサンプルプログラムがディレクトリまたいでるのファイルを結合するプログラムです
拡張子も選択できます
手元もruby1.8だと文字化けするけどほかの環境は試してないです

all.rb
# coding: utf-8

def glob_by_extension(extension)
    Dir.glob("**/*."+extension)
end

def txtadd(file)
    File.read(file)
end

def makealltxt(files)
    @alltxt = ""
    files.each do |file|
        @alltxt << txtadd(file)
    end
    return @alltxt
end

def filewrite(name, txt)
    File.write name, txt
end

def file_write_detail(name, txt)
  file = File.open(name,"w")
  file.puts txt
  file.close
end

def deletemyself(files, selffilename=__FILE__)
    files.delete(selffilename)
    return files
end

files = glob_by_extension("rb")
txt = ""
txt = makealltxt(deletemyself(files))
#txt.encode#("UTF-8", "Shift_JIS")
p filewrite("combining.rb", txt)
1
1
5

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
1