Skip to content

Commit

Permalink
Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
msatmod committed Aug 30, 2022
1 parent 18e5d20 commit 6067e3c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Day 16/Operator_overloading-1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Operator overloading in Ruby

class Animal

attr_accessor:name, :trait

def initialize(name, trait)

@name = name
@trait = trait

end

def +(other_object)

return Animal.new("#{self.name}#{other_object.name}", "#{self.trait}#{other_object.trait}")

end

end

class Zebra < Animal

end

a = Zebra.new("Filly", "Brave")

b = Zebra.new("Mega", "Smart")

puts (a + b) .inspect
14 changes: 14 additions & 0 deletions Day 16/Operator_overloading-2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Operator overloading in Ruby
# <", ">", "=" Comparable operators

include Comparable

def <=> (other)

return self.name<=>other.name

end

puts "Filly"<=>"Filly"

puts "Filly"<=>"Mega"
18 changes: 18 additions & 0 deletions Day 16/Operator_overloading-3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Operator overloading in Ruby
# <", ">", "=" Comparable operators

include Comparable

def <=> (other)

return self.value<=>other.value

end

puts "Filly"<=>"Filly"

puts "Filly"<=>"Mega"

puts 10<=>20

puts 100<=>20
32 changes: 32 additions & 0 deletions Day 16/Operator_overloading-4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Operator overloading in Ruby
# <", ">", "=" Comparable operators

class Myclass

include Comparable
attr_accessor:myname

def initialize(name)

@myname = name

end

def <=> (other)

return self.myname<=>other.myname

end

end

obj1 = Myclass.new("Filly")
obj2 = Myclass.new("Mega")

puts obj1 > obj2

puts obj1 < obj2

puts obj1 == obj2

puts obj1 != obj2
57 changes: 57 additions & 0 deletions Day 16/Operator_overloading-5.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Operator overloading in Ruby
# +", "-", "*", "/", "*", "%", "**" Operators

class Tester

attr_accessor:num

def initialize(num)

@num = num

end

def +(other)

return self.num+other.num

end

def *(other)

return self.num*other.num

end

def /(other)

return self.num/other.num

end

def %(other)

return self.num%other.num

end

def **(other)

return self.num**other.num

end

end

a = Tester.new(5)
b = Tester.new(3)

puts a+b

puts a*b

puts a/b

puts a%b

puts a**b
44 changes: 44 additions & 0 deletions Day 16/Operator_overloading-6.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Operator overloading in Ruby
# "[]", "[]", "<<", Operators using Array

class Tester

attr_accessor:arr

def initialize(*arr)

@arr = arr

end

def [] (x)

return @arr[x]

end

def []=(x, value)

@arr[x] = value

end

def <<(x)

@arr << x

end

end

a = Tester.new(0,1,2,3)

puts a[3]

a << 97

puts a[4]

a[5] = 101

puts a[5]

0 comments on commit 6067e3c

Please sign in to comment.