Convenient things in Ruby for beginners

Garret Coffey
4 min readJun 15, 2021

Ruby was designed to be as easy to use as possible. Here are some of the convenient things Ruby does as that you should know as you get started.

Ruby uses a lot more words to describe things. This is nice at it makes it easier to remember syntax when I am using a lot more letters instead of special characters.

def my_method
puts "hello"
end

Above is how you would define a method in Ruby. you use def to mark the start. The following phase will be the method name. As usual there can not be spaces in the method name. In Ruby we use snake case my_method for most instances. We place the code we want the method to do on the next line. In this case I am using puts to send the string hello to the terminal. Puts is the equivalent of console.log() in Javascript. We then finish the method by using end to denote we are done.

In Ruby methods always return the last line of code. Whatever ever is on the last line of code before the end of your method will automatically be returned. In the case above since we only have one line of code it would have returned the result of puts “hello”. It is important to note that puts always returns Nil. If we wanted to return the string hello we would need to write it without the puts.

def my_method
"hello"
end # would return hello
def two_plus_two
puts 3
2 + 2
end # would return 4

The two_plus_two method above would first send the number 3 to the terminal using the puts command but would then return the number 4 since the last line is our math equation.

Another convenient aspect of Ruby is that our classes are automatically connected. If I was two define two classes in different files.

class MyClassOne  def my_method
5 + 6
end
end

And this class in another file

class MyClassTwo  def what_is_5_plus_6
MyClassOne.my_method
end
end

We can freely tell our first class that we want to use its method somewhere else without defining the file path it needs to take to get here.

If you notice we used camelCase on our class name. We also capitalized the first letter. For class names you should always capitalize the first letter and the first letter of another subsequent words for names of classes.

Ruby has a few features called attr_accessor, attr_reader, attr_writer. We can use thing to quickly declare variables inside or class like we would methods.

class MyClass
attr_reader :book
attr_writer :poem
attr_accessor :name
# @book, @poem, @name we could simply write these in our method to
# declare them as instance variables but without the
# attr_accessor we would need to write methods to access them
# these methods will be shown later
end

Attr_reader allows you to read the value of an instance variable and attr_writer allows you to set the value of an instancevariable. Attr_accessor allows for both. If you notice we used : before the variables when we coded them. The : tells ruby that these belong to the symbol datatype the same way that “hello” defines string datatype. We can interact with these symbols in several ways. If we are inside the method where they are defined we use the @ placed in front of their name since they are instance variables.

@name = Bob

Would set the value of name to Bob. Alternatively we can ask for the value of name by simply calling

@name #would give us Bob

We can also access these the values like we do methods in other classes.

class MyClassTwo
MyClass.name # would give us Bob
MyClass.poem = Songbird # would set the value of poem to Songbird
MyClass.poem # would give us nothing
MyClass.book # would also give nothing
end

This allows us to easily access these variables outside of our method. But hold up above it says we got nothing when we called poem and book. If you remember above we declared poem with the attr_writer which allows to set the value of poem but when we call MyClass.poem it does not let us read the value. Alternatively MyClass.book would give us nothing since we never gave it a value. So what use is reader and writer?

def name
@name
end
def name=(value)
@name = value
end
def book=(value)
@book = value
end
def poem
@poem
end

As mentioned earlier the attr_accessor methods are simply allowing us to declare and use instance variables but without declaring the methods show above we couldn’t use our instance variables. But sometimes we want to do something more the simply set the value of instance variable so it can be useful to declare with attr_reader and attr_writer if we want more functionality to the methods for setting and returning the values of the instance variables otherwise you will mainly use the attr_accessor over the other two.

--

--