March 14th, 2015
In Technology
No Comments
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
If you enjoy this article, see the other most popular articles
Ruby reinvented to look like Clojure
(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.
To be clear, I like everything about Thomas Reynolds “Weird Ruby”:
# The standard "record" that contains information about a file on disk. SourceFile = Struct.new :relative_path, :full_path, :directory, :types # Find a file given a type and path. # # @param [Symbol] type The file "type". # @param [String] path The file path. # @param [Boolean] glob If the path contains wildcard or glob. # @return [Middleman::SourceFile, nil] Contract Symbol, String, Maybe[Bool] => Maybe[SourceFile] def find(type, path, glob=false) watchers .lazy .select { |d| d.type == type } .map { |d| d.find(path, glob) } .reject(&:nil?) .first end
A Record, represented as a Struct in Ruby, is simply a Hash which has pre-defined its keys. This allows you to treat data that represents some item in a predictable way rather than constantly checking for keys or creating a Class which “models” the data.
# The standard “record” that contains information about a file on disk.
SourceFile = Struct.new :relative_path, :full_path,
:directory, :typesThese days, I’m not a big fan of Classes or inheritance, so the idea of attaching methods to a Class that only works in one context is not appealing. When I use a Record instead, I just get pure data, which is well named, and my methods can either pretend it is a normal Hash or ask if it’s a SourceFile and do something special.
Post external references
- 1
http://awardwinningfjords.com/2015/03/03/my-weird-ruby.html
February 8, 2022 9:33 am
From Michael S on How I recovered from Lyme Disease: I fasted for two weeks, no food, just water
"Did you have Bartonella, too? Seems it uses autogenesis..."