ruby on rails

ruby OOP

흰두부1 2020. 11. 9. 14:07
# Object Oriented Programming

class Student
  #setter and getter
  attr_accessor :first_name, :last_name, :username, :email, :password
  #only getter
#  attr_reader :

=begin  
  #setter method
  def first_name=(name)
    @first_name=name
  end

  #getter method
  def first_name
    @first_name
  end
=end

  #definition
  @first_name
  @last_name
  @email
  @username
  @password

  #initialize
  def initialize(firstname, lastname, username, email, password)
    @first_name = firstname
    @last_name = lastname
    @username = username
    @email = email
    @password = password
  end

  def to_s
    "First name: #{@first_name}, Last name: #{@last_name}, Username: #{username}, email address: #{email}"
  end

end

mashrur = Student.new("Mashrur", "Hossain", "mashrur1", "mashrur@example.com", "password1")
john = Student.new("John", "Doe", "john1", "john1@example.com", "password2")
puts mashrur
puts john
mashrur.last_name = john.last_name
puts "Mashrur is altered"
puts mashrur



=begin
class User
  attr_accessor :name, :email
  def initialize(name, email)
    @name = name
    @email = email
  end
  def run
    puts "Hey I'm running"
  end
  def self.identify_yourself
    puts "Hey I am a class method"
  end
end
user = User.new("mashrur", "mashrur@example.com")
user.run
User.identify_youself # to run this class method you don't need an instance of user 
                      # you can directly call the class User 
=end