Seven Languages in Seven Days: Ruby

783 views
659 views

Published on

Some coworkers and I are going through the 7 languages book, I presented on the Ruby chapter. Source code available here: https://github.com/swicklund/polyglot

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
783
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
9
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Seven Languages in Seven Days: Ruby

  1. 1. Seven Languages in Seven Days: Ruby prepared by @zachleat
  2. 2. CreatorYukihiro Matsumoto“Matz”
  3. 3. InfluencesLisp, Smalltalk, Perl
  4. 4. Trade-offs Simplicity for SafetyProductivity for Performance
  5. 5. irbRuby’s Interactive Console
  6. 6. Syntax
  7. 7. WHEN U DECLARE VARSU DECLARE WEAKNESS Courage Wolf
  8. 8. RUBY CODE ROIALWAYS RETURN SOMETHING Business Cat
  9. 9. Y U NO EVALUTE STRING? SINGLE QUOTES!! puts “hello, #{language}” Y U NO?
  10. 10. >> puts ‘literal string’literal string>> subject = ‘world’=> “world”>> puts “hello, #{subject}”hello, world Y U NO?
  11. 11. HUMANS ARE CARBON RUBY IS OBJECTS Philosoraptor
  12. 12. >> 4.class=> FixNum>> 4.methods=> [“inspect”, “%”, “<<“, ...>> false.class=> FalseClass Philosoraptor
  13. 13. MY CONDITIONALSALWAYS SUCCESSFUL Success Kid
  14. 14. >> x = 4=> 4>> puts ‘True!!’ if x == 4True!!=> nil>> puts ‘True!!’ unless x == 4=> nil>> puts ‘True!!’ if not true=> nil>> puts ‘True!!’ if !true=> nil Success Kid
  15. 15. # Everything but nil and false# evaluate to true. 0 is true!>> puts “This is true” if 0This is true=> nil# and, &&# or, ||# &, | are the non-short circuit# equivalents Success Kid
  16. 16. THE BEST LOOPSITERATE OVER THIRST The Most Interesting Man in the World
  17. 17. >> x = x + 1 while x < 10=> nil>> x=> 10>> x = x - 1 until x == 1=> nil>> x=> 1 The Most Interesting Man in the World
  18. 18. KEYBOARD CATHAS NOTHING ON DUCK TYPINGTechnologically Impaired Duck
  19. 19. >> 4 + ‘four’TypeError: String can’t be coercedinto Fixnum# Strongly typed# Dynamic: Checked at run time>> a = [‘100’, 100.0]=> [‘100’, 100.0]>> while i < 2>> puts a[i].to_i>> i += 1>> end100100 Technologically Impaired Duck
  20. 20. FFFFFUUUUUUUUUUUUUUUUUNCTIONS FFFUUUUUU
  21. 21. >> def tell_the_truth>> true>> end# Last expression is return value Technologically Impaired Duck
  22. 22. ARRRRRRRAYSAND HASHES
  23. 23. >> animals = [‘lions’, ‘tigers’]=> [‘lions’, ‘tigers’]>> numbers = {1 => ‘one’, 2 =>‘two’}=> {1=>”one”, 2=>”two”}# Symbols>> ‘string’.object_id=> 3092010>> ‘string’.object_id=> 3089690>> :string.object_id=> 69618>> :string.object_id=> 69618
  24. 24. >> def winning(options = {})>> if(options[:profession] == :gambler)>> true>> else>> false>> end>> end=> nil>> winning=> false# {} optional for last parameter>> winning(:profession => :lawyer)=> true
  25. 25. YO DAWG I HEARD YOU LIKED CODE BLOCKSSO YOU COULD RUN CODE IN YOUR CODE
  26. 26. >> 3.times { puts ‘hi’ }hihihi>> animals = [‘lions’, ‘tigers’]>> animals.each {|a| puts a}lionstigers# Blocks can be passed asparameters>> def pass_block(&block)>> end>> pass_block { puts ‘hi’ }
  27. 27. Classesclass MyClass def initialize(name) @name = name # instance var @@other = ‘’ # class var end def name return @name end # methods that check end in ?endmy_class = MyClass.new(‘Name’)my_class.name # returns ‘Name’
  28. 28. Modulesmodule MyModule def name return @name endendclass MyClass include MyModule def initialize(name) @name = name endendmy_class = MyClass.new(‘Name’)my_class.name # returns ‘Name’
  29. 29. Enumerable# Implements each methodComparable# Implements <=> (spaceship) method
  30. 30. Open Classes# First invocation defines# Second invocation modifiesclass NilClass def blank? true endendclass String def blank? self.size == 0 endend[‘’, ‘person’, nil].each {|a| puts a unless a.blank?}# outputs person
  31. 31. method_missing

×