2007-09-01から1ヶ月間の記事一覧

メールアドレスをリンク形式に置換する正規表現(簡易版)

※正式なメールアドレスの正規表現こちらを参照 http://www.din.or.jp/~ohzaki/perl.htm#Mailメールアドレス置換プログラム # ex70.pl undef $/; # テキストファイルを一気に読み込みます $text = <>; # 引数のファイルを読み込みます $text =~ s{ \b # アド…

ruby学習メモ

irb で使用する文字コードを指定するShift_JIS を指定したい場合 $KCODE='SJIS' その他 "EUC" "SJIS" "UTF8" "NONE" が指定可能、デフォルトは"NONE" "NONE"はマルチバイト文字列を認識なし。d:id:gaba:20060702:p6 http://www.ruby-lang.org/ja/man/?cmd=vi…

ruby学習メモ

モジュールの優先順位 # モジュール1の定義 module Sample1 def test puts "Sample1!!" end end # モジュール2の定義 module Sample2 def test puts "Sample2!!" end end class Clazz1 include Sample1 include Sample2 end Clazz1.new.test => Sample2!! # …

ruby学習メモ

while file = File.open("Ex084.txt") while line = file.gets print if line =~ /start/ .. line=~ /end/ end Ex084.txt の内容 1 start 3 4 end 6出力結果 start 3 4 end $. で読み込んだ行を表示することが出来る puts $. print "行数:#{$.}" begin/end …

ruby学習メモ

条件式 nil でも 定数 false でもないものは全て真(true)を意味する 演算子 意味 == 等価性をテストする === case 文の when で使用 汎用比較演算子。レシーバが引数より小さいとき -1, 両者が等しいとき 0, レシーバが引数より大きいとき +1 を返す , > 大…

ruby学習メモ

コマンド展開 OSによりコマンドとしいて実行される。末尾に改行が付く `echo aaa` => "aaa\n" %x{ echo "aaa" } => "\"aaa\"\n" def duration=(new_duration) puts "new duration = #{new_duration}" @duration = new_duration end end song = Song.new;song…

ruby学習メモ

配列展開メソッド呼び出し アスタリスクをつけることにより、配列を展開してそれぞれを引数とすることが出来る def five(a,b,c,d,e) "#{a} #{b} #{c} #{d} #{e}" end five(1,2,3,4,5) => "1 2 3 4 5" five(1,2,3, *['4','5']) => "1 2 3 4 5" five(*(1..5).t…