< Language Tutorials

Revision as of 01:43, 4 January 2019 by KrIsMa (talk | contribs) (revert...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Document stub.png This article is a stub. It may be incomplete, unfinished, or have missing parts/sections. If the article can be expanded, please do so! There may be suggestions on its talk page. (August 2018)

As is mentioned on the Create Clone of () (block) article, classes are like clones in Scratch. There is the class, and there are instances of the class, which are things with the class's info and added info of their own.

Creating a Class

One can start creating a class with the class keyword, like so:

class Book
end

Next, the class should have an initialize method. This can be created like so:

class Book
  def initialize
  end
end

But, what about the differences between instances of this class? The class needs parameters.

class Book(title, age_group, genre)
  def initialize
  end
end

When the parameters for an instance of the class are known, the class should have those parameters in a variable specific to the instance of the class.

class Book(title, age_group, genre)
  def initialize
  @title = title # Variables beginning with "@" are specific to a certain instance of a class.
  @age_group = age_group # Here, the variables are being set to whatever the parameters were set to.
  @genre = genre
  end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.