Lecture on Ruby and Ruby on Rails

Jul 3, 2024

Lecture on Ruby and Ruby on Rails

Introduction

  • Ruby: Programming language
  • Ruby on Rails: Framework built in Ruby

Why Use a Framework?

  • Ease for developers
  • Pre-built structures: Portability, aesthetics

Course Curriculum

  1. Learn Ruby language
  2. Ruby on Rails: Creating applications
  3. Rails certification

Ruby Language Basics

  • Interpreter-based language: reads instructions line-by-line
  • Object-oriented programming (OOP)
    • everything is an object
    • Classes and objects
    • Attributes and methods

Object-Oriented Programming in Ruby

  • Object: Instance of a class
  • Class: Blueprint for objects
  • Inheritance: Child class derives properties and methods from a parent class
  • Example: Class Car, subclass SUV
  • Variables Types: Local, instance, class, global
  • Instance Methods: Called on an object
  • Class Methods: Called on class name

Variables in Ruby

  • Global Variables: Accessible throughout the application ($var)
  • Local Variables: Accessible within a method or block (var)
  • Class Variables: Accessible within the class (@@var)
  • Instance Variables: Attributes for objects (@var)
  • Constants: Values that won’t change (UPPERCASE)

Methods in Ruby

  • Defining Methods: def, end
  • Instance Methods: Called on objects
  • Class Methods: Defined by class name
  • Scope of Variables: Local and class variables have limited scope
  • Instance Methods: Don’t provide scopes for instance variables

Control Structures

  • If Statement: Conditional execution
  • Unless Statement: Negative conditionals
  • Case Statement: Similar to switch-case
  • Loops: while, until, for, times, each

Working with Strings

  • Define strings using single ('') or double ("") quotes
  • String interpolation with double quotes
  • Important Methods: .length, str.upcase, str.downcase, .capitalize, .reverse
  • Destructive Methods: Append ! to modify the object permanently
  • RegEx Matching: =~, .match

Collections in Ruby

  • Arrays: Ordered list
  • Hashes: Key-value pairs
  • Ranges: Sequence of values (e.g., 1..10)
  • Iterating methods: .each, .map, .select, .reject

Ruby Tools

  • IRB: Interactive Ruby tool for running Ruby expressions
  • RVM: Ruby Version Manager