Class: NumRu

Inherits:
Object
  • Object
show all
Defined in:
lib/numru.rb

Constant Summary collapse

@@np =
RubyPython.import 'numpy'
@@blt =
RubyPython::PyMainClass.send(:new)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(np_obj) ⇒ NumRu

Returns a new instance of NumRu.



13
14
15
# File 'lib/numru.rb', line 13

def initialize np_obj
  @np_obj = np_obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



86
87
88
89
90
91
# File 'lib/numru.rb', line 86

def method_missing(m, *args)
  args.map! { |i| NumRu.preprocess_arg i }
  m = "#{m}!" if args.any? { |i| i.respond_to?(:class) && i.class == Hash }
  obj = @np_obj.__send__ m, *args
  NumRu.return_or_wrap obj
end

Instance Attribute Details

#np_objObject

Returns the value of attribute np_obj.



11
12
13
# File 'lib/numru.rb', line 11

def np_obj
  @np_obj
end

Class Method Details

.arg_to_s(arg) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/numru.rb', line 76

def self.arg_to_s arg
  case arg
  when Hash
    return arg.map { |k, v| "#{k}=#{v.inspect}" }
  when NumRu
    return "ObjectSpace._id2ref(#{arg.object_id}).np_obj"
  end
  arg.to_s
end

.method_missing(m, *args) ⇒ Object



93
94
95
96
97
98
# File 'lib/numru.rb', line 93

def self.method_missing(m, *args)
  args.map! { |i| NumRu.preprocess_arg i }
  m = "#{m}!" if args.any? { |i| i.respond_to?(:class) && i.class == Hash }
  obj = @@np.__send__ m, *args
  return_or_wrap obj
end

.preprocess_arg(obj) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/numru.rb', line 61

def self.preprocess_arg obj
  case obj
  when NumRu
    obj.np_obj
  when Range
    @@blt.range(obj.begin, obj.end+1)
  else
    obj
  end
end

.return_or_wrap(obj) ⇒ Object



17
18
19
20
21
# File 'lib/numru.rb', line 17

def self.return_or_wrap obj
  obj.rubify
rescue
  NumRu.new obj
end

Instance Method Details

#==(arg) ⇒ Object



72
73
74
# File 'lib/numru.rb', line 72

def ==(arg)
  @np_obj.__send__('==', NumRu.preprocess_arg(arg))
end

#[](*args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/numru.rb', line 23

def [](*args)
  args.map! do |i|
    case i
    when Range
      last = i.end == -1 ? nil : i.end + 1
      @@blt.slice(i.begin, last)
    when NumRu
      i.np_obj
    when String
      a, b, c = i.split(':').map { |x| x == '' ? nil : x.to_i }
      @@blt.slice(a, b, c)
    else
      i
    end
  end
  obj = @np_obj.__send__('__getitem__', args)
  NumRu.return_or_wrap obj
end

#[]=(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/numru.rb', line 42

def []=(*args)
  args.map! do |i|
    case i
    when Range
      last = i.end == -1 ? nil : i.end + 1
      @@blt.slice(i.begin, last)
    when NumRu
      i.np_obj
    when String
      a, b, c = i.split(':').map { |x| x == '' ? nil : x.to_i }
      @@blt.slice(a, b, c)
    else
      i
    end
  end
  obj = @np_obj.__send__('__setitem__', args[0..-2], args[-1])
  NumRu.return_or_wrap obj
end

#inspectObject



104
105
106
# File 'lib/numru.rb', line 104

def inspect
  @np_obj.__repr__
end

#to_sObject



100
101
102
# File 'lib/numru.rb', line 100

def to_s
  @np_obj.__str__
end