Table of Content
Ruby basic
In this page, we cover the essential ruby syntax.
- Variable
- Method
- Class
Variable
We use snake_case with multiple words variable.
Method
The following we define a method that takes no parameters.
When calling a method with parameters, we can just type the name.
1method_name
The following method definition takes one parameter.
In string, we can use #{}
to put variable inside double quoted string.
1"hello #{name}"
When we call the method with parameters, we can either use the ()
or a spaces.
Class
Class are defined in CamelCase.
Comments line begins with #
symbol. The comment valid till the line end.
All methods below the private
keywoard are private.
Define the method with self.
makes it a class method.
An initialize
method is a constructor method.
Here we create instance from a class definition with the new
class method.
@variable
with the @
sign is instance variable.
Attribute setting attr_reader
allows us to define instance variable with read-only public access.
Did you notice the bar=
method?
Here we have instance variable with _read-write- public access with attr_accessor
.
And we can define custom setter method.
Let's test our Foo class with custom setter method.
What's more? Please go te TryRuby to learn the ruby syntax.
What’s next? We’re going to take a look at “Installing Rails”.