-
-
Save daemianmack/2599059 to your computer and use it in GitHub Desktop.
A different way to organize Ruby code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ns | |
def initialize | |
use.each do |pkg, method_names| | |
method_names.each do |name| | |
self.class.send(:define_method, name) do |*args| | |
pkg.method(name).call(*args) | |
end | |
end | |
end | |
end | |
end | |
module Math | |
def self.dec(n) n - 1; end | |
def self.inc(n) n + 1; end | |
end | |
module Cows | |
def self.meh_cow; "Moo." end | |
def self.angry_cow | |
[meh_cow.slice(0..-2)]. | |
cycle. | |
take(3). | |
zip(["! "].cycle). | |
flatten. | |
join. | |
slice(0..-2) | |
end | |
end | |
# Meanwhile, back at the farm: | |
class Main < Ns | |
def use | |
{Math => [:inc], | |
Cows => [:meh_cow, :angry_cow]} | |
end | |
def main | |
puts "The meh cow says '#{meh_cow}', the angry cow says '#{angry_cow}', and 1+1 is #{inc 1}!" | |
end | |
end | |
Main.new.main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment