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.



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

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



59
60
61
62
63
# File 'lib/numru.rb', line 59

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

Instance Attribute Details

#np_objObject

Returns the value of attribute np_obj.



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

def np_obj
  @np_obj
end

Class Method Details

.arg_to_s(arg) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/numru.rb', line 49

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



65
66
67
68
69
# File 'lib/numru.rb', line 65

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

.return_or_wrap(obj) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/numru.rb', line 23

def self.return_or_wrap obj
  # if obj.respond_to? :class
  #   obj
  # elsif obj.__class__.__name__ == 'ndarray'
    NumRu.new obj
  # else
  #   obj
  # end
end

Instance Method Details

#[](*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/numru.rb', line 33

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
    else
      i
    end
  end
  obj = @np_obj.__send__('__getitem__', args)
  NumRu.return_or_wrap obj
end

#inspectObject



75
76
77
# File 'lib/numru.rb', line 75

def inspect
  @np_obj.__repr__
end

#to_sObject



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

def to_s
  @np_obj.__str__
end