Skip to content

Instantly share code, notes, and snippets.

@MilanGrubnic70
Last active August 29, 2015 14:00
Show Gist options
  • Save MilanGrubnic70/11098110 to your computer and use it in GitHub Desktop.
Save MilanGrubnic70/11098110 to your computer and use it in GitHub Desktop.
Classes

Creating a class syntax:

class ClassName
    def MethodName( parameter )
        @classVariable = parameter
    end
end

class ClassName
  def initialize(param1, param2)
    @param1 = param1
    @param2 = param2
  end
end

You instantiate an object from a class with:

object = ClassName.new(parameters)

You can directly access the attributes or methods of a superclass with Ruby's built-in super keyword.

Super - When you call 'super' from inside a method, that tells Ruby to look in the superclass of the current class and find a method with the same name as the one from which super is called. If it finds it, Ruby will use the superclass' version of the method.

class SubClass < SuperClass
end

class DerivedClass < Base
  def some_method
    super(optional args)
      # Some stuff
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment