Ruby Tutorial
Ruby CGI
Ruby Advanced
Let's dive into a tutorial on how to work with variables in Ruby.
1. Creating Variables
In Ruby, you can assign a value to a variable using the =
operator. Ruby is a dynamically-typed language, which means you don't have to specify the type of data that a variable will hold.
x = 10 name = "Alice" is_raining = false
2. Naming Variables
Ruby uses snake_case for variable naming. Variables should start with a lowercase letter or an underscore, and can include alphanumeric characters and underscores.
my_variable = "Hello, world!" another_variable = 42 _this_is_also_a_variable = true
3. Variable Scope
Variables in Ruby have different types of scope depending on how they are declared:
def say_hello greeting = "Hello, world!" # local variable puts greeting end say_hello # prints "Hello, world!" puts greeting # raises an error because greeting is not in scope
@
symbol.class Person def initialize(name) @name = name # instance variable end def greet puts "Hello, my name is #{@name}" end end alice = Person.new("Alice") alice.greet # prints "Hello, my name is Alice"
@@
.class Dog @@number_of_dogs = 0 # class variable def initialize @@number_of_dogs += 1 end def self.number_of_dogs @@number_of_dogs end end Dog.new Dog.new puts Dog.number_of_dogs # prints "2"
$
symbol, but their use is discouraged as they can make debugging difficult.$global_variable = "Hello, world!" # global variable
4. Constants
Constants in Ruby are meant to hold values that shouldn't be changed. They are declared using uppercase letters. However, Ruby will only issue a warning and not an error if you change a constant.
PI = 3.14159
5. Parallel Assignment
Ruby allows parallel assignment, which lets you assign values to multiple variables at once:
x, y, z = 10, 20, 30
This is a basic introduction to variables in Ruby. Variables are fundamental to any programming language and are used to hold data that can be manipulated by the program.
Declaring variables in Ruby: Variables are created by assigning values.
name = "Ruby"
Variable naming conventions in Ruby: Follow conventions like using snake_case for variable names.
user_age = 25
Local variables in Ruby: Local variables are confined to the current scope.
local_variable = "I am local"
Instance variables in Ruby: Instance variables belong to an instance of a class.
@instance_variable = "I belong to an instance"
Class variables in Ruby: Class variables are shared among all instances of a class.
@@class_variable = "I am shared among instances"
Global variables in Ruby: Global variables are accessible throughout the program.
$global_variable = "I am accessible everywhere"
Constants in Ruby: Constants are declared using uppercase names.
MY_CONSTANT = 42
Variable scope in Ruby: Scope defines where a variable is accessible.
def method_example local_variable = "I am local to this method" end
Shadowing variables in Ruby: Be cautious with variable shadowing.
x = 10 5.times do |x| puts "Inside the block, x is #{x}" end puts "Outside the block, x is #{x}"
Destructuring assignment in Ruby: Assign multiple variables in a single line.
x, y, z = 1, 2, 3
Ruby variable types: Ruby variables are dynamically typed.
variable = 42 variable = "Now I am a string"
Using variables in Ruby expressions: Include variables in expressions.
x = 5 y = 10 sum = x + y
Initializing variables in Ruby: Assign values when declaring variables.
initialized_variable = "I am initialized"