Skip to content

Instantly share code, notes, and snippets.

@mkurtikov
Last active September 7, 2016 15:26
Show Gist options
  • Save mkurtikov/29862db7b595a104690b2855644c7a84 to your computer and use it in GitHub Desktop.
Save mkurtikov/29862db7b595a104690b2855644c7a84 to your computer and use it in GitHub Desktop.
Ruby classes
# Class
# http://ruby-doc.org/core-2.3.0/Class.html
class A
end
A.new.class # A
class B < A
end
B.new.class # B
B.superclass # A
A.superclass # Object
Object.superclass # BasicObject
BasicObject.superclass # nil
(b = B.new) && b.is_a?(B) && b.kind_of?(B) # true
(b = B.new) && b.is_a?(A) && b.kind_of?(A) # true
B.new.instance_of? B # true
B.new.instance_of? A # false
a = 1
defined? a # "local-variable"
@a = 2
defined? @a # "instance-variable"
@@a = 3
defined? @@a # "class variable"
$a = 4
defined? $a # "global-variable"
class A
def initialize(a)
@a = a
end
def print_a
p @a
end
def self.create_with_a
A.new(22)
end
end
A.new(1).print_a # 1
A.create_with_a.print_a #22
class C
def a
@a
end
def a=(val)
@a = val
end
end
c = C.new
c.a # nil
c.a = 1
c.a # 1
class D
attr_accessor :a
attr_writer :b
attr_reader :c
def initialize(a, b, c)
@a = a
@b = b
@c = c
end
end
d = D.new 1, 2, 3
d.a # 1
d.a = 10
d.a # 10
d.b # NoMethodError: undefined method `b'
d.b = 4
d.instance_variable_get(:@b) # 4
d.c # 3
d.c = 11 # NoMethodError: undefined method `c='
d.instance_variable_set(:@c, 5)
d.c # 5
class Arc
attr_accessor :radians
def degrees
@radians * 180 / Math::PI
end
def degrees=(degrees)
@radians = degrees * Math::PI / 180
end
end
arc = Arc.new
arc.degrees = 180
arc.radians # => 3.14159265358979
arc.radians = 3.141592653589793 / 2
arc.degrees # => 90.0
class Summator
attr_reader :sum
def initialize(start_value = 0)
@sum = start_value
end
def +(value)
@sum += value if value.is_a? Numeric
end
def == (summator)
(summator.is_a? self.class) && summator.sum == sum
end
end
summator = Summator.new
summator.sum # 0
summator + 1
summator.sum # 1
summator2 = Summator.new
summator == 1 #false
summator == summator # true
summator == summator2 # false
summator == (summator2 + 1) #false
summator == summator2 # true
class Summator
attr_reader :sum
def initialize(start_value = 0)
@sum = start_value
end
def +(value)
@sum += value if value.is_a? Numeric
self
end
def == (summator)
(summator.is_a? self.class) && summator.sum == sum
end
end
summator = Summator.new
summator + 1 # summator
summator2 = Summator.new
summator == summator2 # false
summator == (summator2 + 1) #false
class Summator
attr_reader :sum
def initialize(start_value = 0)
@sum = start_value
end
def +(value)
@sum += value if value.is_a? Numeric
self
end
def == (summator)
(summator.is_a? self.class) && summator.sum == sum
end
end
1 + Summator.new # TypeError: Summator can't be coerced into Fixnum
class Summator
def coerce(other)
[sum + other, other]
end
end
class SummatorAgr < Summator
def coerce(other)
[self + other, other]
end
end
1 + Summator.new # 2
(1 + SummatorAgr.new).sum # 2
class A
attr_accessor :val1, :val2
def print_self
p self
end
def inspect
"val1: #{@val1} val2: #{self.val2}"
end
end
a = A.new
a.print_self # val1: val2:
a.val1 = 1
a.val2 = 2
a.print_self # val1: 1 val2: 2
a.inspect # "val1: 1 val2: 2"
class A
attr_reader :val1
@@has_at_least_one_instace = false
def initialize(val1)
@@has_at_least_one_instace = true
@val1 = val1
end
def self.has_at_least_one_instace?
@@has_at_least_one_instace
end
def self.create_with_parameters
self.new 99
end
end
A.has_at_least_one_instace? #false
A.create_with_parameters.val1 # 99
A.has_at_least_one_instace? #true
class A
MY_CONSTANT = 3
def self.constant
MY_CONSTANT
end
def self.update_constant(val)
# self::MY_CONSTANT = val # SyntaxError: dynamic constant assignment
end
end
A::MY_CONSTANT
A.constant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment