×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby basics – Find output programs (set 1)

Last Updated : December 15, 2025

Program 1

def main
    print "Hello World";
    puts "Hello World";
end

Output

Nothing will print.

Explanation

In the above program, we created a method main(), but we did not call it. That is why nothing will be printed.


Program 2

def main
    int num = 20;
    string msg = "Hello World"
    
    puts "Num: ", num;
    puts "Message: ", msg;
end

main();

Output

main.rb:2:in `main': undefined method `int' for main:Object (NoMethodError)

Explanation

The above program will generate a syntax error because "int" and "string" are not any predefined datatype in ruby.


Program 3

def main
    num = 20;
    msg = "Hello World"
    
    puts "Num: ", num;
    puts "Message: ", msg;
end

main();

Output

Num: 
20
Message: 
Hello World

Explanation

In the above program, we created an integer variable num and a string variable msg. Then we printed the value of variables using the puts() method. The puts() method prints the newline by default.


Program 4

def main
    num = 20;
    msg = "Hello World"
    
    print "Num: ", num;
    print "Message: ", msg;
end

main();

Output

Num: 20Message: Hello World

Explanation

In the above program, we created an integer variable num and a string variable msg. Then we printed the value of variables without newline character using the print() method.


Program 5

def main
    num = 20;
    msg = "Hello World"
    
    println "Num: ", num;
    println "Message: ", msg;
end

main();

Output

main.rb:5:in `main': undefined method `println' for main:Object (NoMethodError)

Explanation

The above program will generate a syntax error because println() is not a predefined method in ruby.

Ruby Find Output Programs »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.