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
require 'ostruct' | |
computer = OpenStruct.new(ram: '4GB') | |
computer.class # => OpenStruct | |
computer.methods(false) # => [] | |
computer[:ram] # => "4GB" | |
computer.methods(false) # => [] | |
computer.ram # => "4GB" |
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
require 'ostruct' | |
computer = OpenStruct.new(ram: '4GB') | |
computer.class # => OpenStruct | |
computer.ram # => "4GB" | |
computer[:ram] # => "4GB" | |
computer['ram'] # => "4GB" | |
computer.screens = 2 |
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
Struct.new('Address', :street, :city, :zip) | |
Struct::Address.superclass # => Struct | |
home = Struct::Address.new('Broadway', 'NYC', 10040) | |
home.class # => Struct::Address |
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
Address = Struct.new(:street, :city, :zip) | |
class Address | |
def full_address | |
"#{street} #{city} #{zip}" | |
end | |
end | |
home = Address.new('Broadway', 'NYC', 10040) |
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
Address = Struct.new(:street, :city, :zip) | |
Address.class # => Class | |
Address.superclass # => Struct |
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
home # => #<struct Address street="Broadway", city="NYC", zip=10040> | |
home.street = 'Opéra' | |
home[:city] = 'Paris' | |
home['zip'] = 75009 | |
home # => #<struct Address street="Opéra", city="Paris", zip=75009> |
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
home.street # => "Broadway" | |
home[:city] # => "NYC" | |
home['zip'] # => 10040 | |
home.not_exist # => NoMethodError: undefined method `not_exist' | |
home[:not_exist] # => NameError: no member 'not_exist' in struct | |
home['not_exist'] # => NameError: no member 'not_exist' in struct |
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
Address = Struct.new(:street, :city, :zip) | |
home = Address.new('Broadway', 'NYC', 10040) |
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
class User | |
def self.inherited(user_type) | |
puts "#{user_type} is a kind of user" | |
end | |
end | |
class Tenant < User | |
end |
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
module Commentable | |
def self.included(commentable_entity) | |
puts "The #{commentable_entity} entity now accepts comments !" | |
end | |
end | |
class MediumPost | |
include Commentable | |
end |