Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Basic Syntax
3.
Variables and Data Types
4.
Control Structures
4.1.
Ruby
5.
Methods
5.1.
Ruby
6.
Classes and Objects
6.1.
Ruby
7.
Frequently Asked Questions
7.1.
Do I need to declare variable types in Ruby?
7.2.
What does the @ symbol mean in Ruby?
7.3.
How does Ruby handle errors?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Ruby Syntax

Introduction

Ruby is a high-level, dynamic, and object-oriented programming language designed to prioritize simplicity and productivity. It's known for its elegant syntax, which is easy to write and naturally reads like English, making it a great choice for beginners. This article aims to provide a comprehensive introduction to Ruby syntax for someone who is completely new to this language.

Ruby Syntax

Basic Syntax

Ruby has a very clean syntax that allows developers to do more work with less code. A "Hello, World!" program in Ruby is as simple as:

puts "Hello, World!"

 

The puts method in Ruby is used to output text or other information to the console.

Variables and Data Types

Ruby supports several data types, including numbers, strings, arrays, and hashes. Defining a variable doesn't require a specific type. Here's an example:

name = "John Doe"
age = 30

 

In this code, name is a string and age is an integer. Ruby dynamically assigns the data type based on the value the variable holds.

Control Structures

Like most programming languages, Ruby has control structures such as if, else, while, and for.

  • Ruby

Ruby

age = 18

if age >= 18

  puts "You are eligible to vote."

else

  puts "You are not eligible to vote."

end
You can also try this code with Online Ruby Compiler
Run Code

 

This code will output "You are eligible to vote." since the age is equal to 18.

Methods

Methods in Ruby can be defined using the def keyword. Here's an example:

  • Ruby

Ruby

def greet(name)
  puts "Hello, #{name}!"
end
greet("Alice")
You can also try this code with Online Ruby Compiler
Run Code

 

This will output: "Hello, Alice!". The #{name} syntax is used to interpolate the variable name into the string.

Classes and Objects

Ruby is an object-oriented language, so you can define classes and create objects of those classes.

  • Ruby

Ruby

class Person
  def initialize(name)
    @name = name
  end


  def greet
    puts "Hello, #{@name}!"
  end
end


person = Person.new("Bob")
person.greet
You can also try this code with Online Ruby Compiler
Run Code

 

This code defines a Person class with a constructor and a greet method, then creates an object of that class and calls the method.

Frequently Asked Questions

Do I need to declare variable types in Ruby?

No, Ruby is dynamically typed. The interpreter determines the type of the variable at runtime.

What does the @ symbol mean in Ruby?

The @ symbol denotes instance variables in Ruby. They hold values that are accessible within an instance of a class.

How does Ruby handle errors?

Ruby handles errors using a system of exceptions and rescue clauses to catch and handle these exceptions.

Conclusion

Ruby's syntax is known for its elegance and simplicity, designed to make the code more readable and understandable. From basic constructs to variables, control structures, methods, and classes, Ruby offers a broad set of tools with an easy-to-understand syntax. It's a great language for beginners and a joy for experienced developers. Happy coding with Ruby
 

Live masterclass