Skip to content

Instantly share code, notes, and snippets.

@srinidhi-lwt
Last active May 17, 2019 09:29
Show Gist options
  • Save srinidhi-lwt/059a881f48adb10c83781fc9047538cd to your computer and use it in GitHub Desktop.
Save srinidhi-lwt/059a881f48adb10c83781fc9047538cd to your computer and use it in GitHub Desktop.
BRUG MAY 2019 - Code Examples
# example for send
'brug'.split('')
'brug'.class.ancestors
# ====================================================
# LEVEL 0
class Employee
def name
'Hi! I am a Employee.'
end
private
def hello
'Say Hola!'
end
end
# ====================================================
# LEVEL 1
# Method missing example
class Account
attr_accessor :state
def initialize(state)
@state = state
end
def method_missing(method_name, *args, **keyword_args, &block)
if result = method_name.match(%r(\Adebit_\d*\z))
self.state = state - extract_number(result)
elsif result = method_name.match(%r(\Acredit_\d*\z))
self.state = state + extract_number(result)
else
super
end
end
private
def extract_number(matched_result)
matched_result[0].split('_')[1].to_i
end
end
account = Account.new(20000)
account.debit_200
account.state
account.credit_999
account.state
# ========================================================
# LEVEL 2
# Ruby Method Precedence example
module Developer
def name
puts "Hi. This is Developer"
end
end
class Employee
def name
puts "Hi. This is Employee"
end
end
class Analyst < Employee
include Developer
def name
puts "Hi. This is Analyst"
end
end
analyst = Analyst.new
analyst.name
# ========================================================
# LEVEL 3
# Ruby Method Precedence example
module Developer
def name
puts "Hi. This is Developer"
end
end
module Analyst
def name
puts "Hi. This is Analyst"
end
end
class Employee
include Developer
def name
puts "Hi. This is Employee"
end
end
e = Employee.new
e.extend(Analyst)
e.name
# ========================================================
# LEVEL 4
# Ruby Method Precedence example
module Developer
def name
puts "Hi. This is Developer"
end
end
module Analyst
def name
puts "Hi. This is Analyst"
end
end
class Employee
include Analyst
prepend Developer
def name
puts "Hi. This is Employee"
end
end
e = Employee.new
e.name
# ========================================================
# LEVEL 5
# Ruby Method Precedence example
module Reviewer
def name
puts "Hi. This is Reviewer"
super rescue nil
end
end
module Manager
def name
puts "Hi. This is Manager"
super rescue nil
end
end
module Analyst
def name
puts "Hi. This is Analyst"
super rescue nil
end
end
class Employee
# inherited method to class Employee
def name
puts "Hi. This is Employee "
super rescue nil
end
end
class Developer < Employee
include Reviewer # including method into class Employee
prepend Analyst # prepending method to class Employee
# instance method to class Employee
def name
puts "Hi. This is Developer"
super rescue nil
end
end
developer_1 = Developer.new
def developer_1.name
puts "Hi. This is developer_1"
super
end
developer_1.extend(Manager)
developer_1.name
# ========================================================
# DSL Example
FactoryGirl.define do
sequence :github_username do |n|
"github_#{n}"
end
factory :user do
description "Learn all about Git"
github_username
trait :admin do
admin true
end
end
end
# ========================================================
# DSL Example
SearchFlight.origin('BLR').destination('DEL').depart('2019-05-31').return('2019-06-10')
# ========================================================
# define method example
class Doctor
["rhinoplasty", "checkup", "interpretive_dance"].each do |action|
define_method("perform_#{action}") do |argument|
"performing #{action.gsub('_', ' ')} on #{argument}"
end
end
end
doctor = Doctor.new
puts doctor.perform_rhinoplasty("nose")
puts doctor.perform_checkup("throat")
puts doctor.perform_interpretive_dance("in da club")
# ========================================================
# define method
class Emoji
def initialize
emoji_lists = {
grinning_face: "&#128512",
grinning_face_with_smiling_eyes: "&#128513",
face_with_tears_of_joy: "&#128514",
rofl: "&#129315",
smiling_face_with_open_mouth: "&#128515",
smiling_face_and_smiling_eyes: "&#128516",
smiling_face_and_cold_sweat: "&#128517"
}
emoji_lists.each do |key, value|
self.class.send(:define_method, "#{key}") do
value
end
end
end
end
# ========================================================
# class_eval example
class Employee
end
# Employee is evaluated as a class
Employee.class_eval do
def say_hello
puts 'Hello'
end
end
e = Employee.new
e.say_hello
=> Hello
# String is evaluated as a class
'brug'.class.class_eval do
def split_by_word
split(' ')
end
end
'Srinidhi is presenting'.split_by_word
=> ["Srinidhi", "is", "presenting"]
# ========================================================
# instance_eval example
class Mentor
end
teacher = Mentor.new
teacher_1 = Mentor.new
# teacher is evaluated as a instance
teacher.instance_eval do
def evaluate
puts 'Yes I evaluate'
end
end
teacher.evaluate
=> Yes I evaluate
teacher_1.evaluate
NoMethodError: undefined method `evaluate' for #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment