Git Product home page Git Product logo

ruby-object-model's Introduction

Object Model notes:

start with classes and instances
Classes
  store instance methods
  have a superclass pointer
Instances
  store instance variables
  have a class pointer
Classes are also instances (of Class)
Method lookup
  when you invoke a method on an instance
  start by going to its class
  until you find the method, go to the superclass
  once you find it, create a scope for that object
  and run the code for the method in that scope
Scopes/Bindings
  all code executes in a scope
  store local variables
  they have a self (an instance that they are operating in)
  this gives them access to instance variables
  they can call methods on that object

Underneath the magic:

What are C classes and objects underneath the magic? https://github.com/ruby/ruby/blob/65a5162550f58047974793cdc8067a970b2435c0/ruby.h#L218-233

Example to use for review of lessons 0 through 4

class HasExtraStuff
  def get_name
    @name
  end

  def get_binding
    binding
  end
end

class Person < HasExtraStuff
  def initialize(name)
    @name = name
  end

  def get_initial
    get_name[0]
  end
end

joshs_name = "Josh"
josh = Person.new(joshs_name)
josh.get_name                 # => "Josh"
josh.get_initial              # => "J"
josh.get_binding.eval('self') # => #<Person:0x007fe6348454f0 @name="Josh">

ruby-object-model's People

Contributors

joshcheek avatar

Stargazers

 avatar KSHITIJ GAUTAM avatar Hardik Joshi avatar Koustubh Sinkar avatar Angus H. avatar gerry avatar Kit Pearson avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.