ADVERTISEMENT
ADVERTISEMENT

Ruby basics – Find output programs (set 1)

This section contains the Ruby basics find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on February 10, 2022

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!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.