ToKnow - acanyon/RubyTuesdays-ConnectFour GitHub Wiki

methods

  • They do things, with a method object can perform actions

    def Zassmin
         puts 'running'
    end
    $ Zassmin
    $ => running
    
  • In this method, I'm running! Helpful reference: Beginning Ruby, pg 23

class

  • A type of object we are defining
class Player
     attr_accessor :name
end
  • There is this Human class, that always starts with a capital letter, it's defined with a name and anything else we put in it, you notice, each model and controller page starts by naming the class. More on classes here: Beginning Ruby, pg 18

module

  • Let's say we have 2 classes with the same name that are meant to do different things in our app
class Player
    def player_one 
        'blue'
    end
end
  • and
class Player
    attr_accessor :name
end
  • In the first class we gave player_one a name, in the second, we will give a new Player a name when we call on the class, if we run Player.new which one will work??? See below:
$ player_instance = Player.new
$ player_instance.name = 'blue'
$ => 'blue'
  • It looks like the last one ran but I wanted to first Player class to run, what to do?
module PlayerWithName
    class Player
        def player_one 
            'blue'
        end
    end
end

module PlayerToMakeName
    class Player
        attr_accessor :name
    end
end
  • Now if we want to call on a specific player class we do
$ player_name = PlayerWithName::Player.new
$ player_name.player_one
$ => 'blue'
  • that's why modules are fun, they can also be used with methods and constants for help read: Beginning Ruby, pg 139

serializing...

inheritance

  • Looks like this:
class Game < ActiveRecord
  • We use it so different classes or modules can 'relate to one another and group concepts by their similarities', refer to: Beginning Ruby, pg 21

namespaces && modules

  • Looks like this
class Game < ActiveRecord::Base
  • Base is a class access directly from ActiveRecord that Game is inheriting, good reference: Beginning Ruby, pg 149

extend and include

class Game < ActiveRecord::Base
    extend Game::BoardMixin
    include Game::BoardMixin
end
  • include makes the module available to the instance of the class
  • extend makes the methods available to the class
  • the reference I used is here