OneBite.Dev - Coding blog in a bite size

declare a class in Ruby

Code snippet on how to declare a class in Ruby

  class User
    attr_accessor :name, :email
    def initialize(name, email)
      @name = name
      @email = email
    end
  end
  ``
  
  This code creates a class called User and allows us to store and access data associated with that class. In this example, the class has two attributes, name and email. The attr_accessor method is used to set up a getter and setter methods for the object attributes. These methods will allow us to read and write the attributes for each object. The initialize method is a special method that is used to create new objects from the class. It sets up the initial attributes for each object. When we create a new object from the class, we will pass in the name and email values as parameters. These parameters are then set to the object's attributes.
ruby