Code Monkey home page Code Monkey logo

redwood's Introduction

redwood

Simple Ruby trees. Redwood is a simple implementation of a tree data structure in pure Ruby. It provides a few things:

  • The redwood command-line tool: like the Unix tree tool, but in Ruby!
  • The Redwood module for basic tree-esqueness.
  • Redwood::Node class for basic tree-nodiness
  • Redwood::FileNode class for representing Directories and Files in a tree-like way.

RDoc | Gem

Installation

gem install redwood

redwood

The redwood command-line tool attempts a pure Ruby implementation of tree.

USAGE: redwood [ OPTIONS ] [ DIRECTORY ]

Looks a bit like this:

Redwood
|-- bin
|   `-- redwood
|-- Gemfile
|-- lib
|   |-- redwood
|   |   |-- filenode.rb
|   |   `-- node.rb
|   `-- redwood.rb
|-- LICENSE
|-- pkg
|   `-- redwood-0.0.1.gem
|-- Rakefile
|-- README.md
|-- redwood.gemspec
`-- test
    |-- helper.rb
    `-- test_redwood.rb

5 directories, 12 files

Help is a redwood --help away. See also: redwood(1)

Redwood

The Redwood module is a module for including/extending tree-like features on your objects. It stores nodes in an Array. The only requirement for children is that they too include/extend tree-like features.

Methods include:

root?	 		## Is this a root node? Meaning, it has no parent.
leaf? 			## Is this a leaf node? Meaning, is it without children?
root  			## Get the root node in this tree.
children 		## Get the children of this node.
siblings		## Get this nodes siblings.
only_child? 	## Is this node without siblings?
has_children? 	## Does this node have children?
ancestors 		## All of the parent nodes of this node.
descendants 	## All of the descendant nodes of this node.
depth 			## Integer representing how deep this node is in the tree.
				## A root node has a depth of 1, its children: 2, etc.
height			## The length of this node to its furthest descendant.
				## A leaf node has a height of 1.
unlink			## Detach this node from its parent.
prune			## Unlink all of this node's chidren.
graft			## Add a node to this node's children.
walk			## Recursively yield every node in this tree to a block
view			## Make a fancy string representation of the tree
				## as seen in the command-line tool

Redwood::Node

The Redwood::Node class is a simple implementation of the Redwood module. It is a good starting point for other trees. It adds new methods:

add_child(name)		## Add a child node. Nodes can have a #name.
[](name)			## Lookup children node by their #name.
<<(node)			## Alias for `graft`.

Redwood::FileNode

The Redwood::FileNode class is an example use-case for Redwood, and it powers the redwood CLI. It stores a directory tree in a Redwood-backed structure. It has one primary method that does the magic:

dir = Redwood::FileNode.scandir '~/Projects/Redwood'

That will go through the directory and build a Redwood tree. Redwood::FileNode objects have methods that correspond to the File class. So you can do things like dir.directory? or dir.chmod.

Now go forth and grow some Ruby-flavored trees.

Copyright

Redwood is Copyright (c) 2010 Mark Wunsch and is licensed under the MIT License.

redwood's People

Contributors

mwunsch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.