Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active September 8, 2018 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/a66dbb1e8416370245a4610e14ce7ff2 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/a66dbb1e8416370245a4610e14ce7ff2 to your computer and use it in GitHub Desktop.
dummy_rails_app
# lib/core_ext/hash.rb
class Hash
# monkey-patch to temporarily
# disabling the MyLib#print_config output
def to_json
''
end
end
# lib/my_lib.rb
class MyLib
attr_accessor :config
def initialize
config = {}
yield(config) if block_given?
end
def print_config
"config: #{config.to_json}" # <= Monkey-patch applied here
end
end
# app/controllers/products_controller.rb
class ProductsController < ApplicationController
def index
@products = {
products: [
# list of products
]
}
render json: @products # <= Side-effect of the monkey-patch
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment