├── .gitignore ├── README.md ├── 3-objects_and_classes.rb ├── 2-expressions_and_operators.rb ├── 5-modules_and_mixins.rb ├── 4-inheritance.rb └── 1-basics.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Ruby 2 | 3 | 1. basics 4 | 2. expressions and operators 5 | 3. objects and classes 6 | 4. inheritance 7 | 5. module and mixins 8 | 6. extend 9 | 7. metaprogramming 10 | 11 | chunky bacon by _why 12 | -------------------------------------------------------------------------------- /3-objects_and_classes.rb: -------------------------------------------------------------------------------- 1 | # ================================ 2 | # Objects 3 | # 4 | # all values are objects, and 5 | # there is no distinction between 6 | # primitives types and object 7 | # types. Every thing is an Object! 8 | # 9 | # ================================ 10 | 11 | 12 | 13 | # ================= 14 | # classes 15 | # ================= 16 | 17 | class Foo 18 | #... 19 | end 20 | 21 | foo = Foo.new 22 | 23 | # ================= 24 | # initialize 25 | # ================= 26 | 27 | class Person 28 | 29 | def intialize(name) 30 | @name = name 31 | end 32 | 33 | end 34 | 35 | person = Person.new("Jill") 36 | person.name # => "Jill" 37 | 38 | # ================= 39 | # getters and setters 40 | # ================= 41 | 42 | class Person 43 | def eyes 44 | @eyes 45 | end 46 | 47 | def eyes=(color) 48 | @eyes = color 49 | end 50 | end 51 | 52 | # OR 53 | 54 | class Person 55 | attr_accessor :eyes 56 | end 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /2-expressions_and_operators.rb: -------------------------------------------------------------------------------- 1 | # ==================================== 2 | # expressions and operators 3 | # ==================================== 4 | # ruby's syntax is expression-oriented. 5 | # control structures such as 'if' that 6 | # would be called statements in other 7 | # languages are actually expressions in 8 | # ruby. 9 | 10 | # ================= 11 | # quick examples of 12 | # operators 13 | # ================= 14 | 15 | 1 + 2 # => 3: addition 16 | 2 * 3 # => 6: multiplication 17 | 1 + 2 == 3 # => true: == tests equality 18 | 2 ** 1024 # => 2 to the power of 1024 19 | "dog" + "pound" # => "dogpound" 20 | "hi " * 3 # => "hi hi hi " 21 | 22 | # ================= 23 | # if 24 | # ================= 25 | 26 | if 2 + 2 == 5 27 | #... 28 | else 29 | #... 30 | end 31 | 32 | # ================= 33 | # unless 34 | # ================= 35 | 36 | unless "5".respond_to?(:each) 37 | #... 38 | else 39 | #... 40 | end 41 | 42 | # more commonly used like this... 43 | # 44 | puts "welcome!" unless ["Jill", "Bob", "Allen"][rand(3)] == "Bob" 45 | # or... 46 | # puts report.path unless report.path.nil? 47 | 48 | # ================= 49 | # case 50 | # ================= 51 | 52 | case rand(20) 53 | when 0..5 54 | #... 55 | when 6..10 56 | #... 57 | else 58 | #... 59 | end 60 | 61 | -------------------------------------------------------------------------------- /5-modules_and_mixins.rb: -------------------------------------------------------------------------------- 1 | # ================================ 2 | # Modules 3 | # 4 | # modlules are much like a classes but 5 | # unlike classes it cannot be instantiated. 6 | # modules are stand-alone libraries of code 7 | # that are ideal for mixins and exteding 8 | # existing objects. 9 | # ================================ 10 | 11 | 12 | # ================= 13 | # module 14 | # ================= 15 | 16 | module Foo 17 | #... 18 | end 19 | 20 | # include/extend 21 | 22 | class Bar 23 | include Foo # if you want Foo as instance methods 24 | extend Foo # if you want Foo as class methods 25 | end 26 | 27 | # ================================ 28 | # common mixin paradigm 29 | # ================================ 30 | 31 | module PaymentSystem 32 | 33 | # this is a ruby hook that gets 34 | # called when a module is included 35 | # in a class. The said class gets 36 | # passed in as an argument. 37 | def included(klass) 38 | klass.extend(ClassMethods) 39 | end 40 | 41 | # instance methods 42 | def pay 43 | # pay person 44 | end 45 | 46 | # class methods 47 | module ClassMethods 48 | def probation 49 | # finds people who are on probation 50 | end 51 | end 52 | 53 | end 54 | 55 | 56 | class Employee 57 | include PaymentSystem 58 | # we now have both the 59 | # instance and class methods 60 | # from PaymentSystem 61 | end -------------------------------------------------------------------------------- /4-inheritance.rb: -------------------------------------------------------------------------------- 1 | # ================================ 2 | # Inheritance 3 | # 4 | # classes can inherit values and 5 | # behavior from another class. this 6 | # is one of the primary strengths 7 | # of object oriented programming. 8 | # ================================ 9 | 10 | # ================================ 11 | # simple inheritance 12 | # ================================ 13 | class Animal # Animals can move 14 | def move 15 | puts "moving..." 16 | end 17 | end 18 | 19 | class Bird < Animal # a Bird is an animal 20 | def fly 21 | puts "flying..." 22 | end 23 | end 24 | 25 | roy = Bird.new # Birds can move and fly 26 | roy.move 27 | roy.fly 28 | 29 | # ================================ 30 | # more inheritance 31 | # ================================ 32 | class Runner 33 | def speed 34 | 4.0 35 | end 36 | end 37 | 38 | # a Sprinter (by our definition) runs 2X faster than a general Runner 39 | class Sprinter < Runner 40 | def speed 41 | super() * 2.0 # super() returns the base (Runner's) speed 42 | end # our Sprinter runs twice that speed 43 | end 44 | 45 | # a Jogger (by our definition) runs at 3/4 of the speed of a Runner 46 | class Jogger < Runner 47 | def speed 48 | super() * 0.75 # super() returns the base (Runner's) speed 49 | end # our Jogger is 0.75 times that speed 50 | end 51 | 52 | steve = Jogger.new 53 | puts "steve, the jogger runs at #{steve.speed} mph" 54 | 55 | mike = Sprinter.new 56 | puts "mike, the sprinter runs at #{mike.speed} mph" 57 | -------------------------------------------------------------------------------- /1-basics.rb: -------------------------------------------------------------------------------- 1 | # ================= 2 | # Intro to Ruby 3 | # ================= 4 | 5 | puts "Hello World!" 6 | 7 | # ================= 8 | # types 9 | # ================= 10 | 11 | # String 12 | "a quick brown fox jumps over the lazy dog" 13 | 14 | # Symbol 15 | :name 16 | 17 | # Fixnum (integer) 18 | 42 19 | 20 | # Float 21 | 3.14 22 | 23 | # TrueClass 24 | true 25 | 26 | # FalseClass 27 | false 28 | 29 | # NilClass (yes, even nil is a class!) 30 | nil 31 | 32 | # ================= 33 | # looks like a duck, 34 | # quacks like a duck 35 | # ================= 36 | 37 | x = "a string" # x.class => String 38 | 39 | x = 1 # x.class => Fixnum 40 | 41 | x = 2.3 # x.class => Float 42 | 43 | x = true # x.class => TrueClass 44 | 45 | x = false # x.class => FalseClass 46 | 47 | x = nil # x.class => NilClass 48 | 49 | # ================= 50 | # array 51 | # ================= 52 | 53 | countries = ["canada", "brazil", "spain"] 54 | 55 | # ================= 56 | # hash 57 | # ================= 58 | 59 | person = { :name => "Brock Whitten", :location => "West End" } 60 | 61 | # ================= 62 | # collection (array of hashes) 63 | # ================= 64 | 65 | people = [ 66 | { :name => "Brock Whitten", :location => "West End" }, 67 | { :name => "Brian Leroux", :location => "Gastown" }, 68 | { :name => "Thurston Moore", :location => "NYC" } 69 | ] 70 | 71 | # ================= 72 | # methods 73 | # ================= 74 | 75 | # a simple method 76 | # 77 | def hello 78 | puts "hello world!" 79 | end 80 | 81 | # with an argument 82 | # 83 | def hello(place) 84 | puts "hello #{place}!" 85 | end 86 | 87 | # add a default to the argument 88 | # 89 | def hello(place="world") 90 | puts "hello #{place}!" 91 | end 92 | 93 | # add a default to the argument 94 | # 95 | def hello(place="world", *args) 96 | #... 97 | end 98 | 99 | # ================= 100 | # blocks 101 | # ================= 102 | 103 | 3.times do 104 | puts "hello!" 105 | end 106 | 107 | # lets iterate over an array 108 | # 109 | countries.each do |country| 110 | puts "hello #{country}" 111 | end 112 | 113 | # lets iterate over the hash 114 | # 115 | person.each do |key, value| 116 | puts "the value of #{key} is #{value}" 117 | end 118 | 119 | # this is also a block... 120 | # 121 | countries.each { |country| puts country.capitalize } 122 | 123 | # ================= 124 | # ranges 125 | # ================= 126 | 127 | cold_war = 1945..1989 128 | 129 | alphabet = "a".."z" 130 | 131 | cold_war.include?(1980) # => true 132 | 133 | alphabet.each {|l| print l } # => abcdefghijklmnopqrstuvwxyz 134 | --------------------------------------------------------------------------------