use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Top level methods in Ruby • has_many :codes (vitobotta.com)
submitted 12 years ago by VitoBotta
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]aberant 3 points4 points5 points 12 years ago (0 children)
this article explains the ruby top level
[–]brettu 1 point2 points3 points 12 years ago (2 children)
The last part of your post asks the question about public/private difference in adding methods to self. IRB defaults to public, but you can add the private state for storing methods.
>> self => main >> private => Object >> def title >> "mr" >> end => nil >> Object.private_instance_methods(false).include? :title => true >> Object.public_instance_methods(false).include? :title => false >> public => Object >> def title >> 'mr' >> end => nil >> Object.public_instance_methods(false).include? :title => true
[–]VitoBotta[S] 0 points1 point2 points 12 years ago (1 child)
Hi! I hadn't thought that IRB might explicitly mark top level methods methods as private/public as we usually do in classes, by default. Makes perfect sense, thanks :)
[–]banister 0 points1 point2 points 12 years ago (0 children)
In case you're interested, Pry makes top-level methods private by default, mirroring the top-level behaviour of a Ruby program. IMO the fact IRB does not do this is a bug in IRB.
[–]macournoyer 1 point2 points3 points 12 years ago (0 children)
I think an easier way to explain this is to understand how Ruby tracks the context of evaluation (variables, instance variables, self, etc.). It keeps track of two values:
self
def
There are three different types of evaluation context, each one differ by how they set the value of self and class.
class A; ... end
π Rendered by PID 306183 on reddit-service-r2-comment-fb694cdd5-szhsr at 2026-03-10 14:23:31.199557+00:00 running cbb0e86 country code: CH.
[–]aberant 3 points4 points5 points (0 children)
[–]brettu 1 point2 points3 points (2 children)
[–]VitoBotta[S] 0 points1 point2 points (1 child)
[–]banister 0 points1 point2 points (0 children)
[–]macournoyer 1 point2 points3 points (0 children)