Last active
February 7, 2017 21:11
-
-
Save BenGitsCode/6f405168bfd8dbc5bc6b2cf13ce4c0fa to your computer and use it in GitHub Desktop.
Ruby basics review code
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
# Stores can sell things | |
class Store | |
def sell | |
'You bought the thing!' | |
end | |
end | |
# contains methods common to hot drinks | |
module HotDrink | |
# this defines an instance method make_hot_drink | |
# it is available to instances of any class that includes HotDrink | |
# it sets the drink, and returns the INSTANCE that it was called on | |
def make_hot_drink | |
self.drink = 'Hot water' | |
self | |
end | |
def add_cream | |
self.drink = ' + cream' | |
self | |
end | |
end | |
# A class of stores that doesn't sell hot drinks | |
class PetStore < Store | |
end | |
# A class of stores that sells coffee | |
class CoffeeShop < Store | |
include HotDrink | |
# creates a instance variable @drink and makes it readable and writable | |
attr_accessor :drink | |
def make_hot_drink | |
# drink - drink is an instance variable | |
# We can access drink (read and write to it) because of the | |
# attr_accessor :drink above | |
# super will look for a method called make_hot_drink in the | |
# inheritance chain | |
self.drink = drink ' + filter coffee' | |
self | |
end | |
end | |
# A class of stores that sells Tea | |
# Initialized with one instance variable, name | |
class TeaShop < Store | |
include HotDrink | |
attr_reader :type_of_drink | |
attr_accessor :drink | |
# this method is run when I write TeaShop.new("Rachel's Tea Shop") | |
def initialize(name) | |
# This sets and instance variable based on the name I passed in | |
@name = name | |
# This sets an instance variable, @type_of_drink | |
# It is automatically set by the inialiaze method and cannot be changed | |
@type_of_drink = 'Tea' | |
end | |
# this method is chainable because it returns self | |
def change_name(new_name) | |
self.name = new_name.upcase | |
self | |
end | |
# this method is not chanable because it doesn't return self | |
def add_awesomeness | |
@name + 'Awesome' | |
end | |
# CLASS method | |
# Class methods start with self. | |
# Class methods are not available to instances of the class | |
# The way to call the class method is TeaShop.tell_me_about_tea_shops | |
def self.tell_me_about_tea_shops | |
puts 'Tea shops are amazing' | |
# self is the class, TeaShop | |
self | |
end | |
# Another class method | |
# This method returns a string, not the class, so it is not chainable | |
def self.quickly | |
"It's a short class method name" | |
end | |
# This is not a class method because it does NOT begin with self. | |
# it is an instance method | |
# this method is chainable because it returns self | |
def make_hot_drink | |
# super looks for a method called make_hot_drink and | |
# finds it on the HotDrink module, and executes it | |
# the hot drinks module sets the drink instance variable | |
# to 'hot water' | |
super | |
# we can assign to drink because of the attr_accessor above | |
# I used += instead of self.drink = self.drink + ' + tea' | |
# because the linter told me to | |
# this line adds ' + tea' to the drink that we created with | |
# calling super | |
self.drink += ' + tea' | |
self | |
end | |
end | |
# NAMESPACE EXAMPLE | |
module SomeNamespace | |
class Animal | |
end | |
end | |
animal = SomeNamespace::Animal.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment