Code Monkey home page Code Monkey logo

week-21's Introduction

The Monkey in the Jungle

Monkey-patching is a powerful tool in Ruby at your disposal. And "with great power comes great responsibility". It gets a bad name because of the often unintended confusion and damage that it can cause within your own code and code other people have implemented.

Exercise

The objective of the exercise is to accomplish the following:

Understand the Problem

First and foremost is to understand how you can monkey-patch.

Implement a new #empty? method on String which returns true when the string contains no characters or all whitespace characters; returning false otherwise.

During the first part of the exercise spend some time discussing and implementing a few solutions that employ monkey-patching.

Address the Problem

The second objective is to take your initial implementations and re-implement or supplement your implementation so that it done more safely. Discuss what you might do to make your implementation more safe.

Exercise Retrospective

Ruby provides a few ways to monkey-patch because of it's Open Classes. Discuss what you liked and disliked about your implementations.

Would you change your solution in the follow circumstances:

  • overriding Ruby's core functionality
  • appending a new method on a prominent class in the Ruby's core language
  • appending a new method on a not so prominent class in the Ruby's core language

Would your solution change if this code lived in a different context?

  • A command-line tool
  • A background application
  • A Rails application
  • A Ruby gem
  • A one-off script
  • A project with few dependencies
  • A project with many dependencies

Further Exercise

The solution of this exercise only requires that you implement one method method for one class. How might you develop a solution that allows you to more easily monkey-patch any class with multiple methods with the same safety strategy?

  • How could you make a gist of a pattern that you could copy and paste into a new project?

  • Ruby's Module has an method, included, which is called when the module has been used by a class. How could you use that to ensure methods are not overridden?

  • Ruby's Module has a method called define_method which allows you to define a method dynamically within code. How could that be used to add a method, while ensuring not to override existing methods?

Reading

  • Ruby Metaprogramming pages 27 - 47
  • Ruby Metaprogramming pages 152 - 156
  • Ruby Metaprogramming pages 232 - 237

week-21's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.