A Brief History of Ruby

Ruby is an object-oriented programming language developed by Yukihiro Matsumoto. Ruby is a dynamic programming language with a complex but at the same time expressive grammar. Ruby also has a core class library with a rich and powerful API.

Ruby is inspired by other low level and object oriented programming languages like Lisp, Smalltalk, and Perl and uses syntax that is easy for C and Java programmers to learn.

Ruby, a dynamic and open source programming language with a focus on simplicity and productivity, has an elegant syntax that is natural to read and easy to write.

Although it’s easy to program in Ruby, it is not a simple language. Our job here is to try and gain a deeper understanding of this programming language.

Features of Ruby

  1. Dynamic : Ruby is a very dynamic programming language. Ruby programs aren’t compiled like C or Java programs. All things in a program are built by the code when it is run. A program can also modify its own definitions while running.
  2. Singleton Classes : Every object in Ruby has two classes: a regular class and a singleton class. An object's singleton class is nameless class whose only instances is that object. Singleton classes are created automatically and make Ruby simple and elegant.
  3. Metaprogramming : Everything in Ruby are objects. You can use them to learn about them or even modify them, while your program is running. This technique is called metaprogramming.
  4. Flexibility : Methods can be added to existing classes without sub classing, operators can be overloaded, and even the behaviour of the standard library can be redefined at runtime.
  5. Variables and scopes : Programmer do not need to declare variables or variable scope in ruby. The name of the variable automatically determines its scope.

Examples

var is a local variable
$var is a global variable
@var is an instance variable
@@var is a class variable

Rules for Naming Variables

Variables must begin with a lowercase letter or underscore:

first_name = "Jeremy"

You can also write them using camelCase:

lastName = “Rodriguez”

Followed by the underscore or lowercase letter the next character of a variable can be a number or other letters, but not symbols. Names should match any of the reserved keywords. Some reserved keywords include, but are not limited to:

  • break
  • case
  • rescue
  • return
  • defined?

Constants are variables that holds the same value throughout the program. Ruby is a typical language, where you can reassign constants. As per conventions, all constants in ruby in uppercase characters so that they are easily distinguishable.

Notice that it did change the value of the constant, but it gave a warning saying that “PI is already initialized constant”.

So, just be aware that you can define constants in ruby, you can also change its value which means you wonder they shouldn’t be called constants. As we already said, Ruby is a typical language.

Operators and Expressions

Ruby: Addition Operator

+ symbol is used. It is both binary and unary operator. As the name suggests a Binary operator needs two operands/values on the either side of the operator to perform an operation and a unary operator needs just a single operand. Unary Plus serves no purpose, it is present just for the symmetry with unary minus.

For example : 1 + 2 //binary addition
value = +3 //unary plus

Ruby: Subtraction Operator

- symbol is used. You can use unary minus to reverse sign of a variable.

For Example : 23.2 - 2.2 //binary subtraction
value = -3 //unary minus

Ruby: Modulo Operator

Returns remainder after division.

For Example : 13 % 2 //returns 1 because that is the remainder

There’s a lot more which I plan to continue discussing in my next article to slowly ease you into this language. As always, there are a plethora of resources that can be scoured if you need additional information. For now, if you are just starting out, I hope that this was helpful and wish you the best of luck in your coding adventure! Thank you for taking the time to read through this!