No commit activity in last 3 years
No release in over 3 years
RubyCollections gem provides with general collections framework for Ruby language.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.8
~> 10.0
 Project Readme

RubyCollections

Welcome to ruby_collections gem. This gem will provide you with an easy access to common data structures to be used in Ruby Language.

Installation

Add this line to your application's Gemfile:

gem 'ruby_collections'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ruby_collections

Usage

Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList, Deque, UnionFind

RubyCollections::MaxHeap

max_heap = RubyCollections::MaxHeap.new

max_heap.insert(1)

max_heap.insert(4)

max_heap.heap # => [nil, 1, 4]

max_heap.max # => 4

max_heap.extract_max # => 4

max_heap.max # => 1

Similarly we can use MinHeap as well.

RubyCollections::Stack

stack = RubyCollections::Stack.new

stack.push(1) # => [1]

stack.push(4) # => [4, 1]

stack.top # => 4

stack.pop # => 4

stack.top # => 1

RubyCollections::LinkedList

list = RubyCollections::LinkedList.new

list.size # => 0

list.header # => ""

list.empty? # => true

list.add(1) # => 1 (number of elements in list)

list.add(2) # => 2 (number of elements in list)

list.to_s # => "[2, 1]"

list.add(3,1) # => 3 (number of elements in list)

list.to_s # => "[2, 3, 1]"

list.remove(2) # => 2 (removes element at index 2)

list.to_s # => "[2, 3]"

list.get(1) # => 3 (returns element at index 1)

list.get(1).setNext(12) # => adds a new node after index 1 with value 12. list is now [2, 3, 12]

list.get(1).data = 5 # => changes the value at index 1. list is now [2, 5, 12]

list.get(1).getNext # => 12

RubyCollections::Deque

list = RubyCollections::Deque.new

list.size # => 0

list.head # => nil

list.empty? # => true

list.add(2) # => 1 (number of elements in list), list: [2]

list.add(1) # => 2 (number of elements in list), list: [1,2]

list.to_s # => "[1, 2]"

# add 3 at index 1

list.add(3,1) # => 3 (number of elements in list), list: [1, 3, 2]

# add 5 at first postion

list.add_first(5) # list: [5, 1, 3, 2]

# see tail element

list.tail # => 2

#add 6 at last position

list.add_last(6) # list: [5, 1, 3, 2, 6]

list.size # => 5

# get element at index 2

list.get(2) # => 3

# set element at index 1 to 10

list.set(1, 10) # list; [5, 10, 3, 2, 6]

# remove element at index 2

list.remove(2) # list: [5, 10, 2, 6]

# remove first element

list.remove_first # list: [10, 2, 6]

# remove last element

list.remove_last # list: [10, 2]

RubyCollections::UnionFind

# initialization takes an array argument. each element should have a to_s method defined which should return uniq val

uf = RubyCollections::UnionFind.new (1..8).to_a 

uf.union(1,3) # joins 1 and 3 and returns the leader

uf.union(5,6) # joins 5 and 6 and returns the leader

uf.union(2,4) # joins 2 and 4 and returns the leader

uf.union(1,7) # joins 1 and 7 and returns the leader

uf.find(7) # leader of the cluster containing 7

uf.to_a  # => [[1, 3, 7], [2, 4], [5, 6], [8]] i.e. returns array of all clusters

uf.cluster(1) # => [1, 3, 7] i.e. returns all elements in same cluster as of 1

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/ruby_collections/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request