[ Classes of Ruby ] Things are Classified for Convenience of Life Management, Not by Their Values
Tags: ruby, tutorial, fundamental, beginners
Published: 2022-11-07 04:31:33

Previous chapter: [[ Boxes of Ruby ]](https://dev.to/thinhtran130290/boxes-of-ruby-43lo)

> And then, our new concern is about how to create custom structured boxes.

So far, we've known that an application is nothing more than some blocks of code which are so called `procedure`s and get called at some time to perform certain tasks.

`~/Desktop/main.rb`
```
main = Proc.new { |name, age, online|
   if online
      puts "Hello, World!"
      puts "I'm #{name} and #{age} times cycled around the Sun."
   else
      puts "You're offline now."
      puts "Go online first."
   end
}

main.call "Semi-Art", 32, true
```

And we also get that any `box` may contains information and other boxes as `main`. And if we simplify the example code above in an abstract view with the block of custom code replaced like this:

`~/Desktop/main.rb`
```
main = Proc.new custom_code_block

main.call "Semi-Art", 32, true
```

Now, we see that the `Proc.new` is the actual action to create a bundle which encloses our custom code and the box `call`. So, something like the definition of `Proc.new` is what we truly seek.

`~/Desktop/main.rb`
```
main = Proc.new { |name, age, online|
   semi = Person.new name, age, online
   semi.greet
}

main.call "Semi-Art", 32, true
```

`CMD|Terminal.io`
```
main.rb:2:in `block in <main>': uninitialized constant Person (NameError)
	from main.rb:6:in `<main>'
```

Oh.. that's `Person.new` is just a feeble wish and not yet to be defined. And now, we're gonna learn the basic grammar to express stuff like so.

## The Class

```
class Person

   # - Person.new name, age, online
   def initialize (name, age, online)
      @name = name
      @age = age
      @online = online
   end
   
   # - Person.greet
   def greet
      if @online
         puts "Hello, World!"
         puts "I'm #{@name} and #{@age} times cycled around the Sun."
      else
         puts "You're offline now."
         puts "Go online first."
      end # if
   end

end # Person


# - main - - - - - - - - - - - - - - - - - -

main = Proc.new { |name, age, online|
   semi = Person.new name, age, online
   semi.greet
}

main.call "Semi-Art", 32, true
```

The hash symbols `#` are used to write comments in the code so that the computer will just ignore some words. So, you may need some moments to get familiar with those comments to exclude them from the real code.

There we have a `class Person`.

```
class Person
end
```

And a couple of definitions for `new` and `greet`.

```
class Person

   def initialize (name, age, online)
   end

   def greet
   end

end # Person
```

These all are just parts of a sketch to describe a person who has a `name`, is currently at some `age`, v.v... and has ability to perform an act of `greet`-ing.

And all these things just come a little more real when we invoke the code at `main`:

```
semi = Person.new name, age, online
semi.greet
```

Before the `greet` is invoked. All the information we gave into the `new` is bundled with the pattern we've sketched before.

```
def initialize (name, age, online)
   @name = name
   @age = age
   @online = online
end
```

So, the inner boxes are just a little more special because of the prefix symbol `@`. It means that all of these boxes are stored `at` the same particular outer `box` which is created when we invoke `Person.new`.

And that prefix symbol is also the way we access those boxes in the definition of `greet`.

```
def greet
   if @online
      puts "Hello, World!"
      puts "I'm #{@name} and #{@age} times cycled around the Sun."
   else
      puts "You're offline now."
      puts "Go online first."
   end # if
end
```

## And Classification of The Mind

Then `information` may come from other sources like static files on the Desktop folder. And an application may stimulate different lifetimes to work towards the liberation plane.

```
main = Proc.new { |*options|
   semi = Person.new "Semi", 32, true
   semi.greet
   puts "- - - - - - - - - -"
   marc = Crafter.new "Marc", 21, true
   marc.greet
   marc.craft
   puts "- - - - - - - - - -"
   fred = Teacher.new "Steven", 21, true
   fred.greet
   fred.teach
}

main.call
```

Yes, we know that `Crafter` and `Teacher` are just `Person` to be classified. And each of these people may perform different labor act like `craft` and `teach`.

```
class Person
   def initialize ...
   def greet ...
end # Person


class Crafter < Person
   def craft
      puts "#{@name} is crafting..."
   end
end # Crafter


class Teacher < Person
   def teach
      puts "#{@name} is teaching..."
   end
end # Teacher
```

`CMD|Terminal.io`
```
Hello, World!
I'm Semi and 32 times cycled around the Sun.
- - - - - - - - - -
Hello, World!
I'm Marc and 21 times cycled around the Sun.
Marc is crafting...
- - - - - - - - - -
Hello, World!
I'm Steven and 21 times cycled around the Sun.
Steven is teaching...
```

Yet, like the `Proc.new` we've seen at first. The `class`es are not only used to stimulate real world entities but also used to construct applications with conceptual elements of code. Things should go well when we have a little purpose to write more code of the uses. Do you want to create a simple Tic-Tac-Toe console app?

## Technical Terms

`class` - a definition of a `class` which group a certain kind of entities which have the same properties and abilities to perform acts.

`instance` or `object` - an actual `box` encloses real information which is created by `Class.new`.

`property` - an inner `box` which represent a piece of information stored `@`t a certain `object`. 

`method` - a `def`inition of a block of code which describe the ability to perform an act.

`inheritance` - the possibility of defining a new `class` which make use of the definition of an existing `class` to reduce repetitive code.

See you in the next chapter: [[ Gets in Ruby ]](#)