Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rns, which stands for "Ruby namespaces", is a small library for using classes and modules as packages of functions in order to support functional programming in Ruby. It is inspired by Clojure's ns macro and namespace system.

Build Status

Usage

Importing Methods into Classes

require 'rns'

module Arithmetic
  class << self
    def dec(n) n - 1 end
    def inc(n) n + 1 end
  end
end

module Statistics
  def self.avg(arr); arr.reduce(:+).to_f / arr.count end
end

class Main
  extend Rns.use(Arithmetic => [:inc])
  include Rns.use(Statistics => [:avg])

  def self.incremented(n)
    "#{n} incremented is #{inc n}"
  end

  def main
    puts "#{self.class.incremented 1} and the average of [1,2,3] is #{avg [1,2,3]}"
  end
end

Main.new.main

Importing Methods into Blocks

Rns::using(Arithmetic => [:inc], Statistics => [:avg]) do
  puts avg((1..10).to_a.map(&method(:inc)))
end

Please see the tests for more usage examples.

Rationale

Ruby has good functional programming support, but the class and module system doesn't lend itself to organizing and accessing functions. With rns I hope to make it at least slightly easier to build Ruby programs primarily out of pure functions.

Thanks

To Sam Umbach for helping me tame the eigenclass, and to my employer Relevance for indulging me with time to work on free software.

About

A namespacing library supporting functional programming in Ruby

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages