require "cgi" class Article #参照メソッド attr_reader :id, :title, :name, :comment, :ip $; = $, = "\t" @@id = [] def initialize( line ) id, title, name, comment, date, ip = line.chomp.split() @id = id.to_i raise ArticleError, "読み込み時に記事IDの不正がありました。" if @@id.include?( @id ) @@id.push( @id ) @title = title.to_s @name = name.to_s @comment = comment.to_s @date = date.to_i @ip = ip.to_s end def Article.post( title, name, comment, ip ) #記事IDの取得 id = @@id.first.to_i + 1 #タイトルをチェック title = title.to_s title.gsub!( $;, " " ) title.gsub!( /[\r\n]/, "" ) title = CGI.escapeHTML( title.strip ) title = "無題" if title == "" #名前をチェック name = name.to_s name.gsub!( $;, " " ) name.gsub!( /[\r\n]/, "" ) name = CGI.escapeHTML( name.chomp.strip ) name = "名無し" if name == "" #コメントをチェック comment = comment.to_s comment.gsub!( $;, " " ) comment.gsub!( /[\r\n]+$/, "" ) comment = CGI.escapeHTML( comment.strip ) comment.gsub!( /\r\n/, "
" ) comment.gsub!( /[\r\n]/, "
" ) raise ArticleError, "コメントがありません。" if comment == "" #日時の取得 date = Time.now.to_i #記事インスタンスの作成 Article.new( [ id.to_s, title, name, comment, date.to_s, ip ].join() ) end def to_s [ @id, @title, @name, @comment, @date, @ip ].join() end def date Time.at( @date ).strftime("%y/%m/%d(%a) %H:%M:%S") end end class ArticleError < StandardError end