Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A namespacing library supporting functional programming in Ruby

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Guardfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 rns.gemspec
README.md

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.

Something went wrong with that request. Please try again.