Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 2-Io/day3-method_missing.io @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 46a807289724 |
children |
line wrap: on
line source
# Altering "forward" in Io is apparently similar to overriding # method_missing in Ruby. Except with more to go wrong because # we have prototypes and not classes. # Building arbitrary (basic) XML using a Builder prototype that # assumes all missing methods are XML tags Builder := Object clone # This is the dangerous method Builder forward := method( writeln("<", call message name, ">") call message arguments foreach( arg, # This will trigger another call to forward # It's not clear why "writeln" would return a string rather than write to the console # But apparently it does. Because poorly documented Io. content := self doMessage(arg) # No strong type checking here - it's all based on string names if (content type == "Sequence", writeln(content)) ) writeln("</", call message name, ">") ) Builder ul( li("Obscure"), li("Undocumented"), li("Unclear") )