Download - Web2.0 and Ruby
![Page 1: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/1.jpg)
2008.02.26鈍器本セミナー 第二部
Web 2.0Web 2.0ととRubyRuby
(株)永和システムマネジメント
伊藤 浩一http://www.edit.ne.jp/~koic/
gem install rails gem install rails --yy
![Page 2: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/2.jpg)
2Web 2.0とRuby
• 名前: 伊藤 浩一
• 日記: http://www.edit.ne.jp/~koic/
• Twitter: http://twitter.com/koic/
• メール: [email protected]
• 勤務先: 株式会社 永和システムマネジメント
– プログラマ 兼 現場リーダー
自己紹介
![Page 3: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/3.jpg)
3Web 2.0とRuby
何やっている人が話すの?
![Page 4: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/4.jpg)
4Web 2.0とRuby
仕事風景から~最近の新メニュー~
![Page 5: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/5.jpg)
5Web 2.0とRuby
古今東西
デザパタ古今東西デザパタ古今東西
![Page 6: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/6.jpg)
6Web 2.0とRuby
• 週末の夕会後、
• GoFのデザインパターンのパターン名やjava.langパッケージのクラス名など、
• テーマを決め、
• メンバーが順番に云って行き、
• パス三回でアウトになるサバイバルゲーム
それってなんなの?
![Page 7: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/7.jpg)
7Web 2.0とRuby
なにが狙い?
• 少し離れ気味の知識、伝家の宝刀が錆びないよう素振り
• 奇妙な一体感とノスタルジーによるヒーリング効果
• 楽しく一週間の仕事を終える
![Page 8: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/8.jpg)
8Web 2.0とRuby
思わぬ副作用~周辺プロジェクトの反応~
![Page 9: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/9.jpg)
9Web 2.0とRuby
自ずから巻き込まれる
• 事例1 @nawoto LLWGリーダー
• 事例2 @_dotタン
![Page 10: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/10.jpg)
10Web 2.0とRuby
巻き込まれ力の向上効果
![Page 11: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/11.jpg)
11Web 2.0とRuby
巻き込まれ力~あまのりょーさんの仮説
『”involve” NOT ”evolve”』
あまのりょーさん
![Page 12: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/12.jpg)
12Web 2.0とRuby
一目置かれる人に近づく!
![Page 13: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/13.jpg)
13Web 2.0とRuby
• Web 2.0ビギナーズバイブル (毎日コミュニケーションズ)
• EngineerMind vol.5 (技術評論社)
– 『プログラマのための創造的プロジェクトファシリテーション』
• 開発の現場 vol.11 (翔泳社)
– 『保守プロジェクトとデベロッパーテスティング』
もうひとつの顔~ライター~
928ページ鈍器本
![Page 14: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/14.jpg)
14Web 2.0とRuby
巻き込まれてシメシメやってきました
『”involve” NOT ”evolve”』
あまのりょーさん
![Page 15: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/15.jpg)
15Web 2.0とRuby
今日はよろしくお願いします
![Page 16: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/16.jpg)
16Web 2.0とRuby
• プログラマとしてのお話
– RubyとRuby on Rails
– Ruby on RailsとREST
• 現場リーダーとしてのお話
– Ruby on Railsによるチーム開発
• ライターとしてのお話
–次回作のプレビュー
アジェンダ
![Page 17: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/17.jpg)
17Web 2.0とRuby
プログラマとしてのお話
![Page 18: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/18.jpg)
18Web 2.0とRuby
Chapter 1. WebとRuby
![Page 19: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/19.jpg)
19Web 2.0とRuby
• プログラミングを楽しくする言語
• プログラマのプログラマによるプログラマのための言語
• ツール(言語)は思考を規定する
Ruby
http://www.ruby-lang.org/ja/
![Page 20: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/20.jpg)
20Web 2.0とRuby
Web 2.0 ビギナーズバイブル
Perl, PHP, Python, RubyなどのLightweight LanguageでWebアプリケーション開発を解説した書籍
![Page 21: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/21.jpg)
21Web 2.0とRuby
Lightweight Language
• Lightweight Languageとは「脳力」を少なく消費する• 「脳力」はプログラミング活動中に消費される仮想的なパワーである•消費「脳力」の総和が少ないことももちろん重要だが瞬間最大消費「脳力」が大きすぎるのもよろしくない
![Page 22: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/22.jpg)
22Web 2.0とRuby
発想→変換→表現(プログラム)
人間の脳への負担の少ない性質
say = "I love Ruby"puts say
say['love'] = "*love*"puts say.upcase
5.times { puts say }
変換
表現発想
距離
発想から表現までの変換の距離「脳力」が生産性の指標のひとつ
![Page 23: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/23.jpg)
23Web 2.0とRuby
Ruby/Amazonを使った書誌情報取得例
Web APIを利用したサンプルコード
require 'amazon/search'
if ARGV.size != 2puts "Usage: #{$0} "¥"[Amazon Web Services AccessKey ID] [text to search for]"
exitendaccess_key, search_request = ARGVreq = Amazon::Search::Request.new(access_key)
req.keyword_search(search_request, 'books', Amazon::Search::LIGHT) do |book|
puts %{"#{book.product_name}" by #{book.authors.join(', ')}}end
$ ruby amazon-search.rb アクセスキーアクセスキーアクセスキーアクセスキー "restful web services“"RESTful Web Services" by Leonard Richardson, Sam Ruby, David HeinemeierHansson
amazon-search.rb
実行と結果
『RESTful Webサービス』より抜粋
![Page 24: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/24.jpg)
24Web 2.0とRuby
Ruby on Railsによる波及効果が大きい
言語としてのRuby自体はWebと直接の関係はなく
![Page 25: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/25.jpg)
25Web 2.0とRuby
Ruby on Rails
• Ruby製のWebアプリケーションフレームワーク
• 「美しいコードを書けるからRubyを選んだ」(David Heinemeier Hansson氏)– http://itpro.nikkeibp.co.jp/article/NEWS/20060620/241346/
• 現在のRubyブームの火付け役
http://www.rubyonrails.org/
![Page 26: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/26.jpg)
26Web 2.0とRuby
Ruby on Railsの特徴
• DRY―Don’t Repeat Yourself
(繰り返しを避けること)
• Convention over Configuration(設定より規約)
• full-stack
![Page 27: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/27.jpg)
27Web 2.0とRuby
割愛鈍器本 Chapter 11 をご参照ください
![Page 28: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/28.jpg)
28Web 2.0とRuby
• DSL
• パターン
• REST指向
異なる側面
Patterns
DSL REST
full-stackDRYCoC
![Page 29: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/29.jpg)
29Web 2.0とRuby
• Domain Specific Language (DSL)
– Webとデータベースの領域に特化した内部DSL
• 左から右へ
–目で文章を追う時と同じ動きで記述可能
• BDDフレームワーク『RSpec』に顕著
• 問題領域の語彙を使い、少ない記述でコードの意図を表すことができる
DSL
class Profile < ActiveRecord::Basebelongs_to :accountvalidates_presence_of :name
end
![Page 30: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/30.jpg)
30Web 2.0とRuby
• 内部DSLによりすべてRubyの構文で記述可能
– RubyのAPIとRailsのAPIに違いがない
(補足) 内部DSL
汎用言語
対象ドメイン 外部DSL
外部DSL
汎用言語(Ruby)
内部DSL
内部DSL(Rails)
透過的
例えばXML例えばRuby Railsの場合
Railsの採った道
「脳力」の消費が低い言語で一貫できる
![Page 31: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/31.jpg)
31Web 2.0とRuby
『Patterns of Enterprise Application Architecture』
『Refactoring Databases』『J2EE Patterns』などの
パターンが構成要素に含まれる
Patterns
後に出てくるものが優れていることは世の常
パターンは発明するものではなく発見されるもの
![Page 32: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/32.jpg)
32Web 2.0とRuby
次章に続く
REST指向
![Page 33: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/33.jpg)
33Web 2.0とRuby
Chapter 2. RailsとREST
![Page 34: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/34.jpg)
34Web 2.0とRuby
RESTとROA
REST
ROA
• RESTはWebに依存しない設計条件
• ROA (Resource oriented Architecture)
– RESTアーキテクチャスタイルの実現手段のひとつ
アーキテクチャスタイル
アーキテクチャ
![Page 35: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/35.jpg)
35Web 2.0とRuby
• プログラムを対象としたWeb
– HTTPとMachine ReadableなXMLに基づいている
ROAとプログラマブルWeb
リソース
require ‘open-uri‘require ‘rexml‘
open(“www...
HTML
XML
HTTP
URL
URL
人
マシン(プログラム)
プログラマブルWeb
▲
• 東京の天気予報• 2008年2月26日の
スケジュール• 東京の地図
![Page 36: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/36.jpg)
36Web 2.0とRuby
リソースとURI
リソース(東京の地図)
URI
• リソースとは実体だったり概念だったりする抽象的なもの
• RESTでは、クライアントが操作できるリソースごとにURIを公開
GET /maps/tokyo
メソッド情報 スコープ情報
公開した名前▼
![Page 37: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/37.jpg)
37Web 2.0とRuby
HTTPリクエストとメッセージパッシング
GET /maps/tokyo
Map.find(:first, :conditions => [’area = ?’, ’tokyo’])
名詞 動詞
HTTPリクエスト(抜粋)
メッセージパッシング
![Page 38: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/38.jpg)
38Web 2.0とRuby
リソース指向とオブジェクト指向
GET /maps/tokyo
Map.find(:first, :conditions => [’area = ?’, ’tokyo’])
GETPOSTPUTDELETE…
<<instance>>東京の天気予報
リソースクライアント
<<instance>>2008年2月26日
のスケジュール
<<instance>>東京の地図
URI
名前を持つ
メソッド名が標準化されている
![Page 39: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/39.jpg)
39Web 2.0とRuby
• Webサービスを対象にActiveRecordのようなCRUDを備える
– Rails 2.0で正式に追加された
• REST指向フレームワークという特色をより強く打ち出す
– ActiveResource::Baseクラスを継承して、オブジェクトを公開するサイトを指定することでWebのCRUDに対応
Active Resource
class Map < ActiveResource::Baseself.site = ’http://localhost:3000’
end
![Page 40: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/40.jpg)
40Web 2.0とRuby
• Lightweight LanguageによるWebアプリケーションフレームワークの台頭によりWebプログラミングの敷居が低くなりました
– Railsへの最初の一歩は以下のコマンドです
Chapter 1 & 2 まとめ
$ gem install rails -y
Rubyのインストールと、『鈍器本』の購入をお忘れなく
![Page 41: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/41.jpg)
41Web 2.0とRuby
Chapter 3. Ruby on Railsによるチーム開発
~現場リーダーから見たRails~
![Page 42: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/42.jpg)
42Web 2.0とRuby
• メンバーの立ち上がりが速い
• 比較的簡単な業務知識
• Model/Controllerのプロダクトコード実装量が少ない
• その分Viewやテストを厚く
– デスクトップライクなUIの作成は、JavaScriptで
• 自動化されたテストにするのが大変 ><
– そもそもUIのテストは自動化できるものと、できないものがある
• 限られた開発期間の中で、自動化をするための工数(投資)が見合わないことが多い
Railsを使ったお仕事の経験と風聞
![Page 43: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/43.jpg)
43Web 2.0とRuby
• 道具が変わっても人が変わるわけではない–新しい技術を持ってきても、基礎となる開発の呼吸
は変わらない
– プラットフォーム独特の設計技法やコンピュータへの指示方法が変わる
•新しい技術への対応速度
• 継続的なプランニング、ホワイトボードディスカッション、報告・連絡・相談など、足元が重要
– チームのまわし方への配慮
新しいアイテムを手に入れた?
![Page 44: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/44.jpg)
44Web 2.0とRuby
プロジェクトを取り巻くツール
![Page 45: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/45.jpg)
45Web 2.0とRuby
人とツール
人はあらゆるツールよりも重要である。優れた人々に優れたツールを与えれば、ツールを持たない優れた人々よりも効率が上がる。特にそれを使うときが楽しいものであれば。
![Page 46: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/46.jpg)
46Web 2.0とRuby
バージョン管理
テスティング
自動化SVN
RSpec on Railstest/unit
Capistrano
CC.rb
プロジェクトでの基盤技術とツール
• バージョン管理
• テスティング
– ユニットテスト
– 受入れテスト
• 自動化
– CI
– マイグレーション
– デプロイ
![Page 47: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/47.jpg)
47Web 2.0とRuby
運用環境<production>
ローカル環境<test, development>
リポジトリ(V)
デプロイ(A)※リポジトリから運用環境へ
ソースをチェックアウト
マイグレーション(A)
継続的インテグレーションサーバ<test>
チェックアウト(V)
マイグレーション、テスト(A)
ユニットテスト(T)
コミット(V)更新、
チェックアウト(V)
受入れテスト(T)
作業
(V)バージョン管理(T)テスティング(A)自動化
3つの基盤技術と3つの環境3つの基盤技術
test, development, production
![Page 48: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/48.jpg)
48Web 2.0とRuby
継続的インテグレーション
http://cruisecontrolrb.thoughtworks.com/
リポジトリ(V)
結果レポート、リビルド要求
CIサーバ
エラー通知メール
チェックアウト、ビルド
![Page 49: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/49.jpg)
49Web 2.0とRuby
• 動作するプログラムへの期待を担保するには、プログラムを動かすこと
–静的チェックは構文チェックまでで動かない
• 他のメンバーと同じリソースを使ったテスト
–自分のコミットで自動ビルドが通らなくなった?
–自分の変更で他の人の環境が動かなくなった?
• 変更の差分が小さいうちに早目に検知
–差分が大きいほど、どこで壊したか調査が大変
静的コンパイルのないRubyで自動ビルド?
プログラムが動くまでがビルド、静的チェックは通過点に過ぎない
![Page 50: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/50.jpg)
50Web 2.0とRuby
• わりと後付け
–開発促進よりセーフティーネットの意味合いが強い
• まず受入れテスト基盤を作るコストを考えること
–自動化への二つの道
最近の受入れテスト
![Page 51: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/51.jpg)
51Web 2.0とRuby
• 最初は手作業(ドキュメントによるオペレーション)
• 合間合間にスクラッチで自動化
– irb時代
• net/httpでWebサービスにリクエストを送る
• 結果を目視
– Ver 0.1
• Irbで書いたコードを元におれおれテスティングフレームワーク
• net/httpでWebサービスにリクエストを送る
• WebサービスのレスポンスXMLをrexmlでパースして文字列比較を行った結果、失敗したら例外を投げる
– Ver 0.2
• test/unitを導入
• net/httpでWebサービスにリクエストを送る
• WebサービスのレスポンスXMLをrexmlでパースして文字列比較のアサーションを行う
Webサービスの受入れテスト ステップ例
階段の開始と幅はそのときの力量に応じます。人によりもっとカッコいいステップがあると思います。
![Page 52: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/52.jpg)
52Web 2.0とRuby
• 道具が変わっても、 (Webアプリケーション開発というドメインにおいて) プロジェクトのまわし方は大きくは変わらない
–泥臭いところは通じていたりする
Chapter 3 まとめ
No, Silver Bullet!
![Page 53: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/53.jpg)
53Web 2.0とRuby
Chapter 4. 次回作のプレビュー~ライターとしてのお話~
![Page 54: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/54.jpg)
54Web 2.0とRuby
• Javaプログラマ向けRubyプログラミング書籍『タイトル未定』
• 鈍器本と同じく、毎日コミュニケーションズから
• 共著者に小野良 哲郎さん
–弊社コンサルティングセンター所属
• 2008年8月発売予定
次回作予告
★本章で取り扱う内容は書籍でボツになっている可能性があります
![Page 55: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/55.jpg)
55Web 2.0とRuby
• Javaプログラマ向けRubyプログラミングガイド
• JRuby
• Ruby 1.9
–出版時期が微妙なため扱い方を検討中
三つの柱
![Page 56: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/56.jpg)
56Web 2.0とRuby
アクセサ(メソッド)で宣言するのが慣例
属性へのアクセサ
class Productattr_reader :namedef initialize(name)@name = name
endend
public class Product {private String name;// スペーススペーススペーススペースのののの都合上都合上都合上都合上、、、、コンストラクタコンストラクタコンストラクタコンストラクタはははは省略省略省略省略public String getName() {return name;
}}
Ruby
Java
getter, setter と アクセサ(メソッド)の対応•getter … attr_reader•setter … attr_writer•getter, setter … attr_accessor
name属性へのアクセサ宣言
![Page 57: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/57.jpg)
57Web 2.0とRuby
既存のクラスを拡張可能
オープンクラス
module Specmodule Exceptionsclass ExpectationNotMetError < StandardErrorend
endmodule Expectationsdef should_equal(expected)raise Spec::Exceptions::ExpectationNotMetError ¥if self != expected
endend
end
class Objectinclude Spec::Expectations
end
既存のObjectクラスを拡張
Ruby
p = Product.new(’Ruby’)p.name.should_equal(’Ruby’)p.name.should_equal(’Perl’)
Ruby利用例
![Page 58: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/58.jpg)
58Web 2.0とRuby
すべてのクラス(のインスタンス)から参照可能
すべてのクラス(のインスタンス)から参照可能
public
同一のクラス(・パッケージ)もしくはサブクラス(のインスタンス)から参照可能
同一のクラスもしくはサブクラス(のインスタンス)から参照可能
protected
同一のクラス(のインスタンス)から参照可能(インスタンスが異なっても同一クラスならば参照可能)
同一のインスタンス(自分自身)からのみ参照可能。
レシーバ(self)の指定不可。
(同じインスタンスからも参照可能)
private
JavaJavaJavaJavaRubyRubyRubyRubyアクセスアクセスアクセスアクセス制御制御制御制御
• Javaはクラス単位、Rubyはオブジェクト単位と可視性が異なる
• Rubyにパッケージプライベートはない
パッケージの可視性
![Page 59: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/59.jpg)
59Web 2.0とRuby
• ==メソッドとequalメソッド
– Javaと逆だよ
• オブジェクトの同値性比較は==メソッド (適宜オーバーライド)
• オブジェクトの同一性比較はequalメソッド (オーバーライドするな!)
• Enumerable#sortとEnumarable#sort_by
– sort_byメソッドの方が高速
– まずsort_byで考える
その他、Ruby Tips
![Page 60: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/60.jpg)
60Web 2.0とRuby
• Ruby 1.8との後方互換性はない
– 1.8と1.9は別物
• 大きな変更
– Ruby VMの採用
– M17N対応
– ブロック変数のスコープを変更
–組み込みクラスとそのメソッドの追加、廃止
Ruby 1.9
などなど
Ruby 1.9ネタは、Appendixかなあ。
![Page 61: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/61.jpg)
61Web 2.0とRuby
• Javaで実装されたRubyの処理系
• 現在のJRubyは、Ruby 1.8に対応
• JavaのライブラリをRubyのシンタックスで利用可能
– 処理系がJavaなので当然といえば当然ですが
• アーキテクチャの可能性
– Javaによる既存のサブシステムを背景にフロントをRailsで作る
– 2相コミットや非同期メッセージ処理などはJavaで
• 素直にJavaでやればいい気もしますが選択肢の幅として
– Enterprise Ruby ><
• サンプルは検討中
JRuby
![Page 62: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/62.jpg)
62Web 2.0とRuby
Javaプログラマ向けのRubyプログラミングガイド
Chapter 4 まとめ
2008年8月発売予定
![Page 63: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/63.jpg)
63Web 2.0とRuby
•少ない「脳力」でWebプログラミングができることで、プログラマブルWebの敷居が低くなりました
–機会があれば、Webの可能性をお楽しみください
•道具が変わっても人は変わらない
–如何により楽しい環境にしていくかが大事
おわりに
![Page 64: Web2.0 and Ruby](https://reader034.vdocument.in/reader034/viewer/2022042715/559222b61a28abb6068b464e/html5/thumbnails/64.jpg)
64Web 2.0とRuby
4q!
ご清聴ありがとうございました