Ruby on Rails Tuesday, April 27, 2010

Hi,


'::' method is used to refer to modules inside a module.

If you define a method inside a module eg)

module Test
 def check
  puts "check"
 end
end

Then you create a module and define an instance method by the name 'check'
This instance method can only be access by an instance. So it can be accessed by instances of classes that have included that module only.
You can get the method object directly using Test.instance_method(:method_name), this will give you the UnboundMethod obejct which you can then bind to an instance of an object and use it if you really needed to, other than that I am not sure if you could do anything about it directly.

If you want to define a method inside a module and be able to access it directly using the module without any inclusion & instantiation then you should do the following:

module Test
 def self.check
  puts "check"
 end
end

Now you can call the method check using Test.check #=> check

On Wed, Apr 28, 2010 at 12:18, Mike Disuza <lists@ruby-forum.com> wrote:
Hi,

I want to know the use of "::" operator in rails.
Actually, I have created a module with name "Email" and written a method
def check_email(email)
end
I want to access this method. So I went to the "ruby/script console" and
done like as follows:-

"Email::check_email('test@gmail.com')"
I got an error that "undefined method `check_email' for Email:Module".
Can anyone tell me what is wrong with my code?
Also Is it the use of :: operator?


Thanks
Mike
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.




--

Thanks & Regards,
Dhruva Sagar.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment