2012/05/20

syntax error, unexpected '\n', expecting tCOLON2

c:\Sites\JobChecker>rake db:migrate rake aborted! c:/Sites/JobChecker/db/migrate/20120515111707_create_execute_units.rb:4: syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.' Tasks: TOP => db:migrate (See full trace by running task with --trace)
こんなエラーが出て、なんだと思ったが、
rails generate model execute_unit name:string, comment:string, ...
みたいについうっかりカンマ区切りしてたせいでmigrationファイルにもコンマが混入してたのが原因。「t.string, :name」みたいになっていた。rails generateコマンドのほうでエラーにしといてくれよ。

2012/05/12

telnetでlsコマンドを投げるRubyスクリプト

# -*- encoding: utf-8 -*-
require 'net/telnet'
def get_file_list(address, username, password, dir_path)
telnet = Net::Telnet.new("Host" => host_address) {|c| print c}
telnet.login(username, password) {|c| print c}
telnet.cmd('cd ' + dir_path) {|c| print c}
telnet.cmd('ls -elR') {|c| print c }
end
# Main
address = '1.x.xxx.xxx'
dir_path = '/hoge/piyo/FUGA/'
username = 'USERNAME'
password = 'PASSWORD'
get_file_list(address, username, password, dir_path)
view raw telnet.rb hosted with ❤ by GitHub

2012/05/03

ディレクトリ内のファイルの情報を取得するRubyスクリプト

# -*- encoding: utf-8 -*-
require 'digest/md5'
require 'find'
# ディレクトリ配下のすべてのファイルにつき、ディレクトリパスとファイル名、
# 最終更新日時とMD5チェックサムをハッシュの配列として返す
def get_files_info(dir_path)
files_info = []
Find.find(dir_path) do |path|
file_info = Hash.new
next if File.directory?(path)
file_info['file_path'], file_info['file_name'] = File::split(path)
file_info['last_modified_time'] = File::stat(path).mtime.to_s
file_info['check_sum'] = Digest::MD5.hexdigest(File.binread(path))
files_info << file_info
end
return files_info
end
# Main
dir_path = 'C:/test'
infos = get_files_info(dir_path)
infos.each do |info|
info.each_value do |v|
puts v
end
end