Created
April 24, 2018 11:18
-
-
Save anushadasari/59c8bc45e804a97cb6067e7015673693 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
This file contains 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
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'} |
This file contains 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
def my_method | |
x = "Zoomcar" | |
yield("Pedl") | |
end | |
x = "Amp" | |
my_method {|y| "#{x} #{y} rocks!"} |
This file contains 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 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) |
This file contains 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 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 |
This file contains 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 MyClass | |
def my_method | |
@v = 1 | |
end | |
end | |
obj = MyClass.new | |
obj.class | |
obj.instance_variables | |
obj.my_method | |
obj.instance_variables |
This file contains 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 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 |
This file contains 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
####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 |
This file contains 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 MyClass | |
def my_method | |
'M#my_method()' | |
end | |
end | |
class MySubclass < MyClass | |
end | |
obj = MySubclass.new | |
obj.my_method() |
This file contains 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
#####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 |
This file contains 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 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 |
This file contains 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 D | |
def x; x; end | |
end | |
class D | |
def y; y; end | |
end | |
obj = D.new | |
obj.x | |
obj.y |
This file contains 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' | |
# 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 |
This file contains 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 Printable | |
def print | |
end | |
def prepare_cover | |
end | |
end | |
module Document | |
def print_to_screen | |
prepare_cover | |
format_for_screen | |
end | |
def format_for_screen | |
end | |
def print | |
end | |
end | |
class Book | |
include Document | |
include Printable | |
end | |
b = Book.new | |
b.print_to_screen |
This file contains 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
3.times do | |
class C | |
puts "Hello" | |
end | |
end |
This file contains 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
inc = Proc.new { |x| x + 1 } | |
.... ##Deferred Evaluation | |
inc.call(2) | |
dec = lambda {|x| x - 1 } | |
dec.class | |
dec.call(2) |
This file contains 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 '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 |
This file contains 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 '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 |
This file contains 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
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