class-method

How to understand the difference between class_eval() and instance_eval()?

六月ゝ 毕业季﹏ 提交于 2019-12-17 15:08:08
问题 Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘instance_bar’ for #<Foo:0x7dce8> Just based on the name of the methods, I would expect class_eval to allow you to add a class method to Foo and instance_eval to allow you to add an instance

ActiveRecord Rails 3 scope vs class method

蓝咒 提交于 2019-12-17 10:24:15
问题 I'm new to the new query interface of ActiveRecord so I'm still figuring things out. I was hoping someone could explain the difference between using a scope in an ActiveRecord model and just using a class method (ie self.some_method ) From what I can gather, a scope is always expected to return a relation, whereas a class method doesn't necessarily have to. Is this true? For instance, I thought it would make sense to do something like: class Person scope :grouped_counts, group(:name).count

Class methods which create new instances

巧了我就是萌 提交于 2019-12-17 02:31:55
问题 Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"] According to widespread memory management guides (including Apple's), you're only responsible for releasing the objects that you alloc . Can anyone provide me with a template for such methods? How do you return the allocated object ( [self alloc]; return self; , perhaps)? How do you make sure that it will be released? 回答1: They are class

Can the staticmethod and classmethod decoraters be implemented in pure Python?

对着背影说爱祢 提交于 2019-12-14 03:43:31
问题 This is an academic question, more than a practical one. I'm trying to delve into the foundations of Python, and I wonder: could I implement the staticmethod and classmethod decorators in pure Python if I wanted to? For staticmethod maybe I could keep a reference to the original function and call it omitting the first argument? For classmethod maybe I could keep a reference to the original function and call it passing the type of the first argument, rather than the argument itself? (How would

Calling ruby method without instantiating class

折月煮酒 提交于 2019-12-13 13:20:21
问题 If I call a method on a rails active model method like so: class Foo < ActiveRecord::Base end Foo.first I'll get back the first active record. I don't have to instantiate the class. But if I create my own class and call a method, I get an exception: class Person < ActiveRecord::Base def greeting 'hello' end end Person.greeting #EXCEPTION: undefined method `greeting' for Person:Class How can I make that problem go away? 回答1: There are several kinds of methods. The two most important ones are:

Python - Metaclass decorator - How to use @classmethod

旧街凉风 提交于 2019-12-13 04:22:02
问题 I have the following Python metaclass that adds a deco_with_args decorator to each class: def deco_with_args(baz): def decorator(func): ... return func return decorator class Foo(type): def __prepare__(name, bases): return {'deco_with_args': deco_with_args} This allows me to use the decorator like this: class Bar(metaclass=Foo): @deco_with_args('baz') def some_function(self): ... How do I make the deco_with_args decorator behave like an @classmethod so that I can access the Bar class (or

How to access self in class method objective C

拥有回忆 提交于 2019-12-12 16:55:21
问题 I havea Utility class that uses class methods. I am trying to refer to self in the class method but can't. I was wondering how would I declare the following in a class method: [MRProgressOverlayView showOverlayAddedTo:self.window animated:YES]; self.window it says member reference type struct objc_class *' is a pointer; maybe you meant to use '->' Another problem that relates to not being able to call self is how would I refer to a declared @property in my .h in a class method in my .m . Here

Python classmethod outside the class

强颜欢笑 提交于 2019-12-11 14:37:17
问题 I have to use some Python library which contains file with various utilities: classes and methods. One of these methods is defined in the following way (I cannot put whole code): @classmethod def do_something(cls, args=None, **kwargs): but this declaration is outside any class. How can I get access to this method? Calling by do_something(myClass) gives an error: TypeError: 'classmethod' object is not callable . What is the purpose of creating classmethods outside classes? 回答1: The decorator

If I define a class-method in Ruby Object class, how do I get the name of a child class calling this method?

孤人 提交于 2019-12-11 12:24:36
问题 Example def Object.const_missing(name) puts self.class end class A; end A::B # => Class How can I get A in Object#const_missing ? 回答1: def Object.const_missing(name) puts self end 来源: https://stackoverflow.com/questions/3314108/if-i-define-a-class-method-in-ruby-object-class-how-do-i-get-the-name-of-a-chil

Overriding the __str__ method for @classmethods in python

我只是一个虾纸丫 提交于 2019-12-11 07:35:59
问题 I was trying to override __str__ and __repr__ for class as shown in the code below. The new methods are called whenever I call the instance_method but the object calls for class_method remains the same (Please see the code snippet and the output below for clarity). Is there a way possible to override __str__ and __repr__ for @classmethod so that the value of cls can be changed? I have also tried adding __str__ and __repr__ as @classmethod but nothing changed. class Abc: def __init__(self,