Class: OraNumber

Inherits:
Numeric show all
Includes:
Comparable
Defined in:
lib/oci8/oci8.rb,
ext/oci8/ocinumber.c

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#to_onum

Class Method Details

._load(string) ⇒ Object

Unmarshal a dumped OraNumber object.



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'ext/oci8/ocinumber.c', line 1006

static VALUE
onum_s_load(VALUE klass, VALUE str)
{
    unsigned char *c;
    int size;
    OCINumber num;

    Check_Type(str, T_STRING);
    c = RSTRING_ORATEXT(str);
    size = RSTRING_LEN(str);
    if (size == 0 || size != c[0] + 1 || size > sizeof(num)) {
        rb_raise(rb_eTypeError, "marshaled OCI::Number format differ");
    }
    memset(&num, 0, sizeof(num));
    memcpy(&num, c, size);
    return oci8_make_ocinumber(&num);
}

Instance Method Details

#%(other) ⇒ Object

Returns the modulo after division of onum by other.



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'ext/oci8/ocinumber.c', line 631

static VALUE onum_mod(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_bin(lhs, rhs, '%');
    /* check whether argument is not zero. */
    oci_lc(OCINumberIsZero(oci8_errhp, &n, &is_zero));
    if (is_zero)
        rb_num_zerodiv();
    /* modulo */
    oci_lc(OCINumberMod(oci8_errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r);
}

#*(other) ⇒ Object

Returns a new OraNumber which is the product of onum and other.



587
588
589
590
591
592
593
594
595
596
597
598
# File 'ext/oci8/ocinumber.c', line 587

static VALUE onum_mul(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_bin(lhs, rhs, '*');
    /* multiply */
    oci_lc(OCINumberMul(oci8_errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r);
}

#**(other) ⇒ Object

Raises onum the other power.



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'ext/oci8/ocinumber.c', line 655

static VALUE onum_power(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;

    if (FIXNUM_P(rhs)) {
        oci_lc(OCINumberIntPower(oci8_errhp, _NUMBER(lhs), FIX2INT(rhs), &r));
    } else {
        /* change to OCINumber */
        if (!set_oci_number_from_num(&n, rhs, 0))
            return rb_num_coerce_bin(lhs, rhs, id_power);
        oci_lc(OCINumberPower(oci8_errhp, _NUMBER(lhs), &n, &r));
    }
    return oci8_make_ocinumber(&r);
}

#+(other) ⇒ Object

Returns a new OraNumber which is the sum of onum and other.



547
548
549
550
551
552
553
554
555
556
557
558
# File 'ext/oci8/ocinumber.c', line 547

static VALUE onum_add(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_bin(lhs, rhs, '+');
    /* add */
    oci_lc(OCINumberAdd(oci8_errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r);
}

#-(other) ⇒ Object

Returns a new OraNumber which is the difference of onum and other.



567
568
569
570
571
572
573
574
575
576
577
578
# File 'ext/oci8/ocinumber.c', line 567

static VALUE onum_sub(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_bin(lhs, rhs, '-');
    /* subtracting */
    oci_lc(OCINumberSub(oci8_errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r);
}

#- ⇒ Object

Returns an OraNumber, negated.



532
533
534
535
536
537
538
# File 'ext/oci8/ocinumber.c', line 532

static VALUE onum_neg(VALUE self)
{
    OCINumber r;

    oci_lc(OCINumberNeg(oci8_errhp, _NUMBER(self), &r));
    return oci8_make_ocinumber(&r);
}

#/(other) ⇒ Object

Returns a new OraNumber which is the result of dividing onum by other.



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'ext/oci8/ocinumber.c', line 607

static VALUE onum_div(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    OCINumber r;
    boolean is_zero;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_bin(lhs, rhs, '/');
    /* check whether argument is not zero. */
    oci_lc(OCINumberIsZero(oci8_errhp, &n, &is_zero));
    if (is_zero)
        rb_num_zerodiv();
    /* division */
    oci_lc(OCINumberDiv(oci8_errhp, _NUMBER(lhs), &n, &r));
    return oci8_make_ocinumber(&r);
}

#<=>(other) ⇒ -1, ...

Returns -1, 0, or +1 depending on whether onum is less than, equal to, or greater than other. This is the basis for the tests in Comparable.

Returns:

  • (-1, 0, +1)


679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'ext/oci8/ocinumber.c', line 679

static VALUE onum_cmp(VALUE lhs, VALUE rhs)
{
    OCINumber n;
    sword r;

    /* change to OCINumber */
    if (!set_oci_number_from_num(&n, rhs, 0))
        return rb_num_coerce_cmp(lhs, rhs, id_cmp);
    /* compare */
    oci_lc(OCINumberCmp(oci8_errhp, _NUMBER(lhs), &n, &r));
    if (r > 0) {
        return INT2FIX(1);
    } else if (r == 0) {
        return INT2FIX(0);
    } else {
        return INT2FIX(-1);
    }
}

#_dump ⇒ String

Dump onum for marshaling.

Returns:



990
991
992
993
994
995
996
997
998
# File 'ext/oci8/ocinumber.c', line 990

static VALUE onum_dump(int argc, VALUE *argv, VALUE self)
{
    char *c  = DATA_PTR(self);
    int size = c[0] + 1;
    VALUE dummy;

    rb_scan_args(argc, argv, "01", &dummy);
    return rb_str_new(c, size);
}

#abs ⇒ Object

Returns the absolute value of onum.



932
933
934
935
936
937
938
# File 'ext/oci8/ocinumber.c', line 932

static VALUE onum_abs(VALUE self)
{
    OCINumber result;

    oci_lc(OCINumberAbs(oci8_errhp, _NUMBER(self), &result));
    return oci8_make_ocinumber(&result);
}

#ceil ⇒ Integer

Returns the smallest Integer greater than or equal to onum.

Returns:

  • (Integer)


719
720
721
722
723
724
725
# File 'ext/oci8/ocinumber.c', line 719

static VALUE onum_ceil(VALUE self)
{
    OCINumber r;

    oci_lc(OCINumberCeil(oci8_errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r);
}

#floor ⇒ Integer

Returns the largest Integer less than or equal to onum.

Returns:

  • (Integer)


704
705
706
707
708
709
710
# File 'ext/oci8/ocinumber.c', line 704

static VALUE onum_floor(VALUE self)
{
    OCINumber r;

    oci_lc(OCINumberFloor(oci8_errhp, _NUMBER(self), &r));
    return oci8_make_integer(&r);
}

#round ⇒ Integer #round(decplace) ⇒ Object

Rounds onum to the nearest Integer when no argument. Rounds onum to a specified decimal place decplace when one argument.

OraNumber.new(1.234).round(1) #=> 1.2 OraNumber.new(1.234).round(2) #=> 1.23 OraNumber.new(1.234).round(3) #=> 1.234

Overloads:

  • #round ⇒ Integer

    Returns:

    • (Integer)


739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'ext/oci8/ocinumber.c', line 739

static VALUE onum_round(int argc, VALUE *argv, VALUE self)
{
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    oci_lc(OCINumberRound(oci8_errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    if (argc == 0) {
        return oci8_make_integer(&r);
    } else {
        return oci8_make_ocinumber(&r);
    }
}

#round_prec(digits) ⇒ Object

Rounds onum to a specified number of decimal digits. This method is available on Oracle 8.1 client or upper.

OraNumber.new(1.234).round_prec(2) #=> 1.2 OraNumber.new(12.34).round_prec(2) #=> 12 OraNumber.new(123.4).round_prec(2) #=> 120



782
783
784
785
786
787
788
# File 'ext/oci8/ocinumber.c', line 782

static VALUE onum_round_prec(VALUE self, VALUE ndigs)
{
    OCINumber r;

    oci_lc(OCINumberPrec(oci8_errhp, _NUMBER(self), NUM2INT(ndigs), &r));
    return oci8_make_ocinumber(&r);
}

#shift(fixnum) ⇒ Object

Returns onum * 10**fixnum This method is available on Oracle 8.1 client or upper.



947
948
949
950
951
952
953
# File 'ext/oci8/ocinumber.c', line 947

static VALUE onum_shift(VALUE self, VALUE exp)
{
    OCINumber result;

    oci_lc(OCINumberShift(oci8_errhp, _NUMBER(self), NUM2INT(exp), &result));
    return oci8_make_ocinumber(&result);
}

#to_char(fmt = nil, nls_params = nil) ⇒ String

Returns a string containing a representation of self. fmt and nls_params are same meanings with TO_CHAR of Oracle function.

Returns:



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# File 'ext/oci8/ocinumber.c', line 798

static VALUE onum_to_char(int argc, VALUE *argv, VALUE self)
{
    VALUE fmt;
    VALUE nls_params;
    char buf[512];
    ub4 buf_size = sizeof(buf);
    oratext *fmt_ptr;
    oratext *nls_params_ptr;
    ub4 fmt_len;
    ub4 nls_params_len;
    sword rv;

    rb_scan_args(argc, argv, "02", &fmt /* nil */, &nls_params /* nil */);
    if (NIL_P(fmt)) {
        OCINumber absval;
        sword sign;
        boolean is_int;

        oci_lc(OCINumberIsInt(oci8_errhp, _NUMBER(self), &is_int));
        if (is_int) {
            fmt_ptr = NUMBER_FORMAT_INT;
            fmt_len = NUMBER_FORMAT_INT_LEN;
        } else {
            oci_lc(OCINumberAbs(oci8_errhp, _NUMBER(self), &absval));
            oci_lc(OCINumberCmp(oci8_errhp, &absval, &const_shreshold, &sign));
            if (sign >= 0) {
                fmt_ptr = NUMBER_FORMAT2;
                fmt_len = NUMBER_FORMAT2_LEN;
            } else {
                fmt_ptr = NUMBER_FORMAT1;
                fmt_len = NUMBER_FORMAT1_LEN;
            }
        }
    } else {
        StringValue(fmt);
        fmt_ptr = RSTRING_ORATEXT(fmt);
        fmt_len = RSTRING_LEN(fmt);
    }
    if (NIL_P(nls_params)) {
        nls_params_ptr = NULL;
        nls_params_len = 0;
    } else {
        StringValue(nls_params);
        nls_params_ptr = RSTRING_ORATEXT(nls_params);
        nls_params_len = RSTRING_LEN(nls_params);
    }
    rv = OCINumberToText(oci8_errhp, _NUMBER(self),
                         fmt_ptr, fmt_len, nls_params_ptr, nls_params_len,
                         &buf_size, TO_ORATEXT(buf));
    if (rv == OCI_ERROR) {
        sb4 errcode;
        OCIErrorGet(oci8_errhp, 1, NULL, &errcode, NULL, 0, OCI_HTYPE_ERROR);
        if (errcode == 22065) {
            /* OCI-22065: number to text translation for the given format causes overflow */
            if (NIL_P(fmt)) /* implicit conversion */
                return rb_usascii_str_new_cstr("overflow");
        }
        oci8_raise(oci8_errhp, rv, NULL);
    }
    return rb_usascii_str_new(buf, buf_size);
}

#to_f ⇒ Float

Converts onum to a Float.

Returns:

  • (Float)


892
893
894
895
896
897
898
# File 'ext/oci8/ocinumber.c', line 892

static VALUE onum_to_f(VALUE self)
{
    double dbl;

    oci_lc(OCINumberToReal(oci8_errhp, _NUMBER(self), sizeof(dbl), &dbl));
    return rb_float_new(dbl);
}

#to_i ⇒ Integer

Returns onm truncated to an Integer.

Returns:

  • (Integer)


877
878
879
880
881
882
883
# File 'ext/oci8/ocinumber.c', line 877

static VALUE onum_to_i(VALUE self)
{
    OCINumber num;

    oci_lc(OCINumberTrunc(oci8_errhp, _NUMBER(self), 0, &num));
    return oci8_make_integer(&num);
}

#to_json(options = nil) ⇒ Object

:nodoc:



642
643
644
# File 'lib/oci8/oci8.rb', line 642

def to_json(options=nil) # :nodoc:
  to_s
end

#to_s ⇒ String

Returns a string containing a representation of self.

Returns:



866
867
868
869
# File 'ext/oci8/ocinumber.c', line 866

static VALUE onum_to_s(VALUE self)
{
    return onum_to_char(0, NULL, self);
}

#to_yaml(opts = {}) ⇒ Object

:nodoc:



636
637
638
639
640
# File 'lib/oci8/oci8.rb', line 636

def to_yaml(opts = {}) # :nodoc:
  YAML.quick_emit(object_id, opts) do |out|
    out.scalar(taguri, self.to_s, :plain)
  end
end

#truncate ⇒ Integer #truncate(decplace) ⇒ Object

Truncates onum to the Integer when no argument. Truncates onum to a specified decimal place decplace when one argument.

Overloads:

  • #truncate ⇒ Integer

    Returns:

    • (Integer)


761
762
763
764
765
766
767
768
769
# File 'ext/oci8/ocinumber.c', line 761

static VALUE onum_trunc(int argc, VALUE *argv, VALUE self)
{
    VALUE decplace;
    OCINumber r;

    rb_scan_args(argc, argv, "01", &decplace /* 0 */);
    oci_lc(OCINumberTrunc(oci8_errhp, _NUMBER(self), NIL_P(decplace) ? 0 : NUM2INT(decplace), &r));
    return oci8_make_ocinumber(&r);
}

#yaml_initialize(type, val) ⇒ Object

:nodoc:



632
633
634
# File 'lib/oci8/oci8.rb', line 632

def yaml_initialize(type, val) # :nodoc:
  initialize(val)
end

#zero? ⇒ Boolean

Returns true if onum is zero.

Returns:

  • (Boolean)


917
918
919
920
921
922
923
# File 'ext/oci8/ocinumber.c', line 917

static VALUE onum_zero_p(VALUE self)
{
    boolean result;

    oci_lc(OCINumberIsZero(oci8_errhp, _NUMBER(self), &result));
    return result ? Qtrue : Qfalse;
}