Skip to content

Commit bd3a308

Browse files
committed
Add Method Missing notebook
1 parent f35c703 commit bd3a308

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ But if you prefer using Jupyter, you can follow the [official documentation](htt
2727
13. [Read/Write Text Files](notebooks/ReadWriteTextFiles.ipynb)
2828
14. [Regular Expressions](notebooks/RegularExpressions.ipynb)
2929
15. [Classes](notebooks/Classes.ipynb)
30+
16. [Method Missing](notebooks/MethodMissing.ipynb)
3031

3132
## Acknowledgments
3233

notebooks/MethodMissing.ipynb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "third-craps",
6+
"metadata": {},
7+
"source": [
8+
"# Ruby Method Missing\n",
9+
"\n",
10+
"When you send a message to an object, the object executes the first method it finds on its [method lookup path](http://phrogz.net/RubyLibs/RubyMethodLookupFlow.pdf) with the same name as the message. If it fails to find any such method, it raises a `NoMethodError` exception - unless you have provided the object with a method called `method_missing`.\n",
11+
"\n",
12+
"The `method_missing` method is passed the symbol of the non-existent method, an array of the arguments that were passed in the original call and any block passed to the original method.\n",
13+
"\n",
14+
"`method_missing` is in part a safety net: It gives you a way to interpret unanswerable messages and handle them gracefully."
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"id": "injured-lewis",
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"There's no method called anything here -- please try again.\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"class Dummy\n",
33+
" def method_missing(m, *args, &block)\n",
34+
" puts \"There's no method called #{m} here -- please try again.\"\n",
35+
" end\n",
36+
"end\n",
37+
"\n",
38+
"Dummy.new.anything"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "bulgarian-speaker",
44+
"metadata": {},
45+
"source": [
46+
"_You are also responsible for maintaining the `method_missing` signature._ It's possible to write a hook that captures only a missing method's name while ignoring its arguments and associated block."
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Ruby 3.0.0",
53+
"language": "ruby",
54+
"name": "ruby"
55+
},
56+
"language_info": {
57+
"file_extension": ".rb",
58+
"mimetype": "application/x-ruby",
59+
"name": "ruby",
60+
"version": "3.0.1"
61+
}
62+
},
63+
"nbformat": 4,
64+
"nbformat_minor": 5
65+
}

0 commit comments

Comments
 (0)