README

Summary

This project, called ruby_token_parser, will parse strings containing ruby literals and return a proper ruby object.

This can be used to deconstruct a String into its constituent members as valid ruby objects, similar as to what IRB is doing when you let it evaluate the user input you supply it with.

Features

  • Recognizes about all Ruby literals:

nil, true, false, Symbols, Integers, Floats, Hashes, Arrays and also Ranges.

  • Additionally parses Constants, Dates and Times.

  • Simple to use through [] or .parse such as:

RubyTokenParser[]

Installation

gem install ruby_token_parser

Usage

Several examples follow next:

RubyTokenParser.parse("nil") # => nil
RubyTokenParser.parse(":foo") # => :foo
RubyTokenParser.parse("123") # => 123
RubyTokenParser.parse("1.5") # => 1.5
RubyTokenParser.parse("1.5", use_big_decimal: true) # => #<BigDecimal:…,'0.15E1',18(18)>
RubyTokenParser.parse("[1, 2, 3]") # => [1, 2, 3]
RubyTokenParser.parse("{:a => 1, :b => 2}") # => {:a => 1, :b => 2}
RubyTokenParser.parse("(1..15)")
RubyTokenParser["(1..15)"]

License

You can use this code under the BSD-2-Clause License, free of charge.

A link to the content of this license is provided here:

https://opensource.org/licenses/BSD-2-Clause

Note that the original version of this gem, called "literal_parser", was written by apeiros (Stefan Rusterholz) - you may be able to find his work here at:

https://github.com/apeiros
https://github.com/apeiros/literal_parser

If you need a different license in regards to the original literal_parser, please ask apeiros, not me, since it was his original code and work.

My (shevy) modifications were small, mostly just cosmetic, some renaming, the addition of Range-objects (since I needed that in one other project specifically), a bit more documentation, a general restructure of the code to make it easier for me to read and the addition of [] as a class method.

The BSD-2 license mandates that one has to retain the original copyright.

I assume that the original copyright is the following one here:

Copyright (c) 2012-2014, apeiros (Stefan Rusterholz)

The link to that license can be found at:

https://github.com/apeiros/literal_parser/blob/master/LICENSE.txt

Or respectively as part of apeiros' literal_parser project.

This LICENSE.txt is also part of the gem here and was included in the literal_parser project.

I will add the disclaimer as part of the license link to the readme here, as the original link at opensource.org also states something somewhat similar:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.