What is Ruby? 💎
Ruby is a programming language that appeared in 1995 and quickly became a favorite of developers due to its simplicity and beauty. Its motto is "A programmer should enjoy the code." If you like clean and understandable code, Ruby is a great choice! 🚀
This language is actively used for web development, task automation, game creation, and even writing bots. The most famous web technology on Ruby is Ruby on Rails framework, which allows you to quickly create powerful websites and web applications. 🌍
How to start programming in Ruby? 💻
To work with Ruby, you will need:
✅ Install Ruby. You can download it from official website.
✅ Text editor, for example Visual Studio Code or Sublime Text.
✅ Launching the console. In the terminal (or command line), you can write and test Ruby code.
How to check if Ruby is installed? 🛠
Open the terminal and enter:
ruby -vIf Ruby is installed, you will see its version, for example:
ruby 3.1.2
Ruby's main features 🔧
Simplicity: Ruby has a clear syntax, similar to plain English.
Flexibility: You can easily change the code and add new features.
Object-orientedness: Everything in Ruby is an object!
High performance: Ruby is great for automation and working with text.
Ruby Syntax Basics 📚
1. Variables 📦
In Ruby, you don't need to specify the type of a variable — the language itself determines that it is a number, string, or other object.
Example of a numeric variable:
age = 25Example of a string variable:
name = "Alice"2. Data input and output 📤📥
In Ruby, you can easily display text on the screen using puts, and receive data from the user through gets.
Example of display:
puts "Hello, world!"Example of data entry:
print "Enter your name: "
name = gets.chomp
puts "Hi #{name}!"3. Conditional operators (if...else) 🧐
Using conditions, you can check data and change the behavior of the program.
Example:
if age >= 18
puts "You're an adult!"
else
puts "You're still a child."
end4. Loops (while, times, each) 🔄
Ruby supports several types of loops. For example, the times loop performs an action several times.
Example: output of numbers from 1 to 5
5.times do |i|
puts i + 1
end5. Functions (methods) 📐
The methods allow you to structure the code and reuse it.
Example of a function for calculating the square of a number:
def square(x)
x * x
endIt is called like this:
result = square(5)
puts resultSimple projects in Ruby 💡
1. Addition calculator ➗
The program adds two numbers and displays the result.
num1 = 10
num2 = 5
result = num1 + num2
puts "Addition result: #{result}"2. Random number generator 🎲
Each time you start, a new number will appear.
puts rand(1..100)3. "Guess the number" game 🎯
The computer thinks of a number, and the player tries to guess it.
secret = rand(1..10)
guess = nil
puts "I thought of a number from 1 to 10. Try to guess!"
until guess == secret
print "Your option: "
guess = gets.to_i
puts "Not enough!" if guess < secret
puts "A lot!" if guess > secret
end
puts "Congratulations, you guessed it!"Conclusion 🎉
Ruby is a powerful but easy-to-learn programming language. We have covered the basics: variables, input/output, conditions, loops, and functions. Now you can write your first programs in Ruby! 🚀
Remember: programming is a skill that develops with practice. The more you code, the better! Good luck learning Ruby! 😊
