All Programming Is Metaprogramming: (Engineering Software As A Service 3.5)
All Programming Is Metaprogramming: (Engineering Software As A Service 3.5)
All Programming Is Metaprogramming: (Engineering Software As A Service 3.5)
Metaprogramming
(Engineering Software as a
Service §3.5)
http://pastebin.com/HJTvUid5
Reflection &
Metaprogramming
• You can ask Ruby objects
questions about themselves at
runtime (introspection)
• You can use this information to
generate new code (methods,
objects, classes) at runtime
(reflection)
• …so can have code that writes
code (metaprogramming)
• You can “reopen” any class at any
END
7
Suppose we want to handle
5.euros.in(:rupees)
What change to Numeric would be most
appropriate?
☐ Change Numeric.method_missing to
detect calls to 'in' with appropriate
args
☐ Change Numeric#method_missing to
detect calls to 'in' with appropriate
args
☐ Define the method Numeric#in
8
END
9
Blocks, Iterators,
Functional Idioms
(Engineering Software as a
Service §3.6)
for i in (1..10) do
puts i
end
1.upto 10 do |num|
puts num
end
☐ naan
☐ ananab
☐ anana
16
END
17
Mix-ins and Duck Typing
(Engineering Software as a
Service §3.7)
• Ruby emphasizes
“What methods do you respond
to?”
over
“What class do you belong to?”