Skip to content

Ruby numeric , string class and javascript numeric , string classes

Shekhar Prasad Rajak edited this page Jun 29, 2017 · 4 revisions

Some research is needed to understand ruby and javascript classes for numeric , string, datatime. Since in google chart tool, we need to pass the type in the column of the table to generate the data table.

Examples :

    data_table = GoogleVisualr::DataTable.new
    data_table.new_column('string'  , 'Name')
    data_table.new_column('number'  , 'Salary')
    data_table.new_column('boolean' , 'Full Time Employee')
    # then rows will be added.

In ruby :

Numeric is a base class for other, more specific, types of number objects.
For example:


puts 100.class         # Fixnum
puts (100.2).class     # Float
puts (100**100).class  # Bignum

Base class for these will be Numeric

irb(main):026:0> (100).is_a? Numeric
=> true
irb(main):027:0> (100.2).is_a? Numeric
=> true
irb(main):028:0> (100*100).is_a? Numeric
=> true

Simple way to check boolean :

irb(main):041:0> b = true
=> true
irb(main):042:0> !!b == b
=> true
irb(main):043:0> b = 1
=> 1
irb(main):044:0> !!b == b
=> false
irb(main):045:0> b = 1.2
=> 1.2
irb(main):046:0> !!b == b
=> false
irb(main):047:0> b = 100*100
=> 10000
irb(main):048:0> !!b == b
=> false

To check string :

irb(main):029:0> ('100*100').is_a? String
=> true

So we can automatically pass the type in google chart table by checking these ruby types :

        js type = "string", when v.is_a?(String)
        js type = "number", when v.is_a?(Integer) || v.is_a?(Float) || v.is_a?(BigDecimal)
        js type = "boolean", when v.is_a?(TrueClass) || v.is_a?(FalseClass)
        js type = 'datetime', when v.is_a?(DateTime)  || v.is_a?(Time)
        js type = 'time', when v.is_a?(DateTime)  || v.is_a?(Time)
        js type == "date", when v.is_a?(Date)

Clone this wiki locally