DEV Community

Jr Carreiro
Jr Carreiro

Posted on

๐Ÿ”ต Chapter 02 โ€“ Ruby Language Fundamentals (Line by Line for Absolute Beginners)

Welcome to Chapter 02 of my learning journey through scripting and offensive security.

Iโ€™m not just learning to code โ€” Iโ€™m learning to build, document, and explain.
This series is based on Hacking with Go, but reimagined in Ruby and Swift, with step-by-step examples and real-world analogies to help anyone understand โ€” even if youโ€™ve never written a single line of code.


๐Ÿงฑ What weโ€™ll cover in this chapter:

  • ๐Ÿ“ฆ Variables (how to store information)
  • ๐Ÿ” Loops (how to repeat actions)
  • ๐Ÿ”€ Conditionals (how to make decisions)
  • ๐Ÿง  Functions (how to reuse logic)
  • ๐Ÿ’ฌ Input/Output (how to talk to the user)

๐Ÿ’ก Variables โ€“ Giving names to values

name = "Alice"age = 30
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

Weโ€™re storing data in memory using variables.

  • name holds the string "Alice"
  • age holds the number 30

Think of variables like stickers you put on boxes โ€” the name tells you whatโ€™s inside.


๐Ÿ” Loops โ€“ Repeating things automatically

3.times do  puts "Knock knock!"end
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

  • 3.times runs the block three times
  • puts prints the message

This is like telling a kid: โ€œSay โ€˜knock knock!โ€™ three timesโ€ โ€” and theyโ€™ll do it without question.


๐Ÿ”€ Conditionals โ€“ Making decisions

password = "swordfish"if password == "swordfish"  puts "Access granted!"else  puts "Access denied!"end
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

We check a condition:
If the password matches, we let the person in. Otherwise, we block them.

Itโ€™s like checking someoneโ€™s ID before opening the door.


๐Ÿง  Methods โ€“ Reusable blocks of logic

def greet(name)
  "Hello, #{name}!"endputs greet("Alice")
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

Weโ€™re defining a method (like a mini-program) that:

  • Takes one input: name
  • Returns a message using string interpolation

Calling greet("Alice") is like pushing a button labeled โ€œSay hello to Alice.โ€


๐Ÿ’ฌ User Input โ€“ Interacting with people

print "What's your name? "name = gets.chompputs "Welcome, #{name}!"
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Explanation

  • print shows a prompt without a newline
  • gets waits for input from the user
  • chomp removes the trailing newline

This is how Ruby talks to the user โ€” and listens back.


๐Ÿง  Final Recap

๐Ÿ“ฆ Variables โ†’ Store information with names
๐Ÿ” Loops โ†’ Repeat things automatically
๐Ÿ”€ Conditionals โ†’ Make decisions
๐Ÿง  Methods โ†’ Reuse blocks of logic
๐Ÿ’ฌ Input/Output โ†’ Talk to the user


โœ… Youโ€™ve learned the building blocks

This chapter gave you everything you need to start writing real scripts.

Next up: weโ€™ll dive into useful packages and Ruby gems to do powerful things like parsing files, automating tasks, and working with the web.

๐Ÿ“ซ About the Author

[Jรบnior Carreiro]
๐Ÿ” Mobile AppSec | iOS Security | Reverse Engineering
๐Ÿ“ Let's connect: [GitHub] ยท [Linkedin]

Top comments (0)