instance-methods

How can you call class methods on mailers when they're not defined as such?

寵の児 提交于 2019-12-10 02:52:06
问题 When sending mail in Rails, usually one would do something like this: UserMailer.password_reset(user).deliver But if we look inside UserMailer we can see this: def password_reset(user) # not self.password_reset # ... end Notice that the method name is not prefixed with self . Looking at it, it seems like you need to instantiate the object first as below. How does Rails do this? UserMailer.new.password_reset(user).deliver 回答1: That's a great question. In the source (https://github.com/rails

Ruby Unbound Methods: Is it possible to force bind to instances of other classes?

江枫思渺然 提交于 2019-12-10 02:02:24
问题 I would like to know if I could force this to happen class A def bomb ; "bomb" ; end end class B ; end bomb = A.instance_method(:bomb) b = B.new bomb.bind(b) currently it throws the error TypeError: bind argument must be an instance of A I find this very limiting concerning what I can do with these unbound methods, the possibilities are a bit limiting. In cases like these (and I'm not referring only to idempotent functions) it would make sense right? And an execution error would have sufficed

Python decorator on instance method

妖精的绣舞 提交于 2019-12-06 08:49:12
问题 Anyone know what is wrong with this code? def paginated_instance_method(default_page_size=25): def wrap(func): @functools.wraps(func) def inner(self, page=1, page_size=default_page_size, *args, **kwargs): objects = func(self=self, *args, **kwargs) return _paginate(objects, page, page_size) return inner return wrap class Event(object): ... @paginated_instance_method def get_attending_users(self, *args, **kwargs): return User.objects.filter(pk__in=self.attending_list) I get the following error:

How can you call class methods on mailers when they're not defined as such?

怎甘沉沦 提交于 2019-12-05 02:08:25
When sending mail in Rails, usually one would do something like this: UserMailer.password_reset(user).deliver But if we look inside UserMailer we can see this: def password_reset(user) # not self.password_reset # ... end Notice that the method name is not prefixed with self . Looking at it, it seems like you need to instantiate the object first as below. How does Rails do this? UserMailer.new.password_reset(user).deliver That's a great question. In the source ( https://github.com/rails/rails/blob/master/actionmailer/lib/action_mailer/base.rb ), Rails uses method_missing to create a new

Ruby Unbound Methods: Is it possible to force bind to instances of other classes?

帅比萌擦擦* 提交于 2019-12-05 01:36:47
I would like to know if I could force this to happen class A def bomb ; "bomb" ; end end class B ; end bomb = A.instance_method(:bomb) b = B.new bomb.bind(b) currently it throws the error TypeError: bind argument must be an instance of A I find this very limiting concerning what I can do with these unbound methods, the possibilities are a bit limiting. In cases like these (and I'm not referring only to idempotent functions) it would make sense right? And an execution error would have sufficed, In case I would be handling variables from A which are not replicated in B. I'd really like to know

Can not make static reference to non static method? [duplicate]

廉价感情. 提交于 2019-12-04 22:23:20
This question already has an answer here: Static Classes In Java 14 answers So, in short. I have two classes. package rpg; public class Engine { public void main(String args[]) { Start.gameStart(); System.out.println(menuResult); } } and package rpg; public class Start { int menuResult = 3; public int gameStart() { return menuResult; } public int getMenuResult() { return Start.menuResult; } } It keeps throwing up the error 'Cannot make static reference to non-static method gameStart()'. I'm sure I'm missing something simple, but can't find it. Thanks! You need to create instance of Start class

Can a method be used as either a staticmethod or instance method?

女生的网名这么多〃 提交于 2019-12-04 03:38:03
问题 I'd like to be able to do this: class A(object): @staticandinstancemethod def B(self=None, x, y): print self is None and "static" or "instance" A.B(1,2) A().B(1,2) This seems like a problem that should have a simple solution, but I can't think of or find one. 回答1: It is possible, but please don't. I couldn't help but implement it though: class staticandinstancemethod(object): def __init__(self, f): self.f = f def __get__(self, obj, klass=None): def newfunc(*args, **kw): return self.f(obj,

Call static method from instance in PHP, future deprecation?

情到浓时终转凉″ 提交于 2019-12-03 22:21:45
While I understand the $this variable is not available when a method is called in a static context, to assist in decoupling my application components from one-another I figured it would make sense to call static methods from an instance. For example: class MyExample{ private static $_data = array(); public static function setData($key, $value){ self::$_data[$key] = $value; } // other non-static methods, using self::$_data } // to decouple, another class or something has been passed an instance of MyExample // rather than calling MyExample::setData() explicitly // however, this data is now

Static variables in instance methods

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 11:38:46
Let's say I have this program: class Foo { public: unsigned int bar () { static unsigned int counter = 0; return counter++; } }; int main () { Foo a; Foo b; } (Of course this example makes no sense since I'd obviously declare "counter" as a private attribute, but it's just to illustrate the problem). I'd like to know how C++ behaves in this kind of situation: will the variable "counter" in the bar() method be the same for every instance? Yes, counter will be shared across all instances of objects of type Foo in your executable. As long as you're in a singlethreaded environment, it'll work as

Can a method be used as either a staticmethod or instance method?

柔情痞子 提交于 2019-12-01 18:03:40
I'd like to be able to do this: class A(object): @staticandinstancemethod def B(self=None, x, y): print self is None and "static" or "instance" A.B(1,2) A().B(1,2) This seems like a problem that should have a simple solution, but I can't think of or find one. It is possible, but please don't. I couldn't help but implement it though: class staticandinstancemethod(object): def __init__(self, f): self.f = f def __get__(self, obj, klass=None): def newfunc(*args, **kw): return self.f(obj, *args, **kw) return newfunc ...and its use: >>> class A(object): ... @staticandinstancemethod ... def B(self, x