Slide 35
Slide 35 text
35
Lambdas in Behavior Lookups
Example: Formatters
def formatters
@formatters ||= {
amazing_print: ->(obj) { obj.ai << "\n" },
inspect: ->(obj) { obj.inspect + "\n" },
json: ->(obj) { obj.to_json },
marshal: ->(obj) { Marshal.dump(obj) },
none: ->(_obj) { nil },
pretty_json: ->(obj) { JSON.pretty_generate(obj) },
pretty_print: ->(obj) { obj.pretty_inspect },
puts: ->(obj) { sio = StringIO.new; sio.puts(obj); sio.string },
to_s: ->(obj) { obj.to_s + "\n" },
yaml: ->(obj) { obj.to_yaml },
}
end