Skip to content

Instantly share code, notes, and snippets.

@anushadasari
Created April 24, 2018 11:18
Show Gist options
  • Save anushadasari/59c8bc45e804a97cb6067e7015673693 to your computer and use it in GitHub Desktop.
Save anushadasari/59c8bc45e804a97cb6067e7015673693 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
def a_method(a,b)
a + yield(a,b)
end
a_method(1,2) {|x,y| (x + y) * 3}
def another_method
return yield if block_given?
'no_block'
end
another_method
another_method {'here is a block'}
def my_method
x = "Zoomcar"
yield("Pedl")
end
x = "Amp"
my_method {|y| "#{x} #{y} rocks!"}
class MyClass
define_method :my_method do |my_arg|
my_arg * 3
end
end
obj = MyClass.new
obj.my_method(2)
##Dynamic Dispatch
obj.send(:my_method, 2)
class Roulette
def method_missing(name, *args)
person = name.to_s.capitalize
3.times do
number = rand(10) +1
puts "#{number}..."
end
puts "#{name} got a #{number}"
end
end
number_of = Roulette.new
class MyClass
def my_method
@v = 1
end
end
obj = MyClass.new
obj.class
obj.instance_variables
obj.my_method
obj.instance_variables
class Zoomcar
def initialize(text)
@text = text
end
def welcome
@text
end
end
my_obj = Zoomcar.new('Heya!')
my_obj.class
my_obj.class.instance_methods(false)
my_obj.instance_variables
####Method execution
class MyClass
def testing_self
@var = 10
my_method()
self
end
def my_method
@var = @var + 1
end
end
obj = MyClass.new
obj.testing_self
class MyClass
def my_method
'M#my_method()'
end
end
class MySubclass < MyClass
end
obj = MySubclass.new
obj.my_method()
#####Ancestor chain
module M
def my_method
'M#my_method()'
end
end
class C
include M
end
class D < C; end
D.new.my_method()
D.ancestors
class Payment
def method_missing(method, *args)
puts "You called: #{method}(#{args.join(',')})"
puts "You also passed it a block" if block_given?
end
end
p = Payment.new
p.get_me_all('a','b') do
end
class D
def x; x; end
end
class D
def y; y; end
end
obj = D.new
obj.x
obj.y
# require 'ostruct'
# a = OpenStruct.new
# a.color = 'red'
# a.color
class MyOpenStruct
def initialize
@attributes = {}
end
def method_missing(name, *args)
attribute = name.to_s
if attribute =~ /=$/
@attributes[attribute.chop] = args[0]
else
@attributes[attribute]
end
end
end
a = MyOpenStruct.new
a.color = 'red'
a.color
module Printable
def print
end
def prepare_cover
end
end
module Document
def print_to_screen
prepare_cover
format_for_screen
print
end
def format_for_screen
end
def print
end
end
class Book
include Document
include Printable
end
b = Book.new
b.print_to_screen
3.times do
class C
puts "Hello"
end
end
inc = Proc.new { |x| x + 1 }
.... ##Deferred Evaluation
inc.call(2)
dec = lambda {|x| x - 1 }
dec.class
dec.call(2)
require 'test/unit'
include Test::Unit::Assertions
def replace(array, from, to)
array.each_with_index do |e,i|
array[i] = to if e == from
end
end
def test_replace
colors = ['red', 'blue', 'yellow']
replace(colors, 'yellow', 'orange')
expected = ['red', 'blue', 'orange']
assert_equal expected, colors
end
require 'test/unit'
include Test::Unit::Assertions
########## Object Oriented approach
class Array
def replace(from, to)
each_with_index do |e,i|
self[i] = to if e == from
end
end
end
def test_replace
colors = ['red', 'blue', 'yellow']
colors.replace('yellow', 'orange')
expected = ['red', 'blue', 'orange']
assert_equal expected, colors
end
v1 = 1
class MyClass
v2 = 2
puts "Entered class: #{local_variables}"
def my_method
v3 = 3
puts "Inside method: #{local_variables}"
end
puts "Exiting method: #{local_variables}"
end
obj = MyClass.new
# obj.my_method
# obj.my_method
puts "End of program: #{local_variables}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment