Beginning with Ruby

#Print Hello World

puts "Hello World"

puts("This is my first ruby program")

#Print 10 Times Hello World

10.times do print "Hello, World" end

#Print Message and Result

# print the message

print "2+3 is equal to "

# print the addition of two number

print 2+3

# print the addition of two number with message

puts "2+3 is equal to" +(2+3).to_s

# print the Division

puts "Division " + (10/3).to_s

puts("Division with Float result" +(10.0/3).to_s)

#Writing Class in Ruby

class Person

attr_accessor :name, :age ,:gender

end

person_instance=Person.new

person_instance.name="Harshit"

person_instance.age=18

person_instance.gender="male"

puts person_instance.name

puts person_instance.age

puts person_instance.gender

#program to perform Addition two number by accepting input from user

print "Enter First Number"

num1=gets.to_i

print "Enter Second Number "

num2=gets.to_i

puts num1.to_s+" + "+ num2.to_s+ "="+(num1+num2).to_s

#Checking Type of Variable or values

puts(1.class)

puts(1.234.class)

x="ram"

puts(x.class)


# Making Constant in Ruby

C_CONST=5.12

C_CONST=2.23

puts(C_CONST)


# Ternary Operator

puts "Enter the Age"

age=gets.to_i

puts age>=50? "Old" : "Young"

#If Statement

age =29

if age<25

puts " You are a Teenager"

else

puts" You are not Teenager"

end


#Else If Ladder in Ruby

=begin

This program is for testing the age limit

with the help of age we will able to analyze the result

=end

puts "Enter First subject Marks"

sub1=gets.to_f

puts "Enter First Subject Marks"

sub2=gets.to_f

puts "Enter Third Subject Marks"

sub3=gets.to_f

puts "Enter Fourth Subject Marks"

sub4=gets.to_f

puts "Enter Fifth Subject Marks"

sub5=gets.to_f

avg=((sub1+sub2+sub3+sub4+sub5)/5).to_f

puts ("Average="+avg.to_s)

if(avg>=90.0)

puts "Grade-A"

elsif(avg>80.0)&& (avg<90.0)

puts "Grade B"

elsif(avg>70.0)&& (avg<80.0)

puts "Grad B-"

elsif(avg>50.0)&& (avg<70)

puts "Grade C"

elsif(avg>35.0)&& (avg<50)

puts "Grade D"

else

puts "Fail"

end


# File Operation in Ruby

newFile=File.new("sum.txt","w")

newFile.puts("Hello This content will go to sum file").to_s

newFile.close

read_data=File.read("sum.txt")

puts "Data read from File \n" +read_data