We can use Ruby to capitalize the first letter of a string by using the Ruby capitalize method. some_string = "how are you?" some_string.capitalize Let’s look at a simple example below. Let’s say we have a string and we simply want to capitalize the first letter in that string. To do this we can use […]
Ruby
Ruby include Method – Check if Item in Array
We can use the Ruby include? method to see if an item is contained in a given array. This is pretty straightforward. Here is the code below. some_array.include?("item") The include? method will return either true or false based on whether the item is found in the array. Let’s see a simple example of this below. […]
Ruby unshift Method – Add Items to Start of Array
In Ruby, to add items to the start of an array, we can simply use the Ruby unshift method. The unshift method will add an element to the start of the array. num_array = [1, 2, 3] num_array.unshift(4); print num_array #Output: [4, 1, 2, 3] We can also use the unshift method to add multiple […]
Ruby has_value Method – Check if Hash Has Value
The Ruby has_value? method simply checks to see if a hash has a specific value or not. If it does, true will be returned. If the value is not found, false will be returned. some_hash = {"first_name" => "David", "last_name" => "Smith"} puts some_hash.has_value? "David" #Output true The has_value? method is pretty straightforward but can […]
Ruby has_key Method – Check if Hash Has Key
The Ruby has_key? method simply checks to see if a hash has a specific key or not. If it does, true will be returned. If the key is not found, false will be returned. some_hash = {"first_name" => "David", "last_name" => "Smith"} puts some_hash.has_key? "first_name" #Output true The has_key? method is pretty straightforward but can […]
Using Ruby to Create List of Prime Numbers
In Ruby, we can create an array of prime numbers easily – all we need is a custom method to check if a number is prime or not. To generate an array of the first N prime numbers in Ruby, we can create our own method and loop until we have N prime numbers. So […]
Ruby Infinite Loop
A Ruby infinite loop is one of the most dreaded errors when coding in Ruby because the code inside the loop will just run forever. This can be dangerous if there is a lot of code inside the loop, as it will use a ton of your computer’s resources and usually cause a crash. Here […]
Ruby puts vs print
There are many similarities in Ruby of puts vs print commands. They both basically carry out the same function of printing to the console. There are two main differences between puts vs print that we will focus on. In short, puts will add a newline character at the end, so that all printed calls are […]
Using Ruby to Convert String to Boolean
We can use Ruby to convert a string to boolean with the help of the empty? method. We first need to determine if the string is empty or non-empty. Non-empty strings will be converted to true and empty strings will be converted to false. some_string = "This is a string" empty_string = "" some_string = […]
Using Ruby to Capitalize First Letter of Each Word
In Ruby, we can capitalize the first letter of every word in a string easily with the help of the split and map methods, and the capitalize method. Let’s see the code for this first, and then go over what is happening. some_string = "this is a string with some words" words_array = some_string.split(" ") […]
How to Square a Number in Ruby
To square a number in Ruby, the easiest way is to multiply the number by itself. square_num = num*num Here is a simple example using the code above. square_of_10 = 10*10 print square_of_10 #Output 100 We can also use the Ruby pow method to square a number. square_num = num.pow(2) Using the pow method with […]
Get the Sum of an Array in Ruby
There are many ways to calculate the sum of an array of numbers in Ruby. The easiest way is by using the sum method. nums_array = [1,2,3,4,5,6,7] print nums_array.sum #Output: 28 This is definitely the easiest way if dealing with a simple array of numbers. There are a bunch of other ways to sum an […]
Ruby Round – Round Number to Nearest Integer with the round Method
We can use Ruby to round a number to the nearest integer by using the round method. The round Ruby method rounds a number to the nearest integer. 4.653.round The above code would return 5, as it would round the number 4.653 to the nearest integer, 5. Note that we can also pass the round […]
Ruby Ceiling – Find the Ceiling of a Number with the ceil Method
We can use Ruby to find the ceiling of a number by using the ceil method. The ceil Ruby method rounds a number up to the nearest integer. 4.653.ceil The above code would return 5, as it would round the number 4.653 up to 5. Note that we can also pass the ceil method an […]
Ruby Floor – Find Floor of Number with floor Method
We can use Ruby to find the floor of a number by using the floor method. The floor Ruby method rounds a number down to the nearest integer. 4.653.floor The above code would return 4, as it would round the number 4.653 down to 4. Note that we can also pass the floor method an […]
Using Ruby to Convert Array to String
In Ruby, to convert an array to a string, we can use the join method. We can use the join method without any arguments or pass one that will act as our string delimiter. some_array.join Let’s see this with a full-code example. some_array = ["red","blue","green","yellow","pink","black"] some_array = some_array.join print some_array #Output redbluegreenyellowpinkblack Remember the p […]
Ruby String Comparison
There are several ways we can compare strings in Ruby. Four ways we will look at to do this are using the == equality operator, eql? method, equal? method, and the spaceship operator. Let’s take a look at each one individually first, and then we will show some examples of all of them at the […]
Using Ruby to Get the Length of Array
There are many ways we can use Ruby to get the length of an array. We can use the length method, the size method, or the count method to get the length of an array. Let’s take a look at using each one. Let’s take a look at the length method first. arr.length And here […]
Using Ruby to Print to Console
There are 3 main ways we can use Ruby to print to the console. We can do this by using either the print, puts, or p commands. Let’s take a look at each one. Let’s first look at printing to the console using the print command, with a very simple example. some_string = "This is […]
How to Print With Newline in Ruby
To print with a newline in Ruby, we simply need to use the puts command instead of print. Let’s take a look at each below. Here is how the puts command works. number_one = "one" number_two = "two" number_three = "three" number_four = "four" number_five = "five" puts number_one puts number_two puts number_three puts number_four […]
How to Print Without Newline in Ruby
To print without a newline in Ruby, we simply need to use the print command instead of puts. Let’s take a look at each below. Here is how the print command works. number_one = 1 number_two = 2 number_three = 3 number_four = 4 number_five = 5 print number_one print number_two print number_three print number_four […]
Using Ruby to Get the Length of a String
We can use Ruby to get the length of a String by using either the String length method or the size method. The length method will return the length of the string, and if the string is empty, it will return 0. Let’s take a look at the length method first. string_length = "This is […]
Using Ruby to Reverse String
We can use Ruby to reverse a string by making use of the String reverse method. "This is a string".reverse And here is the full code to complete the example. some_string = "This is a string" some_string = some_string.reverse print some_string #Output gnirts a si sihT When using string objects in Ruby, we can easily […]
Using Ruby to Convert String to Array
In Ruby, we can convert a string to an array by using the split() method. some_string = "string" some_string = some_string.split("") print some_string #Output: ["s", "t", "r", "i", "n", "g"] If we want to convert a string with spaces to an array and have each element in the array be separated by spaces, we just […]