ruby

zipファイルの作成

rubyzip というライブラリがあるのでそれを使用した。http://x.jeez.jp/archives/date/2008/01/14 を参考にしました。 まずは rubyzip をインストール wget http://downloads.sourceforge.net/rubyzip/rubyzip-0.9.1.tgz tar zxvf rubyzip-0.9.1.tgz cd ruby…

SpecialGeneration のインストール

config/environment.rb 一番先頭に文字コード指定 $KCODE = 'u' プラグインのインストール cd vendor/plugins svn co http://wota.jp/svn/rails/plugins/branches/stable/special/ テストテーブル作成 create table members(id serial, name varchar(64), da…

mongrel のインストール

※作成中fedora coare7で g:rubyist:id:muscovyduck:20070402 を参考にしました yum install ruby-devel gem install mongrel # 2. mongrel 1.0.4 (ruby) を選択 gem install mongrel_cluster上記参考エントリの www-data を www に変えました bbs も hello …

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…

ruby学習メモ 練習問題

CodeZine「脱 超初心者 Javaアルゴリズム問題集 第2回」を ruby でやってみる http://codezine.jp/careerup/article/aid/1495.aspx 問題1 標準入力された月、日が、今日であるかどうかを判定するプログラムを作成してください。 コマンドラインに「今日は何…

ruby学習メモ 変数

num = 81 => 81 6.times do puts "#{num.class}: #{num}" num *= num end Fixnum: 81 Fixnum: 6561 Fixnum: 43046721 Bignum: 1853020188851841 Bignum: 3433683820292512484657849089281 Bignum: 11790184577738583171520872861412518665678211592275841109…

ruby学習メモ 練習問題

CodeZine「脱 超初心者 Javaアルゴリズム問題集 第1回」を ruby でやってみる http://codezine.jp/careerup/article/aid/1426.aspx 問題1 標準入力された値があればそのまま表示、値がなければエラーメッセージを表示するプログラムを作成してください。 な…

ruby学習メモ

クロージャブロック class TestClass def name @name end def initialize(label, &action) @name = label @action = action end def call_action puts "start call_action" @action.call(self) end end テスト1 test1 = TestClass.new("test") { name } tes…

ruby学習メモ

イテレータ each irb(main):006:0> [ 1,2,3,4,5].each {|i| puts i} 1 2 3 4 5 => [1, 2, 3, 4, 5] collect 配列を作成してくれる irb(main):008:0* [ 1,2,3,4,5].collect {|i| puts i ; i + 1 } 1 2 3 4 5 => [2, 3, 4, 5, 6] ファイル内容出力 f = File.op…

ruby学習メモ

オブジェクトの凍結(#freeze) test1 = "string" test2 = test1 test1.freeze # 凍結 test2[0] = "A" test1が参照する文字列は凍結されているので TypeError が発生する TypeError: can't modify frozen string from (irb):33:in `[]=' from (irb):33 from :0…

ruby学習メモ

ブロックを利用したファイルのオープン、読み込み、クローズ # ファイルをオープンしてクローズする class File def File.open_and_process(*args) f = File.open(*args) yield f f.close end end File.open_and_process("testfile.txt", "r") do |file| # f…

ruby学習メモ アクセス制御

class TestClass def defaultMethod # public return "default -> public" end protected def protectedMethod # protected return "protected" end private def privateMethod # private return "private" end public def publicMethod # public return "pu…

ruby学習メモ Singleton

コンストラクタの private 化とインスタンスの Singleton 化 class MyClass # new をプライベート化することにより、コンストラクタによるインスタンス化 # が出来ないようにする private_class_method :new #インスタンスを保持するクラス変数 @@singleton …

ruby学習メモ インスタンスメソッドとクラスメソッド

class Example /** インスタンスメソッド */ def instance_method return "instance method" end /** クラスメソッド */ def Example.class_method return "class method" end end irb(main):012:0* Example.class_method => "class method" irb(main):013:0…

メモ

1〜1000までで 1 が出現する回数確認 class Counter def count c = 0; (1..1000).each{|n| c += ( n.to_s.count "1") } return c; end end Counter.new.count

メモ

クラスの生成と #to_s のオーバーライド class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end def to_s "Song: #@name--#@artist (#@duration)" end end song = Song.new("Bicylops", "Fleck", 260)…

メモ

プログラミングRuby 第2版 言語編作者: Dave Thomas,Chad Fowler,Andy Hunt,まつもとゆきひろ,田和勝出版社/メーカー: オーム社発売日: 2006/08/26メディア: 大型本購入: 7人 クリック: 270回この商品を含むブログ (152件) を見る 命名規則 ローカル変数・メ…

railsメモ

RubyGems での ActiveSupport irb -r rubygems -r active_support こっちでもOK ruby script/console 時間と数 1.second #=> 1 2.minutes + 30.seconds #=> 150 4.hours + 32.minutes #=> 16320 10.months + 10.days #=> 26784000 1.week #=> 604800 1.fortn…

rubyメモ

文字列とシンボルの違い "name".class # =>String "name".intern.class # => Symbol :name.object_id == "name".object_id # => false :name.object_id == "name".intern.object_id # => true 配列 [1,2,3] [:num, 10] %w( a b c) # ['a', 'b', 'c']と同じ …

Aptana RadRails のインストールメモ

Aptana RadRails をインストールし、DBから一覧を表示するページを作成 IDEのインストール 以下より Aptana + RadRails をダウンロード http://aptana.com/download_rails_rdt.php プラグインのインストール インストール後起動し、 [ヘルプ]-[ソフトウェア…

ruby rails のインストール

yum install ruby* yum install rdoc yum install irb yum install rubygems gem install rails --include-dependencieshello プロジェクト作成 rails hellohello サーバの起動 ruby script/server起動確認 http://127.0.0.1:3000/