Forum: Ruby on Rails why we are Using self.method_name?

Posted by Manoj M. (manoj_m)
on 2012-11-28 11:00
Whats is the idea behind using self.method_name ????? please help





Thanks.
Posted by Colin Law (Guest)
on 2012-11-28 11:06
(Received via mailing list)
On 28 November 2012 10:00, Manoj M. <lists@ruby-forum.com> wrote:
> Whats is the idea behind using self.method_name ????? please help

The first hit in google for
ruby def self
is

http://yehudakatz.com/2009/11/15/metaprogramming-i...

which seems to just about cover it.

Colin
Posted by Frederick Cheung (Guest)
on 2012-11-28 11:17
(Received via mailing list)
On Nov 28, 6:04am, Colin Law <clan...@googlemail.com> wrote:
> which seems to just about cover it.
>

Or it could be about disambiguating local variable versus method call.
Hard to tell without more context.

Fred
Posted by Manoj M. (manoj_m)
on 2012-11-28 11:18
Colin Law wrote in post #1086832:
> On 28 November 2012 10:00, Manoj M. <lists@ruby-forum.com> wrote:
>> Whats is the idea behind using self.method_name ????? please help
>
> The first hit in google for
> ruby def self
> is
>
> http://yehudakatz.com/2009/11/15/metaprogramming-i...
>
> which seems to just about cover it.
>
> Colin

Hello colin

it seems tougher, can you paste a link which is in simple words
Posted by fahim patel (fahim_patel)
on 2012-11-28 13:38
(Received via mailing list)
Read Class method and instance method in model .

On Wednesday, November 28, 2012 3:30:24 PM UTC+5:30, Ruby-Forum.com User
Posted by thil (Guest)
on 2012-11-28 13:47
(Received via mailing list)
Hi ,

Self.methods is static methods using the class name itself you can call 
the
methods and is not available to the instance of the class.

Thanks,
Senthil Srinivasan
Posted by Jordon Bedwell (Guest)
on 2012-11-28 15:03
(Received via mailing list)
On Wed, Nov 28, 2012 at 6:46 AM, thil <thil.1212@gmail.com> wrote:
> Self.methods is static methods using the class name itself you can call the
> methods and is not available to the instance of the class.

There is no such thing as static methods in Ruby.  self.method_name
(or sometimes self.class.method_name) are instance methods on the
singleton instance of the object (in 1.9 you can access the singleton
via singleton_class too).  Since everything is an object in Ruby
(literally) you have multiple types of instances, in his case he is
accessing instance methods on the singleton (or anonymous class or
eigen, however you want to label it) vs instance methods on /an/
instance of that object.
Posted by thil (Guest)
on 2012-12-04 08:27
(Received via mailing list)
class A
  def self.b
      print "this is static methods"
   end
 end

You can only be able to call the method "b" by using : A.b

that wat I am trying to say.
Posted by thil (Guest)
on 2012-12-04 08:34
(Received via mailing list)
Hi Jordon,

Thanks for your information and I agree on wat your saying.

Thanks,
Senthil Srinivasan
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.