Sha256: 6426bea8f5f1ec83e376a1a07aa52afcdd9c5074df1289826f8d417f31247660

Contents?: true

Size: 1.8 KB

Versions: 52

Compression:

Stored size: 1.8 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)

ruby_version_is "1.9" do
  describe "String#to_r" do
    it "returns a Rational object" do
      String.new.to_r.should be_an_instance_of(Rational)
    end

    it "returns (0/1) for the empty String" do
      "".to_r.should == Rational(0, 1)
    end

    it "returns (n/1) for a String starting with a decimal _n_" do
      "2".to_r.should == Rational(2, 1)
      "1765".to_r.should == Rational(1765, 1)
    end

    it "ignores trailing characters" do
      "2 foo".to_r.should == Rational(2, 1)
      "1765, ".to_r.should == Rational(1765, 1)
    end

    it "ignores leading spaces" do
      " 2".to_r.should == Rational(2, 1)
      "  1765, ".to_r.should == Rational(1765, 1)
    end

    it "does not ignore arbitrary, non-numeric leading characters" do
      "The rational form of 33 is...".to_r.should_not == Rational(33, 1)
      "a1765, ".to_r.should_not == Rational(1765, 1)
    end

    it "treats leading hypens as minus signs" do
      "-20".to_r.should == Rational(-20, 1)
    end

    it "does not treat a leading period without a numeric prefix as a decimal point" do
      ".9".to_r.should_not == Rational(8106479329266893, 9007199254740992)
    end

    it "understands decimal points" do
      "3.33".to_r.should == Rational(333, 100)
      "-3.33".to_r.should == Rational(-333, 100)
    end

    it "ignores underscores between numbers" do
      "190_22".to_r.should == Rational(19022, 1)
      "-190_22.7".to_r.should == Rational(-190227, 10)
    end

    it "understands a forward slash as separating the numerator from the denominator" do
      "20/3".to_r.should == Rational(20, 3)
      " -19.10/3".to_r.should == Rational(-191, 30)
    end

    it "returns (0/1) for Strings it can't parse" do
      "glark".to_r.should == Rational(0,1)
    end
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/string/to_r_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/string/to_r_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/string/to_r_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/string/to_r_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/string/to_r_spec.rb