Guides

Learn Ruby | part 1

Started by lemonDragon · Jul 12, 2025

#12614
🧵Strings🧵


slang = "stay frosty"
slang.capitalize! # => "Stay frosty"


See in the code above that the 's' in stay is now capitalized. If the

's' was already capitalize, it would return nil.

Now if we have input that has both uppercase and lowercase characters we can use downcase. as seen in the below code.


lyrics = "I used to bitE my tongue and holD my BreatH"
puts lyrics.downcase


The code above will make lyrics like this:

i used to bite my tongue and hold my breath

Like the downcase, we can also make input all upercase.


lyrics = "Do you ever feel like a plastic bag"
puts lyrics.upcase


The code above will make the lyrics variable contents look like this:

DO YOU EVER FEEL LIKE A PLASTIC BAG

🟰EQUAL🟰

Like almost anything in Ruby, there are two ways of checking if equal. If the equal is true then

nothing has to be done. Like see the example bellow:


msg = "hi"
if msg.eql?("hi")
puts "MSG is equal"
else
puts "MSG is not EQUAL"
end



The variable msg IS equal true so the code will print "MSG is equal. "

The other way of doing this can be seen below:


msg = "HI"
if msg == "HI"
puts "MSG IS HI"
else
puts "MSG is not HI"
end



The code above will print out:

MSG IS HI

Stripping text

Some times, usually when reading a file there are new lines ( \n ) or return character ( \r ) in the string that needs to be removed or it the code will say it is not equal. This is because Ruby sees the newline and return character as a characters.

The code below shows how we can remove or strip the unwanted characters from an string.


message = "I love my dog\n"
p message
p message.strip


The code above output looks like this:

"I love my dog\n"

"I love my dog"

See how the last line does not have the new line character.

The thing is, that both need to be strings. For example, take a look

at the code below.


message = "1"
if message == 1
puts "YESSSSS"
else
puts "NOOO :("
end


Since the 1 is a integer it will not match and

NOOO :(

will be shown.

You could add ".to_s" to 1 to convert the number into a string. If you need to convert a string to number you can use

".to_i"

If you need to print a variable you can use #{variable} to print a variable. You could also use a '+' sign to combine two

variables to print. The code below shows how these can be used.


name = "Thomas"
msg = "My name is "
puts msg + name
puts "#{msg}#{name}"


The output would look like:

My name is Thomas

My name is Thomas

If statements

If statements work like this: If had is red; then allow person to enter room.

An example of an if statement can be seen below:


wearing_hat = true

if wearing_hat
puts "Do not allow them to enter restaurant"
else
puts "Allow them to enter restaurant"
end



The code above will print as if "wearing_hat" is true it will always trigger the first if statement. :

Do not allow them to enter restaurant"

Now let's change the wearing_hat variable to false.


wearing_hat = false

if wearing_hat
puts "Do not allow them to enter restaurant"
else
puts "Allow them to enter restaurant"
end


The code will print:

"Allow them to enter restaurant"

Ruby also has case statements which is similar to if statements.


print("Enter color of shoes")
shoe_color = gets.chomp

case shoe_color.to_s
when "red"
puts "Not allowed admittance"
when "black"
puts "Admittance allowed"
when "grey"
puts "Admittance allowed"
when "yellow"
puts "Not allowed admittance"
end


Let's break down the code above a little. First the code will print a message to terminal. The next part, show_color = gets.chomp gets and defines the input as the 'shoe_color' variable.

Before using "when" you have to put the keyword "case" and then the variable. Next it will decide if shoe_color matches the four different colors. If it matches the certain color it run the code in between the when. In this case it will print out if the person is or not allowed to enter the restaurant.

💎Install gems💎

In Python, packages are downloaded via the command "pip3 install package_name". While in ruby packages are called gems. Anyone

can create gems. They are pre package code that can be 'required' in code to add functionality to your code.

The code below will how to install a gem named httparty. The main place where you should install gems are from rubygems dot org. If you ever created a gem, you could upload it to rubygems. Also you can set up your own server to host gems locally. This is often used by companies who want to self host gems they created and not make the gems public.


gem install httparty


To uninstall the gem use the following commands:


gem uninstall httparty


✨Arrays✨

As seen in the code below, an array should be encased by []. Each comma in an array is called elelments


pets_name = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]
puts pets_name


In Ruby arrays start at '0'. For example, inside the pets_name array, "Fluffy" element is considered 0.

In the code below, I will show you how to enumerate an array. This means accessing all the elements.


pets_name = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]

pets_name.each do |e|
puts e
end



The code above will enumerate the array and print the element into the terminal. The 'e' part could be any word or letter

without a space in it.

You can also access an array directly as seen in the code snippet below.


[ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ].each do |name|
puts name
end


Like I mentioned before, arrays start at 0. The code below shows how you can

access a certain element of an array. The enumerate must end with "end"


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]

puts pet_names[0]
puts pet_names[1]


Now, lets say we want to check the array to see if a certain string is inside the array. We can use the method below.


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]

if pet_names.include?("Teddy")
puts "Teddy is in array"
else
puts "Teddy is not in array"
end


The code above will check to see if the word "Teddy" is in the array. The array name should

be first and the string that you want to search should be after the '?'. Note that if you change "Teddy" to "teddy" it will

not match the element.

Getting array length

Sometimes you need to know the length of an array so you can access the elements.


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ].length
puts pet_names.length
puts pet_names.count


There are two ways to do that, the length and count.

⏪Add to array⏪

Once again, there are couple of different ways of adding an array.

I use this method a lot in my programs.


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]
pet_names << "Bill"
puts pe_names


The code above will add the element "Bill" to the end of the array. The other way to add an element to the

end of the array is using the "push" method.


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]
pet_names.push("Bill")
puts pet_names


Now let's say you want to add to the front of an array. The 'unshift' method can do just that!


pet_names = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]
pet_names.unshift("Dave")
p pet_names


After running the code above, the output would look like this:

["Dave", "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy"]

The "insert" method is another way that you can add a new element to a certain position inside the array. Check out the

code below to see how.


busted_markets = [ "DarkMarket", "DeepSea", "Dream Market", "Hansa", "AlphaBay", "Silk Road 2.0", "Silk Road"]
busted_markets.insert(0, 'Archetyp Market')
p busted_markets


The code above will insert the market "Archetype Market" into the first element of the array. The results of

the code can be seen here:

["Archetyp Market", "DarkMarket", "DeepSea", "Dream Market", "Hansa", "AlphaBay", "Silk Road 2.0", "Silk Road"]

❌Removing Elements❌


busted_markets = [ "DarkMarket", "DeepSea", "Dream Market", "Hansa", "AlphaBay", "Silk Road 2.0", "Silk Road", "WhiterHouseMarket"]
busted_markets.pop


WHM did not get seized so we pop'd it which removes the element from the array. The 'pop' method removes the last element of the array.

Deleting at a certain Index

This will delete an element at a certain index. The code below uses a couple of things we already talked about, the length


busted_markets = [ "DarkMarket", "DeepSea", "Dream Market", "Hansa", "AlphaBay", "Silk Road 2.0", "Silk Road", "WhiteHouseMarket"]

busted_markets.delete_at(busted_markets.count - 1)
p busted_markets


Since the arrays start at 0, we have to minus one the total count of elements of the array.

If you want to delete a certain element that you do not know the index. You use the delete method

to delete by element contents. See the code below.


busted_markets = [ "DarkMarket", "DeepSea", "Dream Market", "Hansa", "AlphaBay", "Silk Road 2.0", "Silk Road", "WhiteHouseMarket"]
busted_markets.delete("WhiteHouseMarket")
p busted_markets


Remove Duplicates


tv_shows = [ "the office", "Barry", "Monk", "the office", "The rookie", "Mr. Robots", "Monk", "Shameless", "Breaking Bad"]
p tv_shows.uniq


This will remove all duplicates from the array.

The output will be the following: ["the office", "Barry", "Monk", "The rookie", "Mr. Robots", "Shameless", "Breaking Bad"]
#12615
↳ Replying to @lemonDragon
busted_markets = [ "Fluffy", "Charlie", "Nathan", "Daisy", "Frosty", "Teddy" ]
#12617
↳ Replying to @Paxtonysiu
I don't know but for sure am going to need a tutor.