Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Ruby and Rails

Classes and Objects in Ruby

Book navigation

  • Single line swapping in Ruby
  • Array Operations in Ruby
  • File operations in Ruby
  • User defined functions in Ruby
  • Classes and Objects in Ruby
  • Simple Form Hello Rails Application
  • Database Application using scaffold - [Video]
  • Session Management
  • Cookies Management in Ruby on Rails
  • File Uploads in Ruby on Rails
By Karthikeyan , 19 October, 2012
Example #1

class Stud
  def getdata()
    puts "Enter Numer:"
    @sno = gets()
    puts "Enter name:"
    @sname = gets()
  end
  def putdata()
    puts "Your Number is #{@sno}"
    puts "Your Name is #{@sname}"
  end
end
class Smarks < Stud
  def initialize(m1,m2,m3)
    @m1 = m1
    @m2 = m2
    @m3 = m3
  end
  def total
    if @m1 >=50 and @m2>=50 and @m3>=50
      pass = 1
    else
      pass = 0
    end
      @total = @m1 + @m2+@m3
      @average = @total/3
        if @average >=80
          @cls="Distinction"
        elsif @average >=60
          @cls="First"
          elsif @average >=50
          @cls="Second"
        end

    dispdata(pass)
    end
    def dispdata(pass)
      puts "Student Details"
      puts "----------------"
      putdata
      puts "Total is #{@total}"
      puts "Average is #{@average}"      
    if pass==1
      puts "Result : PASS"
      puts "Class : #{@cls}"
      puts "----------------"
    else
      puts "Result : RE-APPEAR"
      puts "----------------"
    end

     end
end
a=Smarks.new(49,64,94)
a.getdata()
a.total
c=Smarks.new(52,64,78)
c.getdata()
c.total
Sample Ouput :-

Enter Numer:
15
Enter name:
raju
Student Details
----------------
Your Number is 15
Your Name is raju
Total is 207
Average is 69
Result : RE-APPEAR
----------------
Enter Numer:
75
Enter name:
Memo
Student Details
----------------
Your Number is 75
Your Name is Memo
Total is 194
Average is 64
Result : PASS
Class : First
----------------
  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy