Questions tagged [instance-methods]

The tag has no usage guidance.

instance-methods
Filter by
Sorted by
Tagged with
5 votes
2 answers
1k views

Calling instance method using class definition in Python

Lately, I've been studying Python's class instantiation process to really understand what happen under the hood when creating a class instance. But, while playing around with test code, I came across ...
scharette's user avatar
  • 9,787
0 votes
1 answer
120 views

manipulate class instance

I want to give the second instance of my Player class the remaining marker after the first instance gets one of them. class Player(): available_markers = ['X', 'O'] num_of_players = 0 ...
jakuta's user avatar
  • 130
2 votes
3 answers
2k views

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(...
user avatar
0 votes
2 answers
1k views

Python: Why does this AttributeError in __str__ method disappear after this indentation change [duplicate]

I hope you will be able to replicate this error on your machine. I couldn't figure out why exactly the code below gave me an error [pasted thereafter] when I had the __str__ method aligned with the ...
heretoinfinity's user avatar
1 vote
4 answers
71 views

Can constructor in Java inherited classes be empty

I've got 2 classes (Date and Student) in the same project. I need to test the Student class but am unable to call an instance method in a static method. I tried to create a reference to the instance ...
user avatar
4 votes
1 answer
112 views

Why do Ruby instance method invocations behave differently when prefixed with 'self'?

The two invocations of an instance method carry different semantics. The first call to bar works as expected. class T def foo bar # <= This works. "bar" is printed. self.bar # <= ...
Sniggerfardimungus's user avatar
0 votes
1 answer
109 views

Python Class: function or instance method

I'm learning Python with a textbook called Introduction to Computation and Programming Using Python, in Chapter 8 there is an example code: class IntSet(object): """An intSet is a set of integers"...
Madmint's user avatar
  • 5,994
0 votes
1 answer
568 views

Function callable as class method or instance method [duplicate]

I have a Python class that I created previously, it looks something like this: class Foo: @classmethod def bar(cls, x): print(x + 3) Foo.bar(7) # prints '10' Now I would like to retrofit ...
Ken Williams's user avatar
  • 23.4k
1 vote
4 answers
117 views

Illogical: circle of logic, in ruby using 'super'

I keep trying to reason upon the functionality of a method within a subclass that inherits functionality of a parents class. But it seems that I keep getting into a mental loop of: one cannot behave ...
Ghoyos's user avatar
  • 614
1 vote
4 answers
2k views

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 ...
Daniel Butler's user avatar
0 votes
1 answer
96 views

Swift instance vs class methods plus class inheritance

I have question. Coming from ruby I'm used to a syntax where I can do the following. client = NewsAPI::Client.new( url: 'https://www.newshost.com/api/', http_token: '1234567890', ) ...
Hannes_hal's user avatar
1 vote
1 answer
1k views

Taking a key from a hash, and interpolate that as a getter on an object

I have a class that creates objects. I've created getter/setter methods for that class. Class Test attr_accessor :method1, :method2, :method3, :method4 I have created several instances from that ...
Wings's user avatar
  • 1,006
0 votes
4 answers
5k views

What is an "instance method"?

From 3. Data model: Instance methods An instance method object combines a class, a class instance and any callable object (normally a user-defined function). If it is a definition, what does it mean? ...
Tim's user avatar
  • 97.1k
2 votes
1 answer
69 views

Update hidden attribute in LoopbackJS

I have a strange problem in LoopbackJS. In one of my persisted models which are saved in PostgreSQL, I am not able to change the value of some fields of an instance with .save() or .updateAttributes ...
Maryam Saeidi's user avatar
0 votes
2 answers
206 views

Questions about instance methods in ruby.

I have an assignment to build a tic-tac-toe game using ruby classes. I'm new to programming and self taught so far, but was accepted into a program and need to polish up on some concepts to be ahead ...
hancho's user avatar
  • 1,407
0 votes
1 answer
1k views

Sequelize: 'findAll' in instance method getParticipants()?

Specification for my conference instance method: getParticipants() : Promise -> Participant array Conference model: return sequelize.define('conference', { id: { type: Sequelize.UUID,...
mrks's user avatar
  • 5,531
-1 votes
2 answers
634 views

Ruby: What is "Self" inside "If" Statement of Instance Method

What does "self" refer to when it's inside an "if" statement (or is it called block?) that is inside an instance method? I'm referring to my code in the last method: "apply_discount" I thought it'd ...
Edson's user avatar
  • 255
8 votes
3 answers
3k views

Ruby programmatically calling method, with variable number of args

I am trying to do something similar to this: def foo(mode= :serial) if (mode == :serial) then self.send(:bar, "one_type") else self.send(:bar,"second_type",:T5) end end I can ...
John Nikolaou's user avatar
0 votes
0 answers
133 views

Calling all instance methods with multiple inheritance in Python

I am trying to create a set of classes as containers of modular blocks of logic. The idea is to be able to mix and match the classes through inheritance (possibly multiple inheritance) to execute any ...
FShoukry's user avatar
0 votes
0 answers
219 views

Before conditional for multiple instance methods in a rails model

I have several methods in my rails model that basically check the same before conditional before running their code so that when I run obj = new Model obj.method1(args) It will run the before method....
Jeff Davenport's user avatar
-2 votes
1 answer
119 views

How can use instance method before create instance of class?

i have this code : public class NumbersFragment extends Fragment { /** Handles playback of all the sound files */ private MediaPlayer mMediaPlayer; private MediaPlayer....
Yazan Khateeb's user avatar
2 votes
1 answer
498 views

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; ...
nanocv's user avatar
  • 2,229
2 votes
1 answer
3k views

Ruby Calling Class Method from Instance Method

What is the syntax for calling a class method from an instance method? Suppose I have the following class Class1 def initialize #instance method self.class.edit puts "hello" ...
Diane's user avatar
  • 21
0 votes
1 answer
34 views

Waterline - REST response missing instance methods

When calling an instance of a waterline object via REST, the response does not contain any of the the instance methods defined in the waterline model. I read somewhere that when the object is written ...
Andrew Bettke's user avatar
2 votes
1 answer
58 views

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 = ...
Saqib Ali's user avatar
  • 12.5k
0 votes
1 answer
47 views

PHP instance method vs. variable

It is possible that the two code below gives different results? This runs as i expected: $message = new Message(); $cond = $message->getMessage(); $helper->log($cond); if(!empty($cond)){ // ...
szenbalu's user avatar
  • 193
0 votes
2 answers
49 views

objective c simple calculator questions about addNumber instance method

- (IBAction)addButton; { for (int i = 0; i<10; i++) { UIButton *numberButton = [UIButton buttonWithType : UIButtonTypeRoundedRect]; [numberButton addTarget:self action:@...
Mike Schaf's user avatar
0 votes
3 answers
205 views

Extend return value of class instance method

I have a class that have an instance method, that returns a hash. I can't change the code of that class directly, but I can extend it with modules. I need to add some new keys to the returning hash of ...
Jack Owels's user avatar
1 vote
2 answers
42 views

Choosing which instance method to use

I wrote some code which takes a bunch of objects, uses an instance method on each of them and puts the value returned from the instance method in a list. object_list # list of objects value_list = [...
GeckStar's user avatar
  • 1,136
19 votes
5 answers
2k views

How to make len() work with different methods on different instances of a class, without modifying the class?

Is there a way to make len() work with instance methods without modifying the class? Example of my problem: >>> class A(object): ... pass ... >>> a = A() >>> a.__len__ ...
ARF's user avatar
  • 7,580
0 votes
1 answer
34 views

calling ruby instance method

class R def initialize(number) @number = number end attr_accessor :number end r = R.new(3) r.number => 3 r.@number => syntax error r.(@number) => undefined method call Why can't ...
Sean Magyar's user avatar
  • 2,370
0 votes
1 answer
215 views

How to access instance method using "self" in singleton class?

I am accessing instance method in a class method by using self in singleton class it shows error, but if I try to access instance method with parameter using self then its perfectly working, so ...
Prashant Goyal's user avatar
2 votes
1 answer
26 views

Why am I throwing no errors for calling object-specific methods/fields from inner classes?

I'm using something similar to the following code in one of my Java classes: public class SomeClass { private int someValue; void incrementValue() { someValue++; } public ...
Addison Crump's user avatar
1 vote
1 answer
2k views

Can not multiply a pandas "instance method" - how to change to float

I am using Pandas to calculate the standard deviation of a column in a data frame then multiply it by 100 to get a percentage, and then finally print it as follows: import pandas as pd results = pd....
s666's user avatar
  • 537
0 votes
1 answer
2k views

How can I dynamically add/extend methods from a Class?

I'm using a Ruby Component inside an Audio Application Environment. This Ruby Component is represented by an instance of a RubyEdit class. This provide the interface between my Ruby code and the ...
markzzz's user avatar
  • 47.7k
2 votes
1 answer
87 views

Why does 'is' not work when comparing instance methods in python [duplicate]

I've noticed that sometimes instance methods do not compare as identical (using is rather than ==), even when they relate to the same bound instance method e.g. >>> class A(object): ... ...
Phil Underwood's user avatar
11 votes
4 answers
51k views

Java create a unique ID for each instantiated object using instance methods instead of class/static methods

Quite new to this so I hope I have the terminology in the title right. I am trying to figure out how to create an instance method that will do the following: --An ID number is returned. --As each ...
SeesSound's user avatar
  • 503
0 votes
3 answers
309 views

Calling Instance Method with 'this' vs. calling it without 'this' - is there a difference?

Is there any difference between the call to getName() on line 8 and that on line 9. If yes, then what is it? This might be very simple but I did my Google search and the only SO result I got was ...
Solace's user avatar
  • 8,832
0 votes
2 answers
348 views

Turning a method into an enumerable method

I rewrote the map method: def my_map(input, &block) mod_input = [] x = -1 while x < input.length - 1 x = x + 1 if block == nil return input break end ...
Danny's user avatar
  • 11
0 votes
2 answers
56 views

Why can a factory method call an instance method?

As we all know, factory methods can't call instance methods. Why does the code below work? // .m file implementation DemoClass // custom instance init method - (instancetype)initWithDate:(NSDate *)...
Jack.Ma's user avatar
4 votes
2 answers
7k views

can't pickle instancemethod objects

I met a problem of pickle, Code is that: import cPickle class A(object): def __init__(self): self.a = 1 def methoda(self): print(self.a) class B(object): def __init__(...
DemoRunner's user avatar
2 votes
2 answers
47 views

Splitting program into classes and accessing variables from one class in another

I wrote a tictactoe program, and I am trying to organize it into classes. class Board attr_accessor :fields def initialize self.fields = { '1' => ' ', '2' => ' ', '3' => ' ', ...
ayda's user avatar
  • 23
0 votes
1 answer
155 views

Setters from Rails4 InstanceMethods for non-existing fields

I am writing a gem rails4 where i can add dynamic attributes which wont persist in the actual model table, rather it will save in my second table with a reference. Now i have before_validation to an ...
useranon's user avatar
  • 29.4k
-2 votes
1 answer
460 views

Where to Use Class Method ,Instance Method in Objective-C

"Instance" mean in Objective-C? Kindly tell me where to use Class Method And where to use Instance Method,also tell me where we use (Instacetype) method? why/where we use multi Parameters?
Rayaim's user avatar
  • 13
0 votes
0 answers
434 views

How to make a Python class instance pretend to be certain type (e.g. float)? [duplicate]

I create a Python class which has one important float value in its core, and all its methods work in this. It would be very convenient to make it behave smoothly with arithmetic operators, for example:...
deeenes's user avatar
  • 4,350
1 vote
2 answers
94 views

NoMethodError: Calling an instance method correctly in Ruby with the use of self

I was reading Why's (Poignant) guide to Ruby, and came across a method that didn't quite work out as expected. The method is aimed to return a value (from a hash) for a given string, somewhat like an ...
Dust_In_The_Wind's user avatar
1 vote
1 answer
377 views

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 ...
csi's user avatar
  • 9,218
0 votes
1 answer
119 views

How to pass instance method calls on to instance property?

Is there a way to pass all instance calls other than a specified few to an instances's property? I am trying to figure out the easiest way to write a wrapper class for PyMongo as below: class Mongo():...
sunny's user avatar
  • 3,873
1 vote
3 answers
332 views

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 ...
user1868607's user avatar
  • 2,560
22 votes
4 answers
13k views

Extension Methods vs Instance Methods vs Static Class [closed]

I'm a little bit confused about the different ways to use methods to interact with objects in C#, particularly the major design differences and consequences between the following: Invoking an ...
johnnyRose's user avatar
  • 7,410