Questions tagged [instance-method]
30 questions
                    
                    66
                    
            votes
                
                5 answers
            
        How to understand the difference between class_eval() and instance_eval()?
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   #=>…
         
    
    
        pez_dispenser
        
- 4,394
- 7
- 37
- 47
                    59
                    
            votes
                
                3 answers
            
        How can I use functools.singledispatch with instance methods?
Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation:
from functools import singledispatch
class TestClass(object):
    @singledispatch
    def test_method(arg,…
         
    
    
        Dustin Oprea
        
- 9,673
- 13
- 65
- 105
                    34
                    
            votes
                
                2 answers
            
        Adding attributes to instancemethods in Python
I bumped into this behaviour when trying to get class-decorators and method-decorators to play nicely together. Essentially, the method decorators would flag some of the methods as special with some dummy value, and the class decorator would come by…
         
    
    
        Li Haoyi
        
- 15,330
- 17
- 80
- 137
                    14
                    
            votes
                
                8 answers
            
        Delegating instance methods to the class method
In Ruby, suppose I have a class Foo to allow me to catalogue my large collection of Foos. It's a fundamental law of nature that all Foos are green and spherical, so I have defined class methods as follows:
class Foo
  def self.colour
    "green"
 …
         
    
    
        Chowlett
        
- 45,935
- 20
- 116
- 150
                    8
                    
            votes
                
                3 answers
            
        Overcoming Python's limitations regarding instance methods
It seems that Python has some limitations regarding instance methods.
Instance methods can't be copied.
Instance methods can't be pickled.
This is problematic for me, because I work on a very object-oriented project in which I reference instance…
         
    
    
        Ram Rachum
        
- 84,019
- 84
- 236
- 374
                    6
                    
            votes
                
                2 answers
            
        Why does instance_eval() define a class method when called on a class?
Foo = Class.new
Foo.instance_eval do
  def instance_bar
    "instance_bar"
  end
end
puts Foo.instance_bar       #=> "instance_bar"
puts Foo.new.instance_bar   #=> undefined method ‘instance_bar’
My understanding is that calling instance_eval on an…
         
    
    
        pez_dispenser
        
- 4,394
- 7
- 37
- 47
                    3
                    
            votes
                
                4 answers
            
        Objective-C class method does not call delegate methods while instance method does
I have the following 2 methods:
-(void)authenticateUserToGoogle:(NSString *)userName withPassword:(NSString *)password {
    NSString *URLstr = GOOGLE_CLIENT_LOGIN;
    URLstr = @"http://www.google.com/ig/api?stock=AAPL";
    NSURL *theURL =…
         
    
    
        ennuikiller
        
- 46,381
- 14
- 112
- 137
                    3
                    
            votes
                
                3 answers
            
        where is the instancemethod decorator?
In my code I have a method that returns an instance of a class, like this:
class MyClass:
  def fun( self, *args ): # the method
    return Props( self, *args )
class Props: # the returned object
  def __init__( self, parent, *args ):
   …
         
    
    
        gertjan
        
- 843
- 1
- 8
- 16
                    2
                    
            votes
                
                3 answers
            
        How to use Rails instance public method performed? in if-else
How does one use the Ruby on Rails method performed? in an if else statement?
I tried to use this StackOverflow anwser in my example below:
if condition
  does something
elsif redirect_to(records_path)
  performed?
  does another thing
else
  yet…
        user9673474
                    2
                    
            votes
                
                1 answer
            
        Perl map using object method
I have a Perl Module file MyClass.pm with a very basic class definition.
use strict;
use warnings;
package MyClass;
sub new {
    my $this = shift;
    my $self = {};
    bless $self, $this;
    return $self;
}
sub displayChar{
    my $self =…
         
    
    
        nanocv
        
- 2,227
- 2
- 14
- 27
                    2
                    
            votes
                
                1 answer
            
        Django: How to do reverse foreign key lookup of another class without an instance of this class?
I have the following two Django Classes MyClassA and MyClassB.
MyClassB has a foreign key reference to an instance of MyClassA.
from django.db import models
class MyClassA(models.Model):
    name = models.CharField(max_length=50, null=False)
   …
         
    
    
        Saqib Ali
        
- 11,931
- 41
- 133
- 272
                    2
                    
            votes
                
                1 answer
            
        Root node operations in tree data structures
Need a way to indicate/enforce that certain methods can only be done on the root Node of my tree data structure.  I'm working in Python 2.x
I have a class, Node, which I use in conjunction with another class, Edge, to build a tree data structure…
         
    
    
        Clay Wardell
        
- 14,846
- 13
- 44
- 65
                    1
                    
            vote
                
                4 answers
            
        Python when to use instance vs static methods
I am struggling to understand when it makes sense to use an instance method versus a static method. Also, I don't know if my functions are static since there is not a @staticmethod decorator. Would I be able to access the class functions when I make…
         
    
    
        Daniel Butler
        
- 3,239
- 2
- 24
- 37
                    1
                    
            vote
                
                1 answer
            
        Overriding Instance Method in Rails Library
This seems like a monkey patch.  How can I improve it?
Trying to override the deliver_now instance method from the ActionMailer::MessageDelivery class.  The below code works.  
However, is there a way I can achieve this more elegantly say through…
         
    
    
        csi
        
- 9,018
- 8
- 61
- 81
                    1
                    
            vote
                
                3 answers
            
        Calling inherited class method from instance method in Ruby
I've got the following Ruby code:
class B
  class << self
    protected
    def prot
      puts "victory"
    end
  end
end
class C < B
  def self.met
    C.prot
  end
end
C.met
which tries to proof that protected class methods are inherited in…
         
    
    
        user1868607
        
- 2,558
- 1
- 17
- 38