Are you considering learning Ruby? Before you jump in, let's take a look at some of the basics of Ruby. It's history, it's design, and a brief look at the code will help any Ruby-beginner better understand the language, and decide if Ruby is the language for them.
A Brief History
Ruby was created in 1993 by Yukihiro Matsumoto. The creation of Ruby was heavily influenced by Perl and Python. Although both are object-oriented languages, Matsumoto felt that the object-oriented aspects of these languages were more of an after-thought. He set out to create a language that was object-oriented in it's core design. Thus, he created Ruby.
Matsumoto also designed Ruby with usability and simplicity in mind. He created Ruby with the intention of creating the best experience for developers. Rather than focusing on designing Ruby to best interact with the computer, his focus was to make a language that best interacted with the human using it.
I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy.
-- Yukihiro Matsumoto
In 2005, Ruby on Rails was released. This web development framework had a huge impact on the popularity of the language overall. Rails made it easier and more feasible for many people to create their own web applications with Ruby, including small businesses and startups. After Rails was released, the usage of Ruby took off.
For almost two decades now, Ruby has remained a popular programming language. It had been used to create many large websites, such as Github, Airbnb, Hulu, and Shopify. Although it's usage has decreased somewhat in recent years, Ruby remains consistently in the top 10 most popularly used languages.
What is Ruby?
Ruby is a dynamically typed, object-oriented, interpreted programming language. Let's break all of that down into plain English.
Ruby is dynamically typed as opposed to statically typed. This means that types are checked at runtime. This may allow developers to write less code and for interpreters to load new code dynamically. For reference, examples of other dynamically typed languages include Javascript and Python.
Ruby is an object-oriented language, meaning it's design is organized around objects and data. Moreover, everything in Ruby is considered an object. Strings, integers, and booleans are all considered objects.
Finally, Ruby is interpreted rather than compiled. Another program interprets the language and executes instructions, rather than the target machine.
A Look At Ruby
Let's get a feel for Ruby by writing some basic code. Since Ruby is heavily object-focused, I will demonstrate how to make a class constructor to create many like objects.
class Cat
def
initialize(name, color)
@name = name
@color = color
@ranking = 'the best cat ever'
end
def prnt
puts "This is #{@name}, he is #{@color} and he is #{@ranking}!"
end
end
Here, we use the class
keyword to create a new class of Cat objects. The def
keyword lets Ruby know that you are defining a method. In particular, we first use initialize
to set some default values for our objects. We want to give every cat object default values of name
, color
, and ranking
. In Ruby, the @
symbol is used to create an instance variable.
Next, we create another method called prnt
. To keep the demonstration simple, all this method will do is print a sentence about my object. We could use the print
keyword to print any following code. Instead, we will use puts
, which also prints any following code, but with a built-in line break at the end.
In our sentence, we use string interpolation to refer to our object variables. For those who are familiar, this is comparable to template literals in ES6.
Now that we have our Cat class created, let's create some new instances of cat objects.
cat_one = Cat.new('Dr. Chocolate', 'brown')
Here, we create a new variable cat_one
. Note that declarative keywords are not used in Ruby. Rather, the casing of the variable determines it's type. cat_one
is lower-case, therefore it is not constant and can be re-assigned.
If we want to create a constant variable, we can use an upper-case variable name.
CAT_TWO = Cat.new('Mr. Mustard', 'orange')
CAT_TWO
is a constant variable and cannot be reassigned.
Now that we have created some cat objects, we can access their methods. Let's use the prnt
method to print descriptive sentences about our objects.
cat_one.prnt
# => This is Dr. Chocolate, he is brown and he is the best cat
# ever!
CAT_TWO.prnt
# => This is Mr. Mustard, he is orange and he is the best cat
# ever!
Conclusion
In conclusion, Ruby is a general purpose programming languages used for many things, most often used for building web applications. Ruby has a strong online community because of it's long-held popularity. Because of it's simplicity, Ruby is a good language for beginners to learn. Additionally, it is especially intuitive for programmers who have prior experience with another language.
Sources
Top comments (0)