Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7634745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:15:31+00:00 2026-05-31T07:15:31+00:00

I’m new to Ruby, so apologies if this sounds really silly. I can’t seem

  • 0

I’m new to Ruby, so apologies if this sounds really silly.

I can’t seem to figure out how to write a “main” code and have methods in the same file (similar to C). I end up with a “main” file which loads a seperate file that has all the methods. I appreciate any guidance on this.

I spotted the following SO post but I don’t understand it:
Should I define a main method in my ruby scripts?

While it’s not a big deal, it’s just easier being able to see all the relevant code in the same file. Thank you.

[-EDIT-]

Thanks to everyone who responded – turns out you just need to define all the methods above the code. An example is below:

def callTest1
    puts "in test 1"
end

def callTest2
    puts "in test 2" 
end

callTest1
callTest2

I think this makes sense as Ruby needs to know all methods beforehand. This is unlike C where there is a header file which clearly list the available functions and therefore, can define them beneath the main() function

Again, thanks to everyone who responded.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:15:32+00:00Added an answer on May 31, 2026 at 7:15 am

    @Hauleth’s answer is correct: there is no main method or structure in Ruby. I just want to provide a slightly different view here along with some explanation.

    When you execute ruby somefile.rb, Ruby executes all of the code in somefile.rb. So if you have a very small project and want it to be self-contained in a single file, there’s absolutely nothing wrong with doing something like this:

    # somefile.rb
    
    class MyClass
      def say_hello
        puts "Hello World"
      end
    end
    
    def another_hello
      puts "Hello World (from a method)"
    end
    
    c = MyClass.new
    c.say_hello
    another_hello
    

    It’s not that the first two blocks aren’t executed, it’s just that you don’t see the effects until you actually use the corresponding class/method.

    The if __FILE__ == $0 bit is just a way to block off code that you only want to run if this file is being run directly from the command line. __FILE__
    is the name of the current file, $0 is the command that was executed by the shell (though it’s smart enough to drop the ruby), so comparing the two tells you precisely that: is this the file that was executed from the command line? This is sometimes done by coders who want to define a class/module in a file and also provide a command-line utility that uses it. IMHO that’s not very good project structure, but just like anything there are use cases where doing it makes perfect sense.

    If you want to be able to execute your code directly, you can add a shebang line

    #!/usr/bin/env ruby
    
    # rest of somefile.rb
    

    and make it executable with chmod +x somefile.rb (optionally rename it without the .rb extension). This doesn’t really change your situation. The if __FILE__ == $0 still works and still probably isn’t necessary.

    Edit

    As @steenslag correctly points out, the top-level scope in Ruby is an Object called main. It has slightly funky behavior, though:

    irb
    >> self
    => main
    >> self.class
    => Object
    >> main
    NameError: undefined local variable or method `main' for main:Object
    from (irb):8
    

    Don’t worry about this until you start to dig much deeper into the language. If you do want to learn lots more about this kind of stuff, Metaprogramming Ruby is a great read 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.