2008年10月2日木曜日

初めてのRuby (9)

第9章

RubyGems
インストール済みの一覧 gem list
インストール可能な一覧 gem list -r
検索 gem search -r postgres(パターン)
インストール      gem install evil-ruby
更新          gem update

おすすめのGems
rake ruby-debug active-support

ネットワーク
 標準添付ライブラリ HTTP, SMTP, POP3, IMAP, FTP。より低水準の処理をするためのsocketライブラリもある。
require 'open-url'
open("http://www.oreilly.co.jp") {|connection|
connection.each_line do |line|
print line
end
}
データベースに対応するライブラリがgem形式で提供されている。
画像ライブラリ 
GUIライブラリ
Continuationオブジェクト
 require 'continuation' if RUBY_VERSION >='1.9'
1.upto(10) do |i|
if i == 3
$cont = callcc{|continuation|
continuation
}
end
print i, ' '
end
#cont.call(nil) if $cont
#=> 1 2 3 4 ...... 10

continutation の実行例はRubyではあまりない。

2008年10月1日水曜日

初めてのRuby (8)

第8章 オブジェクトとクラス

継承 
 class Duration < Range
end

インスタンスメソッドの定義
 class Duration
def display; puts self end
end
duration = Duration.new
duration.display

クラスメソッドの定義
 class Duration
def Duration.print(x); p x end
end
Duration.print 1 #=> 1
次の定義もある。
 class Duration
def self.print(x); p x end
end
Duration.print

インスタンス化するクラスメソッドnew
 class Duration
def initialize(since, till)
@since = since
@until = till
end
attr_accessor :since, :until
end
duration = Duration.new(Time.now, Time.now + 3600)
p duration.until
p duration.since = Time.new

duration.since += 10
duration.since = (duration.sinc() + 10)

クラス定義の追加
 class String
def caesar; tr 'a-zA-Z', 'n-za-mN-ZA-M' end
end
puts "Learning Ruby".ceasar #=> Yrneavat Ehol

クラスの上書きの禁止 Fixnum.freeze

スーパークラスの呼び出し
 class Duration
def display(targt=>$>)
super # super(target)に同じ
    target.write "(#{self.since}-#{self.until})"
end
end
dusration.display

 クラス変数 クラスと子孫クラスとそれらのすべてのインスタンスの間で共有

 定数 クラスに属するimmutable変数

 class Duration
DAYS_OF_WEEK = 7
p DAYS_OF_WEEK

def print_days_of_week
p DAYS_OF_WEEK
end
end
p Duration::DAYS_OF_WEEK

アクセス制限

 public どこからでも呼び出し可能
 protected クラスまたはサブクラスのインスタンスメソッドからのみ可能
 private selfに対してのみ
  protected privateがJava C# と意味が異なる。

特異メソッド
 Singletonパターン
  システム内のある種の情報を集中管理するクラスなど
  CENTRAL_REPOSITORY = Object.new
def CENTRAL_REPOSITORY.register(target)
@registerd_objects ||= []
unless @registered_objects.include? target
@registerd_objects << target
end
end
def CENTRAL_REPOSITORY.unregister(target)
@registerd_objects ||= []
@registgered_objects.delete(target)
end

クラスメソッドは、Classオブジェクトの特異メソッド
class Duration
def self.a_week_from(from)
return self.new(from, from+7*24*60*60)
end
end
p Duration.a_week_from(Time.now)

特異メソッドでクラスメソッドを定義する流儀もある。

 class Duration
class << self
...

モジュール
 インスタンス化できないクラスのようなもの
Mix-in 制限された多重継承(Mix-inはアイスクリームの上にのせるトッピングに由来)

モジュールのincludeはちょっとした実装を複数のクラスで共有するための仕組み。

Comparableは大小比較機能をトッピングするモジュール
"an" < "and" #=> false

class Foo
include Comparable
def <=>(rhs)
# 何らかの実装
 end
end
foo1 = Foo.new
foo2 = Foo.new
foo1 <= foo2 # Comaparableが提供

includeしたモジュールはクラスのancestorsメソッドで知る
 String.include #=> [String, Comparable, Object, Kernel, BasicObject]

Kernelは組み込み定数や組み込み関数を保持しているモジュール

拡張部分をモジュールとしてまとめてから、include
class Numeric
include ActiveSupport::CoreExtensions::Numeric::Bytes
end

名前空間
class Service; end
module Library
class Service; end
end

最初のServiceはトップレベルの名前空間。後のは、モジュールLibraryに属している。
 ::Service # トップレベルのService
Library::Service # LibraryのService

メソッドの探索
 ・特異メソッド インスタンスメソッド includeしたモジュールのインスタンスメソッド 親クラスのインスタンスメソッド 

Rubyはオーバーロードの機能はない。
内部で引数の数やクラスを調べて処理を振り分けている。