Your SlideShare is downloading. ×
Ruby 同好会宣言
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby 同好会宣言

1,224
views

Published on

RubyDoukoukai Statement …

RubyDoukoukai Statement
Ruby Introduction for PHPer

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,224
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Ruby 同好会宣言 RubyDoukoukai Statement 2010/09/14 (Tue) @yuya_takeyama
  • 2. Rubyと私
  • 3. ・日曜Rubyist ・趣味で1年ほど ・仕事ではコード生成などに ・アプリ構築経験ほぼ無し ・たまにブログでRubyの話 http://blog.yuyat.jp ・GitHubにRubyコード http://github.com/yuya-takeyama
  • 4. Agenda 第一部 Rubyの特徴 第二部 Ruby同好会宣言
  • 5. Rubyの特徴 #1 シンプルな構文
  • 6. 文字列出力
  • 7. PHP echo "Hello, World!"; Ruby print "Hello, World!" puts "Hello, World!"
  • 8. 変数代入
  • 9. PHP $foo = "bar"; Ruby foo = "bar"
  • 10. 定数定義
  • 11. PHP define("FOO", 1); Ruby FOO = 1 「変数は小文字で始める」 「定数(クラス名)は大文字で始める」 というルールが言語仕様として決められている
  • 12. 条件分岐
  • 13. PHP if ($n > 0) { $positive = true; } else { $positive = false; }
  • 14. Ruby positive = if n > 0 true else false end Rubyにおけるifは式なので、 それ自体で返り値を持つことができる。
  • 15. クラス定義 メソッド呼び出し
  • 16. PHP class Foo { public function bar($str) { return "(" . $str . ")"; } } $foo = new Foo; echo $foo->bar("baz");
  • 17. Ruby class Foo def bar(str) "(" + str + ")" end end foo = Foo.new puts foo.bar("baz")
  • 18. Rubyの特徴 #2 純粋 オブジェクト指向 言語
  • 19. 何でも オブジェクト
  • 20. 文字列も
  • 21. 数値も
  • 22. 配列も
  • 23. Object
  • 24. 例 "foo,bar,baz" ↓ "Foo/Bar/Baz"
  • 25. in PHP ...
  • 26. join("/", array_map( function ($s) { return ucfirst($s); }, explode(",","foo,bar,baz") )); => "Foo/Bar/Baz" in PHP5.3.x stringもarrayも オブジェクトじゃない (プリミティブ値) → メソッドを持たない
  • 27. join("/", array_map( create_function( '$s', 'return ucfirst($s);' ), explode(",","foo,bar,baz") )); => "Foo/Bar/Baz" in PHP5 (< 5.3)
  • 28. ???
  • 29. in Ruby ...
  • 30. "foo,bar,baz".split(",").map{|s|s.capitalize}.join("/") => "Foo/Bar/Baz" StringもArrayもオブジェクト → メソッドをつなげられる
  • 31. メ ソ ッドチェイン
  • 32. 流れるようなインターフ ェ イ ス
  • 33. Ruby "foo,bar,baz".split(",").map{|s|s.capitalize}.join("/") => "Foo/Bar/Baz" JavaScript "foo,bar,baz".split(",").map(function (s) { return s.capitalize(); }).join("/"); => "Foo/Bar/Baz" (ただし、capitalize()は架空メソッド)
  • 34. Rubyの特徴 #3 オブジェクト指向 を支える 強力な言語機構
  • 35. セッター あるいは ゲッター
  • 36. in PHP ...
  • 37. class Company { protected $_name, $_foundedYear; public function __construct($name, $foundedYear) { $this->_name = $name; $this->_foundedYear = $foundedYear; } public function setName($name) { $this->_name = $name; } public function getName( ) { return $this->_name; } public function getFoundedYear( ) { return $this->_foundedYear; } } $company = new Company('YY Company', 2000); echo $company->getName( ) . ' is founded in ' . $company->getFoundedYear( ); // => "YY Company is founded in 2000" $company->setName('GayaGaya Company'); echo $company->getName( ) . ' is originally founded in ' . $company->getFoundedYear( ); // => "GayaGaya Company is originally founded in 2000"
  • 38. in Ruby ...
  • 39. class Company attr_accessor :name attr_reader :founded_year def initialize(name, founded_year) @name = name @founded_year = founded_year end end company = Company.new('YY Company', 2000) puts company.name + " is founded in " + comapny.founded_year.to_s # => "YY Company is founded in 2000" company.name = "GayaGaya Company" puts company.name + " is originally founded in " + comapny.founded_year.to_s # => "GayaGaya Company is originally founded in 2000"
  • 40. $this->_name $this->_foundedYear ↓ @name @founded_year
  • 41. public function getFoundedYear() { ~~~ } ↓ attr_reader :founded_year
  • 42. public function getName() { ~~~ } public function setName() { ~~~ } ↓ attr_accesor :name
  • 43. 継承
  • 44. in PHP ...
  • 45. 単一 継承
  • 46. class Dog extends Animal { public function osuwari( ) { $this->shitDown( ); } }
  • 47. 多重 継承?
  • 48. class List implements Iterator, ArrayAccess, Countable { public function rewind() {} /* ~~~ */ }
  • 49. 仕様 のみ 継承
  • 50. 実装は 継承 されない
  • 51. in Ruby ...
  • 52. 単一 継承
  • 53. class Dog < Animal def osuwari shit_down end end
  • 54. 多重 継承
  • 55. class Iroha include Enumerable include Foo include Bar def each "いろはにほへと".each_char do |c| yield c end end end
  • 56. include Enumerable するだけで ...
  • 57. all?, any?, chunk, collect, collect_concat, count, cycle, detect, drop, drop_while, each_cons, each_entry, each_slice, each_with_index, each_with_object, entries, find, find_all, find_index, first, flat_map, grep, group_by, include?, inject, map, max, max_by, member?, min, min_by, minmax, minmax_by, none?, one?, partition, reduce, reject, reverse_each, select, slice_before, sort, sort_by, take, take_while, to_a, zip これだけのメソッドが使用可能に
  • 58. ミックスイン 継承
  • 59. まとめ 1. Rubyの文法はシンプル 2. Rubyでは何でもオブジェクト 3. Rubyではセッターやゲッターのメソッドを書 かなくていい 4. Rubyではミックスインで多重継承できる
  • 60.
  • 61. Ruby同好会で 何を やりたいですか?
  • 62. 【急募】 あなたの わたしの Ruby同好会宣言
  • 63. ご清聴 ありがとう ございました