Questions tagged [methods]

A method is a block of code that performs a task and is associated with a class or an object. It is related to the non-object-oriented concepts of functions and procedures.

A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments.

In object-oriented programming, a method is a subroutine (or procedure) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time.

Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance.

The association between class and method is called binding. A method associated with a class is said to be bound to the class. Methods can be bound to a class at compile time (static binding) or to an object at runtime (dynamic binding).

Common features

They have few things in common:

  1. They may take some parameters / arguments for their processing.
  2. They may have a return-value, that returns some value to the calling method.
  3. The concept of and is also associated with methods.

References

Also see:

30291 questions
45
votes
3 answers

How do I log every method that's called in a Ruby program?

I've inherited a large pile of Ruby code that's, frankly, close to impossible to understand for a mortal like myself. It's actually Rspec unit test code, but the structure is "highly unusual" to put it nicely. What I'd like to be able to do is run…
monch1962
  • 5,151
  • 5
  • 31
  • 38
45
votes
6 answers

How do methods use hash arguments in Ruby?

I saw hash arguments used in some library methods as I've been learning. E.g., list.search(:titles, genre: 'jazz', duration_less_than: 270) Can someone explain how a method uses arguments like this, and how you could create a method that makes use…
Senjai
  • 1,811
  • 3
  • 20
  • 40
45
votes
5 answers

Vim: Go to Beginning/End of Next Method

Is there native functionality in Vim that allows one to move the cursor to the beginning/end of the next method? I already know about [[, ]], [], and ][, but these don't cut the job, because they only work on braces that are in column zero. Hence,…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
44
votes
2 answers

Difference between isEqualTo: and isEqual:

What on earth is the difference between these methods?
lbrndnr
  • 3,361
  • 2
  • 24
  • 34
44
votes
5 answers

Can a C# method return a method?

Can a method in C# return a method? A method could return a lambda expression for example, but I don't know what kind of type parameter could I give to such a method, because a method isn't Type. Such a returned method could be assigned to some…
Miro
  • 1,778
  • 6
  • 24
  • 42
44
votes
3 answers

What is method inlining?

I've been trying to understand what that really means : inline function In C++, a member function defined in the class declaration. (2) A function call that the compiler replaces with the actual code for the function. The keyword inline …
Tarik
  • 79,711
  • 83
  • 236
  • 349
44
votes
3 answers

Python using methods from other classes

If I have two classes, and one of them has a function that I want to use in my other class, what do I use so that I don't have to rewrite my function?
James
  • 513
  • 2
  • 6
  • 7
43
votes
7 answers

C#: How to create an attribute on a method triggering an event when it is invoked?

Is there a way in C# or .NET in general to create an attribute on a method which triggers an event when the method is invoked? Ideally, I would be able to run custom actions before and after the invocation of the method. I mean something like…
Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
43
votes
10 answers

Is it a bad idea to declare a final static method?

I understand that in this code: class Foo { public static void method() { System.out.println("in Foo"); } } class Bar extends Foo { public static void method() { System.out.println("in Bar"); } } .. the static…
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
42
votes
4 answers

Ruby convert string to method name

I have two methods defined in my ruby file. def is_mandatory(string) puts xyz end def is_alphabets(string) puts abc end An array containing the names of the methods. methods = ["is_mandatory", "is_alphabets"] When I do the…
verdure
  • 3,201
  • 6
  • 26
  • 27
42
votes
2 answers

What is the use of mongoose methods and statics?

What is the use of mongoose methods and statics and how are they different from normal functions? Can anyone explain the difference with example.
iiR
  • 707
  • 1
  • 6
  • 21
42
votes
7 answers

codeigniter check for user session in every controller

I have this private session in one of my controllers that checks if a user is logged in: function _is_logged_in() { $user = $this->session->userdata('user_data'); if (!isset($user)) { return false; } else { return…
user393964
42
votes
3 answers

Call a model method in a Controller

I'm have some difficulties here, I am unable to successfully call a method which belongs to a ProjectPage model in the ProjectPage controller. I have in my ProjectPage controller: def index @searches = Project.published.financed …
sidney
  • 2,704
  • 3
  • 28
  • 44
41
votes
2 answers

What is a method that's inside another method called?

What type of method is String dgvValue(int cell) in the below code? private void btnEdit_Click(object sender, EventArgs e) { if (dgvGuestList.SelectedRows.Count > 0) { String dgvValue(int cell) { return…
Steven Tan
  • 435
  • 4
  • 6
41
votes
6 answers

Executing code for every method call in a Ruby module

I'm writing a module in Ruby 1.9.2 that defines several methods. When any of these methods is called, I want each of them to execute a certain statement first. module MyModule def go_forth a re-used statement # code particular to this…
GladstoneKeep
  • 3,832
  • 2
  • 26
  • 38