Beginner's guide to ruby

Introduction

Ruby is a dynamic, open-source, object-oriented programming language developed by Yukihiro Matsumoto in the mid-1990s. Known for its elegant syntax, it's widely used for web development, especially with the Ruby on Rails framework.

Basic Syntax

Here's a simple 'Hello, World!' program in Ruby:


  puts "Hello, World!"
  

Variables and Data Types

Variables in Ruby can hold data of any type:


  age = 25
  salary = 50000.50
  letter = 'A'
  name = "John"
  is_happy = true
  

Conditional Statements

Make decisions in code based on conditions:


  if age > 20
      puts "You are older than 20."
  else
      puts "You are 20 or younger."
  end
  

Loops

Execute repetitive tasks using loops:


  5.times do |i|
      puts i
  end

  count = 0
  while count < 5
      puts count
      count += 1
  end
  

Methods

Methods in Ruby are like functions in other languages:


  def multiply(a, b)
      a * b
  end

  result = multiply(5, 2)
  puts "Result: #{result}"  # Outputs 10
  

Conclusion

This brief overview introduces you to the elegance and simplicity of Ruby. While the language has much more to offer, this guide provides a solid foundation to start your journey. To go further, explore topics like blocks, procs, lambdas, and the rich RubyGems ecosystem.