Skip to content

Instantly share code, notes, and snippets.

@octosteve
Created April 12, 2016 17:17
Show Gist options
  • Save octosteve/7da061e28cd0a219416c36261a2ecc3f to your computer and use it in GitHub Desktop.
Save octosteve/7da061e28cd0a219416c36261a2ecc3f to your computer and use it in GitHub Desktop.
class Person
attr_reader :name # => nil
def initialize(name)
@name = name # => "Bob"
end
def to_s
"Hi, My name is chicka chicka #{name}" # => "Hi, My name is chicka chicka Bob"
end
end
bob = Person.new("Bob") # => #<Person:0x007faab88964c0 @name="Bob">
bob.name # => "Bob"
bob.to_s # => "Hi, My name is chicka chicka Bob"
# Is a relationship Inheritance
# Has a relationship Modules Mixins
# Super class!!!!!
class Media
attr_accessor :name, :location
end
# Super class
# Mixin
module Viewable
attr_accessor :resolution
end
#
class Video < Media
include Viewable
def description
@description
end
def description=(new_description)
@description = new_description
end
end
class Image < Media
include Viewable
end
class Audio < Media
# attr_accessor :name, :location, :quality
attr_accessor :quality
end
never_gonna_give_you_up = Video.new
never_gonna_give_you_up.resolution = "4k"
never_gonna_give_you_up.resolution
Video.ancestors
class Media
attr_accessor :name, :location # => nil
end
module Viewable
attr_accessor :resolution # => nil
def description=(new_description)
@description = "New Viewable item #{new_description}"
end
end
module Describable
def describe
"Hey, I'm a class" # => "Hey, I'm a class"
end
end
class Video < Media
prepend Viewable # => Video
extend Describable # => Video
def description
@description
end
def description=(new_description)
# @description = new_description # => "Best video ever"
# result = super # => "New Viewable item Best video ever"
# @description = result.reverse # => "reve oediv tseB meti elbaweiV weN"
@description = super.reverse
end
end
Video.describe # => "Hey, I'm a class"
never_gonna_give_you_up = Video.new # => #<Video:0x007fea9b1b0cf0>
Video.ancestors # => [Viewable, Video, Media, Object, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]
# never_gonna_give_you_up.resolution = "4k" # => "4k"
# never_gonna_give_you_up.resolution # => "4k"
# never_gonna_give_you_up.description = "Best video ever" # => "Best video ever"
# never_gonna_give_you_up.description # => "reve oediv tseB meti elbaweiV weN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment