You are on page 1of 5

ADAP, Vielka Angela V.

CS21S1

3.1 COMPILER VS. INTERPRETER


Ruby
In programming, there are two ways of translating the program code into a language that the computer will
understand (machine language). First, there is a compiler (responsible for compiling) where in the compiler
accepts the entire program before translating it into object code that is stored in a file, where the machine
executes the file afterward. An interpreter, on the other hand, translates program code without having to
convert them into object code or machine code, directly executing a program. A good example of an
interpreted program language is Ruby. It is described as an open-source programming language that
focuses on ease and writability as its syntax is easy for every programmer to understand while maintaining
productivity. Yukihiro Matsumoto, Ruby’s creator, mixed different parts of his favorite languages (Perl,
Smalltalk, Eiffel, Ada, and Lisp) to create a balanced language that is useful in the field of imperative
programming.
A few features of Ruby are as follows:
1. Object-oriented – Ruby is an object-oriented programming language; therefore, every facet of a
program may be given their own properties, actions, and functions through the use of instance
variables and methods. In a sense, Ruby is reminiscent of Smalltalk as Ruby gives methods and
instance variables to all of its types where this promotes ease of use.
2. Flexibility – Ruby is considered to be a flexible language as users can freely modify its parts
wherein essential parts may be modified or redefined as long as the user wants to do so, thereby
giving the coder more freedom as he works on his program.
3. Blocks – With Ruby’s block feature, the programmer can enclose any method while also providing
a description of how a method should act.
4. Mixin – Ruby functions on a single-inheritance concept, but even if it only uses a single-inheritance
concept, Ruby’s facility called mixin allows modules to access instance methods of another one
using include method.
5. Visual Appearance – Ruby makes use of an easy-to-read syntax with very limited punctation and
English keywords. No variable declarations can be found in Ruby as it uses simple naming
conventions to define the scope of variables as shown in the example below.

With this feature, programmers will be able to find it easier to identify the different variables and
their roles in the program, removing the need to use self. on every instance member.
Some other features include exception handling that is also present in Java and Python, garbage collection,
C extensions, extension libraries, OS independent threading, and portability.
RUBY’S SYNTAX AND FEATURES
a) In Ruby, variable types are automatically detected. Therefore, you do not need to state what kind
of data type the variable you’re declaring is. You simply need to type it out and Ruby will do the
work for you. For displaying output, we use #{ } in Ruby. This method allows you to print almost
any kind of data type in Ruby. Mathematical operations in Ruby are the same as they are in other
programming languages.
For example:

b) In Ruby, a variable’s data type does not need to be declared because Ruby automatically detects
the variable’s data type once its value has been put in. The data types native to Ruby are the
following:
 Integers – Ruby considers any number without decimals as an integer.
 Floats – when an integer is added to a floating point, the resulting value would be a floating
point as demonstrated in the short program provided below.
Example for both floating points and integers:

 Boolean – these are truth values that Ruby represents as either true or false.

 Strings – to define a String variable, provided below is an example of Ruby’s syntax for String
variables.
 Arrays – in order to define or declare an array, you must enclose the values in a pair of
brackets [ ], where each value is separated by a comma (,).

 Hashes - A hash is a dictionary-like collection of keys and values. They serve as a way to store
and access data in Ruby wherein they often contain data related to each other.
 Symbols – it is a special and immutable data type that acts like a label or an identifier in a
Ruby program. Symbols in Ruby are used to denote an important item/element that you can
use everywhere in your program as when you reference a symbol multiple times, you
reference the same object everywhere, referencing the same memory location. Strings, on the
other hand, are used for text you work on. Each string in Ruby is unique.
Example for hashes and symbols:

c) Printing of Strings (Note: Ruby treats newline (\n) characters and semicolons as the end of a line)
puts 2+3; # prints the sum of 2 and 3 on the screen
print "Jimmy" # prints the String Jimmy without beginning a new line
puts "Jimmy" # prints the String Jimmy on screen

OUTPUT:

d) Beginning and ending blocks of code


BEGIN
{
# this executes the code at the beginning, before the program runs
}

END
{
# this executes the code at the end of the program
}

As seen above, the code within the begin block runs before the main body which is denoted by the
puts “This is main body of program” while the end block runs when everything else has completed
execution.
e) Comments
# this is a comment
=begin
this is how you
make a multi-line
comment
=end 

REFERENCES:
DigitalOcean. (2020, November 26). Understanding Data Types in Ruby. Retrieved December 21, 2020,
from https://www.digitalocean.com/community/tutorials/understanding-data-types-in-ruby
Ruby For Beginners. (2018, July 30). Retrieved December 21, 2020, from
https://www.geeksforgeeks.org/ruby-for-beginners/?ref=lbp
Compiler vs Interpreter. (2020, June 08). Retrieved December 21, 2020, from
https://www.geeksforgeeks.org/compiler-vs-interpreter-2/
Ruby. (n.d.). Retrieved December 21, 2020, from https://www.ruby-lang.org/en/

You might also like