├── README.md ├── For.rb ├── Until.rb ├── ternary.rb ├── while.rb ├── Array.rb ├── Loop.rb ├── Hello World.rb ├── next.rb ├── redo.rb ├── Break.rb ├── Do_While.rb ├── hashes.rb ├── Condition.rb ├── global.rb ├── Switch.rb ├── Instance.rb ├── Operations.rb └── class_variable.rb /README.md: -------------------------------------------------------------------------------- 1 | # Ruby_On_Rails_Practice_Question -------------------------------------------------------------------------------- /For.rb: -------------------------------------------------------------------------------- 1 | a = gets.chomp.to_i 2 | for i in 1..a do 3 | puts i 4 | end -------------------------------------------------------------------------------- /Until.rb: -------------------------------------------------------------------------------- 1 | i = 1 2 | until i == 10 3 | puts i*10 4 | i += 1 5 | end -------------------------------------------------------------------------------- /ternary.rb: -------------------------------------------------------------------------------- 1 | var = gets.chomp.to_i; 2 | a = (var > 3 ? true : false); 3 | puts a -------------------------------------------------------------------------------- /while.rb: -------------------------------------------------------------------------------- 1 | x = gets.chomp.to_i 2 | while x >= 0 3 | puts x 4 | x -=1 5 | end -------------------------------------------------------------------------------- /Array.rb: -------------------------------------------------------------------------------- 1 | data = ["Akash", "Ankit", "Aman"] 2 | puts data[0] 3 | puts data[1] 4 | puts data[2] -------------------------------------------------------------------------------- /Loop.rb: -------------------------------------------------------------------------------- 1 | x = ["Blue", "Red", "Green", "Yellow", "White"] 2 | for i in x do 3 | puts i 4 | end -------------------------------------------------------------------------------- /Hello World.rb: -------------------------------------------------------------------------------- 1 | # Hello World Program in Ruby 2 | print "ashish " 3 | puts "Hello World!"; 4 | puts "ashish" -------------------------------------------------------------------------------- /next.rb: -------------------------------------------------------------------------------- 1 | for i in 5...11 2 | if i == 7 then 3 | next 4 | 5 | end 6 | puts i 7 | end 8 | -------------------------------------------------------------------------------- /redo.rb: -------------------------------------------------------------------------------- 1 | i = 0 2 | while(i < 5) # Prints "012345" instead of "01234" 3 | puts i 4 | i += 1 5 | redo if i == 5 6 | end -------------------------------------------------------------------------------- /Break.rb: -------------------------------------------------------------------------------- 1 | i = 1 2 | while true 3 | if i*5 >= 25 4 | break 5 | end 6 | puts i*5 7 | i += 1 8 | end -------------------------------------------------------------------------------- /Do_While.rb: -------------------------------------------------------------------------------- 1 | loop do 2 | puts "Checking for answer" 3 | answer = gets.chomp 4 | if answer != '5' 5 | break 6 | end 7 | end -------------------------------------------------------------------------------- /hashes.rb: -------------------------------------------------------------------------------- 1 | data = {"Akash" => "Physics", "Ankit" => "Chemistry", "Aman" => "Maths","Akash" => "Physi"} 2 | puts data["Akash"] 3 | puts data["Ankit"] 4 | puts data["Aman"] -------------------------------------------------------------------------------- /Condition.rb: -------------------------------------------------------------------------------- 1 | a = gets.chomp.to_i 2 | if a <50 3 | puts "Student is fail" 4 | elsif a >= 50 && a <= 60 5 | puts "Student gets D grade" 6 | elsif a >= 70 && a <= 80 7 | puts "Student gets B grade" 8 | elsif a >= 80 && a <= 90 9 | puts "Student gets A grade" 10 | elsif a >= 90 && a <= 100 11 | puts "Student gets A+ grade" 12 | end -------------------------------------------------------------------------------- /global.rb: -------------------------------------------------------------------------------- 1 | $global_var = "GLOBAL" 2 | class One 3 | def display 4 | puts "Global variable in One is #$global_var" 5 | end 6 | end 7 | class Two 8 | def display 9 | puts "Global variable in Two is #$global_var" 10 | end 11 | end 12 | 13 | oneobj = One.new 14 | oneobj.display 15 | twoobj = Two.new 16 | twoobj.display -------------------------------------------------------------------------------- /Switch.rb: -------------------------------------------------------------------------------- 1 | print "Enter your day: " 2 | day = gets.chomp 3 | case day 4 | when "Tuesday" 5 | puts 'Wear Red or Orange' 6 | when "Wednesday" 7 | puts 'Wear Green' 8 | when "Thursday" 9 | puts 'Wear Yellow' 10 | when "Friday" 11 | puts 'Wear White' 12 | when "Saturday" 13 | puts 'Wear Black' 14 | else 15 | puts "Wear Any color" 16 | end -------------------------------------------------------------------------------- /Instance.rb: -------------------------------------------------------------------------------- 1 | class States 2 | def initialize(name) 3 | @states_name=name 4 | end 5 | def display() 6 | puts "States name #@states_name" 7 | end 8 | end 9 | 10 | # Create Objects 11 | first=States.new("Assam") 12 | second=States.new("Meghalaya") 13 | third=States.new("Maharashtra") 14 | fourth=States.new("Pondicherry") 15 | 16 | # Call Methods 17 | first.display() 18 | second.display() 19 | third.display() 20 | fourth.display() -------------------------------------------------------------------------------- /Operations.rb: -------------------------------------------------------------------------------- 1 | puts("Unary operator") 2 | puts(~5) 3 | puts(~-5) 4 | puts(!true) 5 | puts(!false) 6 | puts("add operator") 7 | puts(10 + 20) 8 | puts("subtract operator") 9 | puts(35 - 15) 10 | puts("multiply operator") 11 | puts(4 * 8) 12 | puts("division operator") 13 | puts(25 / 5) 14 | puts("exponential operator") 15 | puts(5 ** 2) 16 | puts("modulo operator") 17 | puts(25 % 4) 18 | puts("Ternary operator") 19 | puts(2<5 ? 5:2) 20 | puts(5<2 ? 5:2) 21 | puts("Comparison operator") 22 | puts(2 == 5) 23 | puts(2 != 5) 24 | puts(2 > 5) 25 | puts(2 < 5) 26 | puts(2 >= 5) 27 | puts(2 <= 5) -------------------------------------------------------------------------------- /class_variable.rb: -------------------------------------------------------------------------------- 1 | class States 2 | @@no_of_states=0 3 | def initialize(name) 4 | @states_name=name 5 | @@no_of_states += 1 6 | end 7 | def display() 8 | puts "State name #@state_name" 9 | end 10 | def total_no_of_states() 11 | puts "Total number of states written: #@@no_of_states" 12 | end 13 | end 14 | 15 | # Create Objects 16 | first=States.new("Assam") 17 | second=States.new("Meghalaya") 18 | third=States.new("Maharashtra") 19 | fourth=States.new("Pondicherry") 20 | 21 | # Call Methods 22 | first.total_no_of_states() 23 | second.total_no_of_states() 24 | third.total_no_of_states() 25 | fourth.total_no_of_states() --------------------------------------------------------------------------------