Project

rns

0.01
No commit activity in last 3 years
No release in over 3 years
rns is a namespace management library designed to ease functional programming in Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

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'

Arithmetic = Rns do
  def dec(n) n - 1 end
  def inc(n) n + 1 end
end

Statistics = Rns do
  def avg(arr) arr.reduce(:+).to_f / arr.count end
end

class Main
  Funcs = Rns(Statistics, Arithmetic => [:inc]) do
    def incremented_avg(nums)
      avg nums.map(&method(:inc))
    end
  end

  def main
    nums = [1, 2, 3]
    puts "The average of #{nums.inspect} incremented is: #{Funcs.incremented_avg nums}"
  end
end

Main.new.main

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.