Module: Tb::Func

Defined in:
lib/tb/func.rb

Overview

Copyright © 2012 Tanaka Akira <[email protected]>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.
3. The name of the author may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “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 AUTHOR 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.

Defined Under Namespace

Modules: Avg, Count, First, Last, Max, Min, Sum, UniqueValues, Values Classes: FirstN, LastN

Constant Summary collapse

AggregationFunctions =
{}

Class Method Summary collapse

Class Method Details

.smart_cmp_value(v) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tb/func.rb', line 30

def self.smart_cmp_value(v)
  case v
  when nil
    []
  when Numeric
    [0, v]
  when String
    if v.respond_to? :force_encoding
      v = v.dup.force_encoding("ASCII-8BIT")
    end
    case v
    when /\A\s*-?\d+\s*\z/
      [0, v.to_i(10)]
    when /\A\s*-?(\d+(\.\d+)?)([eE][-+]?\d+)?\s*\z/
      [0, Float(v)]
    else
      a = []
      v.scan(/(\d+)|\D+/) {
        if $1
          a << 0 << $1.to_i
        else
          a << 1 << $&
        end
      }
      a
    end
  else
    raise ArgumentError, "unexpected: #{v.inspect}"
  end
end

.smart_numerize(v) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tb/func.rb', line 61

def self.smart_numerize(v)
  return v if v.kind_of? Numeric
  v = v.strip
  if /\A-?\d+\z/ =~ v
    v = v.to_i
  elsif /\A-?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?\z/ =~ v
    v = v.to_f
  else
    raise ArgumentError, "number string expected: #{v.inspect}"
  end
  v
end