class-method

How to dynamically define a class method which will refer to a local variable outside?

一个人想着一个人 提交于 2019-12-31 12:59:34
问题 class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end end I know, this is not correct, because the def created a new scope. I also know that use define_method can create a instance method without creating a new scope, but my point is how to define a class method . 回答1: Class methods don't really exist in Ruby, they are just singleton methods of the class object. Singleton methods don't really exist, either, they are just ordinary instance methods of

Is Class declaration an eyewash in ruby? Is everything really object oriented?

喜夏-厌秋 提交于 2019-12-31 03:11:29
问题 class Person def name puts "Dave" end end puts Person.object_id There are only two ways of accessing methods : 1) Someclass.method in case of class methods. #where Someclass is a class. 2) and Object.method when the method being accessed is a regular method declared inside a class. and Object is an instance of a class. It follows the pattern Object.method so, does it mean Person class is really an object? or object_id is a class method? The latter seems unlikely because class methods cannot

Is it bad form to call a classmethod as a method from an instance?

孤者浪人 提交于 2019-12-30 05:32:08
问题 Ex. If I have something like this: class C(object): @classmethod def f(cls, x): return x + x This will work: c = C() c.f(2) 4 But is that bad form? Should I only call C.f() or c.__class__.f() Obviously, this would only make sense in cases where f doesn't interact with self/cls expecting it to be class. ? 回答1: If you are tempted to call a class method from an instance you probably don't need a class method. In the example you gave a static method would be more appropriate precisely because of

Find name of dynamic method in Python

纵然是瞬间 提交于 2019-12-25 05:59:25
问题 I want to proxy an API over a network. I have the API in a dictionary. I'd like to create a class with the API methods from the dictionary so I can use the API as if I was local. The trouble is finding the name of my dynamically created method. (My approach is based on Adding a Method to an Existing Object and Python dynamic class methods.) class MainClass(object): def build_API(self): methods = dict(meth1='arg1', meth2='arg2') for key in methods.iterkeys(): setattr(self, key, MethodType(self

Find name of dynamic method in Python

半腔热情 提交于 2019-12-25 05:58:05
问题 I want to proxy an API over a network. I have the API in a dictionary. I'd like to create a class with the API methods from the dictionary so I can use the API as if I was local. The trouble is finding the name of my dynamically created method. (My approach is based on Adding a Method to an Existing Object and Python dynamic class methods.) class MainClass(object): def build_API(self): methods = dict(meth1='arg1', meth2='arg2') for key in methods.iterkeys(): setattr(self, key, MethodType(self

In what scenario 'this' pointer is passed to the class methods? [duplicate]

允我心安 提交于 2019-12-23 22:37:45
问题 This question already has answers here : Does every c++ member function take `this` as an input implicitly? (4 answers) Closed 2 years ago . I was doing some reading on the 'this' pointer, and I think I understand it more than I originally did, but I still need some clarification. So, by my understanding, if you have class Simple { private: int m_nID; public: Simple(int nID) { SetID(nID); } void SetID(int nID) { m_nID = nID; } int GetID() { return m_nID; } }; The SetID(int nID) function

Access property from a class method?

给你一囗甜甜゛ 提交于 2019-12-22 10:55:42
问题 In order to make my code testable, I have created a lazy initializer; this way in my unit test, I can mock any object I want before the getter gets called. When it comes to class methods, though, my class method doesn't have access to the properties I have defined. Is there any way to make the properties accessible by my class method? If not, is there any way to create static variables that are also accessible outside of this class, i.e., accessible by my unit test class? @implementation

Is there a way to call a private Class method from an instance in Ruby?

青春壹個敷衍的年華 提交于 2019-12-22 01:44:56
问题 Other than self.class.send :method, args... , of course. I'd like to make a rather complex method available at both the class and instance level without duplicating the code. UPDATE: @Jonathan Branam: that was my assumption, but I wanted to make sure nobody else had found a way around. Visibility in Ruby is very different from that in Java. You're also quite right that private doesn't work on class methods, though this will declare a private class method: class Foo class <<self private def

(In Ruby) allowing mixed-in class methods access to class constants

无人久伴 提交于 2019-12-21 07:31:10
问题 I have a class with a constant defined for it. I then have a class method defined that accesses that class constant. This works fine. An example: #! /usr/bin/env ruby class NonInstantiableClass Const = "hello, world!" class << self def shout_my_constant puts Const.upcase end end end NonInstantiableClass.shout_my_constant My problem arises in attempting to move this class method out to an external module, like so: #! /usr/bin/env ruby module CommonMethods def shout_my_constant puts Const

(In Ruby) allowing mixed-in class methods access to class constants

不羁的心 提交于 2019-12-21 07:31:10
问题 I have a class with a constant defined for it. I then have a class method defined that accesses that class constant. This works fine. An example: #! /usr/bin/env ruby class NonInstantiableClass Const = "hello, world!" class << self def shout_my_constant puts Const.upcase end end end NonInstantiableClass.shout_my_constant My problem arises in attempting to move this class method out to an external module, like so: #! /usr/bin/env ruby module CommonMethods def shout_my_constant puts Const