Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Ruby's main object

Posted on Oct 1 In Java, every Function must be wrapped by a class, but in Ruby, we can define a function without classes. Therefore, some years ago I used to suppose that:There are no functions in Java, only methods. But for Ruby, we can have a function that isn't a method. However, these days I have realized that: "Every function in Ruby is also a method".But why can we define the function at the top-level scope, without creating classes for them, like Java? Or which class does this function belong to?For example, I have the function incrementWe try to call increment this way:Since self keyword contains a reference to the current object, the receiver for the current method, increment must be a method. Now let's check which object self reference to.You can see puts self code returns string "main", and it is an instance of Object, the root class of Ruby’s class hierarchy. So we can conclude that increment is an instance method in the Object class. Technically speaking, Ruby automatically creates a main object as the top self object, it is the default receiver for top-level methods. Actually, all Ruby functions are private methods of Object.Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Pierre-Henry Soria ✨ - Jul 10 Andrei Maxim - Jul 9 Vlad Hilko - Jul 7 Blake Creasser - Jun 16 Once suspended, hungle00 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, hungle00 will be able to comment and publish posts again. Once unpublished, all posts by hungle00 will become hidden and only accessible to themselves. If hungle00 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to hungle00. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag hungle00: hungle00 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging hungle00 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Ruby's main object

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×