Your SlideShare is downloading. ×
  • Like
Ruby 程式語言簡介
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Ruby 程式語言簡介

  • 4,480 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
4,480
On SlideShare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
236
Comments
1
Likes
27

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 20 ihower@handlino.com 2009/4/17 OSDC.TW
  • 2. ? • a.k.a. ihower • http://ihower.idv.tw/blog/ • twitter: ihower • 2006 Ruby • ( ) Rails Developer • http://handlino.com • http://registrano.com
  • 3. Ruby? • • • • Yukihiro Matsumoto, a.k.a. Matz • Lisp, Perl, Smalltalk • Happy
  • 4. # quot;UPPERquot; puts quot;upperquot;.upcase # -5 puts -5.abs # quot;Ruby Rocks!quot; 5.times do puts quot;Ruby Rocks!quot; end
  • 5. Methods def end def say_hello(name) result = quot;Hi, quot; + name return result end puts say_hello('ihower') # Hi, ihower
  • 6. Methods def end def say_hello(name) result = quot;Hi, quot; + name return result end puts say_hello('ihower') # Hi, ihower
  • 7. Class class Greeter def initialize(name) @name = name end def say(word) puts quot;#{phrase}, #{@name}quot; end end g1 = Greeter.new(quot;ihowerquot;) g2 = Greeter.new(quot;ihoverquot;) g1.say(quot;Helloquot;) # Hello, ihower g2.say(quot;Helloquot;) # Hello, ihover
  • 8. Class class Greeter def initialize(name) @name = name end def say(word) puts quot;#{phrase}, #{@name}quot; end end g1 = Greeter.new(quot;ihowerquot;) g2 = Greeter.new(quot;ihoverquot;) g1.say(quot;Helloquot;) # Hello, ihower g2.say(quot;Helloquot;) # Hello, ihover
  • 9. Class class Greeter def initialize(name) @name = name end def say(word) puts quot;#{phrase}, #{@name}quot; end end g1 = Greeter.new(quot;ihowerquot;) g2 = Greeter.new(quot;ihoverquot;) g1.say(quot;Helloquot;) # Hello, ihower g2.say(quot;Helloquot;) # Hello, ihover
  • 10. Class class Greeter def initialize(name) @name = name end def say(word) puts quot;#{phrase}, #{@name}quot; end end g1 = Greeter.new(quot;ihowerquot;) g2 = Greeter.new(quot;ihoverquot;) g1.say(quot;Helloquot;) # Hello, ihower g2.say(quot;Helloquot;) # Hello, ihover
  • 11. Class ( ) class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
  • 12. Class ( ) class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
  • 13. Class ( ) class Greeter @@name = “ihower” def self.say puts @@name end end Greeter.say # Hello, ihower
  • 14. Array a = [ 1, quot;catquot;, 3.14 ] puts a[0] # 1 puts a.size # 3 a[2] = nil puts a.inspect # [1, quot;catquot;, nil]
  • 15. Hash config = { quot;fooquot; => 123, quot;barquot; => 456 } puts config[quot;fooquot;] # 123
  • 16. Symbols config = { :foo => 123, :bar => 456 } puts config[:foo] # 123
  • 17. If if account.total > 100000 puts quot;large accountquot; elsif account.total > 25000 puts quot;medium accountquot; else puts quot;small accountquot; end
  • 18. If if account.total > 100000 Perl Style puts quot;large accountquot; elsif account.total > 25000 puts quot;medium accountquot; else puts quot;small accountquot; end
  • 19. Case case name when quot;Johnquot; puts quot;Howdy John!quot; when quot;Ryanquot; puts quot;Whatz up Ryan!quot; else puts quot;Hi #{name}!quot; end
  • 20. false nil puts quot;not executequot; if nil puts quot;not executequot; if false puts quot;executequot; if true # execute puts quot;executequot; if “” # execute ( JavaScript ) puts quot;executequot; if 0# execute ( C ) puts quot;executequot; if 1# execute puts quot;executequot; if quot;fooquot; # execute puts quot;executequot; if Array.new # execute
  • 21. Regular Expressions Perl # phone = quot;123-456-7890quot; if phone =~ /(d{3})-(d{3})-(d{4})/ ext = $1 city = $2 num = $3 end
  • 22. code block closure { puts quot;Helloquot; } # block do puts quot;Blahquot; # block puts quot;Blahquot; end
  • 23. code block (iterator) # people = [quot;Davidquot;, quot;Johnquot;, quot;Maryquot;] people.each do |person| puts person end # 5.times { puts quot;Ruby rocks!quot; } # 1.upto(9) { |x| puts x }
  • 24. code block (iterator) # people = [quot;Davidquot;, quot;Johnquot;, quot;Maryquot;] people.each do |person| puts person end # 5.times { puts quot;Ruby rocks!quot; } # 1.upto(9) { |x| puts x } while, until, for
  • 25. code block # a = [ quot;aquot;, quot;bquot;, quot;cquot;, quot;dquot; ] b = a.map {|x| x + quot;!quot; } # [quot;a!quot;, quot;b!quot;, quot;c!quot;, quot;d!quot;] # [1,2,3].find_all{ |x| x % 2 == 0 } # [2]
  • 26. code block # a = [ quot;aquot;, quot;bquot;, quot;cquot; ] a.delete_if {|x| x >= quot;bquot; } # [quot;aquot;] # [2,1,3].sort! { |a, b| b <=> a } # [quot;3quot;,”2”,”1”]
  • 27. code block functional programming fu? # (5..10).inject {|sum, n| sum + n } # find the longest word longest = [quot;catquot;, quot;sheepquot;, quot;bearquot; }.inject do |memo, word| ( memo.length > word.length )? memo : word end
  • 28. code block file = File.new(quot;testfilequot;, quot;rquot;) # ... file.close File.open(quot;testfilequot;, quot;rquot;) do |file| # ... end #
  • 29. Yield yield code block # def call_block puts quot;Startquot; yield yield puts quot;Endquot; end call_block { puts quot;Blocks are cool!quot; } # # quot;Startquot; # quot;Blocks are cool!quot; # quot;Blocks are cool!quot; # quot;Endquot;
  • 30. code block def call_block yield(1) yield(2) yield(3) end call_block { |i| puts quot;#{i}: Blocks are cool!quot; } # # quot;1: Blocks are cool!quot; # quot;2: Blocks are cool!quot; # quot;3: Blocks are cool!quot;
  • 31. Proc object code block def call_block(&block) block.call(1) block.call(2) block.call(3) end call_block { |i| puts quot;#{i}: Blocks are cool!quot; } # proc object proc_1 = Proc.new { |i| puts quot;#{i}: Blocks are cool!quot; } proc_2 = lambda { |i| puts quot;#{i}: Blocks are cool!quot; } call_block(&proc_1) call_block(&proc_2) # # quot;1: Blocks are cool!quot; # quot;2: Blocks are cool!quot; # quot;3: Blocks are cool!quot;
  • 32. Module Mixins module Debug def who_am_i? quot;#{self.class.name} (##{self.object_id}): #{self.to_s}quot; end end class Foo include Debug # Mixin # ... end class Bar include Debug include AwesomeModule # ... end ph = Foo.new(quot;12312312quot;) et = Bar.new(quot;78678678quot;) ph.who_am_i? # quot;Foo (#330450): 12312312quot; et.who_am_i? # quot;Bar (#330420): 78678678quot;
  • 33. Module Mixins module Debug def who_am_i? quot;#{self.class.name} (##{self.object_id}): #{self.to_s}quot; end end class Foo include Debug # Mixin # ... end class Bar include Debug include AwesomeModule Ruby # ... Module end ph = Foo.new(quot;12312312quot;) et = Bar.new(quot;78678678quot;) ph.who_am_i? # quot;Foo (#330450): 12312312quot; et.who_am_i? # quot;Bar (#330420): 78678678quot;
  • 34. (duck typing) # class Duck def quack puts quot;quack!quot; end end # ( ) class Mallard def quack puts quot;qwuaacck!! quak!quot; end end
  • 35. Class Type birds = [Duck.new, Mallard.new, Object.new] # birds.each do |duck| duck.quack if duck.respond_to? :quack end
  • 36. Class Type birds = [Duck.new, Mallard.new, Object.new] # birds.each do |duck| duck.quack if duck.respond_to? :quack end OOP !
  • 37. Metaprogramming
  • 38. define_method class Dragon define_method(:foo) { puts quot;barquot; } ['a','b','c','d','e','f'].each do |x| define_method(x) { puts x } end end dragon = Dragon.new dragon.foo # quot;barquot; dragon.a # quot;aquot; dragon.f # quot;fquot;
  • 39. Domain-Specific Language class Firm < ActiveRecord::Base has_many :clients has_one :account belongs_to :conglomorate end # has_many AciveRecord class method Firm instance methods firm = Firm.find(1) firm.clients firm.clients.size firm.clients.build firm.clients.destroy_all
  • 40. Method Missing class Proxy def initialize(object) @object = object end def method_missing(symbol, *args) @object.send(symbol, *args) end end object = [quot;aquot;, quot;bquot;, quot;cquot;] proxy = Proxy.new(object) puts proxy.first # Proxy first method_missing quot;aquot;
  • 41. Introspection ( ) # Object.methods => [quot;sendquot;, quot;namequot;, quot;class_evalquot;, quot;object_idquot;, quot;newquot;, quot;singleton_methodsquot;, ...] # Object.respond_to? :name => true
  • 42. Ruby on Rails 5 ihower@gmail.com 2009/4/17 OSDC.TW
  • 43. Rails? • Web database-backed • Model-View-Control • ( ) Ruby Ajax ORM (object-relational-mapping) • 2004 David Heinemeier Hanson(DHH) 37signals
  • 44. • Ruby • • Don’t Repeat Yourself • • Convention Over Configuration •
  • 45. Rails • 2005 DHH Hacker • 2006 Rails Jolt • 2005~2006 Ruby/Rails 1552% • Ruby Tiobe 26 10
  • 46. Rails ? Java(Spring/Hibernate) Rails 4 20 4 ( 5 ) 3293 1164 1161 113 / 62/549 55/126 Justin Gehtland Java Rails
  • 47. Rails ? • Justin Gehtland Java :Rails = 3.5 : 1 • Proc.net PHP : Rails = 10 : 1 • JavaEye JAVA : Rails = 10 : 1 • thegiive PHP : Rails = 8 : 1
  • 48. Rails clone
  • 49. Why Ruby? “ 20% Java 300% ” From Beyond Java
  • 50. Rails M V C
  • 51. DB schema Ruby class CreatePeople < ActiveRecord::Migration def self.up create_table :people do |t| t.string :name t.integer :age t.date :birthday t.text :bio t.timestamps end end def self.down drop_table :people end end
  • 52. Active Record ORM class Person < ActiveRecord::Base # end person = Person.new person.name quot;ihowerquot; person.age = 18 person.save person = Person.find(1) puts person.name # ihower
  • 53. Action Controller HTTP request class PeopleController < ApplicationController # GET /people def index @people = Person.all end end
  • 54. Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end end
  • 55. Action Controller HTTP request class PeopleController < ApplicationController method action # GET /people def index @people = Person.all end instance variable View end
  • 56. Action View Ruby HTML <html> <body> <h1>Guestbook</h1> <% @people.each do |person| %> <p><%= person.name %>: <%= person.bio %></p> <% end %> </body> </html>
  • 57. MVC Browser Controller Action Model-View-Control HTTP request route.rb GET /users/1 UsersController Model def show @user = User.find(params[:id]) Database respond_to do |format| format.html format.xml end View #show.html.erb end <html> def index <h1>User Profile</h1> ...... <p><%= @user.nickname %></p> end </html> end
  • 58. Thank you. Programming Ruby (The Pragmatic Programmers) Ruby (O’Reilly) Get to the Point! (http://johnwlong.com/slides/gettothepoint/) Ruby on Rails slide by thegiive in COSCUP