Skip to content

Instantly share code, notes, and snippets.

@paddor
Created September 29, 2008 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddor/13665 to your computer and use it in GitHub Desktop.
Save paddor/13665 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# File : to_num.rb
# Author : Patrik Wenger <paddor@gmail.com>
# Date : 26th September 2008
# Version : 0.2
#
# It would be nice if there were fancy strings like %i, %f and %n for integers,
# floats and numbers, respectively. This library attempts to solve this problem.
#
# Use String#to_num to convert a number string to an Integer or Float object.
# If the string contains more than one number (separated by whitespaces),
# an Array of Integer and Float objects is returned.
#
# Kernel#num can be used to achieve the same effect. Just pass it the string.
#
# Numeric#to_num returns itself.
class String
def to_num
if self[/\s/]
strip.split(/\s+/).map {|n| n.to_num}
else
self["."] ? to_f : to_i
end
end
end
module Kernel
def num *input
return nil if input.empty?
if input.size > 1
input.map {|n| n.to_num}
else
input.first.to_num
end
end
end
class Numeric
def to_num; self end
end
if $0 == __FILE__
require 'rubygems'
require 'test/spec'
# Have to use #be (same object ID) for Integers because 4 == 4.0.
# Have to use #== (equal) for Floats because 4.0.object_id != 4.0.object_id.
describe "String" do
it "should convert a single integer to an integer" do
"15" .to_num.should.be 15
"501".to_num.should.be 501
"-17".to_num.should.be -17
end
it "should convert a single float to a float" do
"3.141".to_num.should == 3.141
"-6.3" .to_num.should == -6.3
"100.7".to_num.should == 100.7
end
it "should convert integer list strings to integers" do
n = "3 -5 22".to_num
n.class.should.be Array
n[0].should.be 3
n[1].should.be -5
n[2].should.be 22
end
it "should convert float list strings to floats" do
n = "3.3 4.0 -0.0 0.0".to_num
n.class.should.be Array
n[0].should == 3.3
n[1].should == 4.0
n[2].should == -0.0
end
it "should convert mixed list strings to integers and floats" do
n = "45.12 300 -1 418.3496 1".to_num
n.class.should.be Array
n[0].should == 45.12
n[1].should.be 300
n[2].should.be -1
n[3].should == 418.3496
n[4].should.be 1
end
end
describe "Kernel#num" do
it "should handle single integers" do
num(5).should.be 5
num("666").should.be 666
num("-555").should.be -555
end
it "should handle single floats" do
num(3.1412).should == 3.1412
num("45.45").should == 45.45
num("-32.32").should == -32.32
end
it "should handle integer lists" do
n = num "12", "-200", -6
n.class.should.be Array
n[0].should.be 12
n[1].should.be -200
n[2].should.be -6
n = num *%w[687 23 -70]
n.class.should.be Array
n[0].should.be 687
n[1].should.be 23
n[2].should.be -70
end
it "should handle float lists" do
n = num "0.3", "-14.7", 3.4, -12.0
n.class.should.be Array
n[0].should == 0.3
n[1].should == -14.7
n[2].should == 3.4
n[3].should == -12.0
n = num *%w[0.55 -5.2 1.2 -52.0]
n.class.should.be Array
n[0].should == 0.55
n[1].should == -5.2
n[2].should == 1.2
n[3].should == -52.0
end
it "should handle list strings" do
n = num "3.141 440 -2 305.96 3"
n.class.should.be Array
n[0].should == 3.141
n[1].should.be 440
n[2].should.be -2
n[3].should == 305.96
n[4].should.be 3
end
end
describe "Numeric" do
it "should return itself" do
88.to_num.should.be 88
4.3.to_num.should == 4.3
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment