lib/pycall/pyobject_wrapper.rb in pycall-0.1.0.alpha.20170419 vs lib/pycall/pyobject_wrapper.rb in pycall-0.1.0.alpha.20170426
- old
+ new
@@ -54,10 +54,20 @@
@__pyobj__ = pyobj
end
attr_reader :__pyobj__
+ def eql?(other)
+ rich_compare(other, :==)
+ end
+
+ def hash
+ hash_value = LibPython.PyObject_Hash(__pyobj__)
+ return super if hash_value == -1
+ hash_value
+ end
+
def type
LibPython.PyObject_Type(__pyobj__).to_ruby
end
def null?
@@ -166,10 +176,18 @@
return res.to_ruby if LibPython.PyErr_Occurred().null?
raise PyError.fetch
end
def method_missing(name, *args, **kwargs)
- if PyCall.hasattr?(__pyobj__, name.to_s)
+ name_s = name.to_s
+ if name_s.end_with? '='
+ name = name_s[0..-2]
+ if PyCall.hasattr?(__pyobj__, name.to_s)
+ PyCall.setattr(__pyobj__, name, args.first)
+ else
+ raise NameError, "object has no attribute `#{name}'"
+ end
+ elsif PyCall.hasattr?(__pyobj__, name.to_s)
PyCall.getattr(__pyobj__, name)
else
super
end
end