Created
September 2, 2013 15:06
-
-
Save emilebosch/6413874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple class | |
class Base | |
def self.this_class_method_will_be_called_on_load | |
puts "I'm loaded" | |
end | |
end | |
# Class /w inheritence | |
class TestClass < Base | |
this_class_method_will_be_called_on_load | |
#instance method | |
def im_instance_method | |
end | |
#class methods (statics in c#) | |
def self.im_a_class_method | |
end | |
#alternative ways to create class methods - Handy if you have a bunch to declare | |
class << self | |
def im_class_method2 | |
end | |
def im_class_method3 | |
end | |
end | |
end | |
# Dynamic class creation | |
class BaseController | |
def self.im_a_class_method | |
puts "Im called in a class method from the BaseController" | |
end | |
def greeting | |
puts "Hello there" | |
end | |
end | |
class_name = 'MyClassName' | |
klass = Object.const_set(class_name, Class.new(BaseController)) # Inherits from BaseController | |
klass.im_a_class_method | |
module Helloify | |
def hello | |
puts "Im from the module Hello" | |
end | |
end | |
klass.extend(Helloify) # Extends add class methods | |
klass.hello | |
# Breakign open/overriding classes (Check that now the const MyClassName exists!) | |
class MyClassName | |
def self.hello | |
puts "This" | |
end | |
end | |
# Instantiate a new class | |
myInstance = klass.new | |
myInstance.greeting | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment