{

Learn free ruby tutorials


Difference between put and println in Ruby with examples

December 1, 2022 ·  1 min read

As you know print and puts are used to display data to console. put and print are functions defined in io class in Ruby. These objects print the arguments to console and return nil What is difference between puts and print, when to use print and puts? Difference between put and print puts and prints function used to print an object to console. In case of Singe arguments to this two functions, it adds the same output....


How to check a string contains a substring in Ruby with examples

December 1, 2022 ·  1 min read

This tutorial explains about how to convert a String into upper case or lower case How to Check a String contains a substring in Ruby? There are multiple ways we can check a Substring contains in a String Ruby use includes method ruby provides String include? method that checks given string exists in a string or not. This checks for exact case and returns boolean value. For example....


How to check if element exists in hash or not in Ruby with examples

December 1, 2022 ·  1 min read

Array contains key and value pairs, Sometimes we want to check if a given key exists, resulting in a boolean values checks against conditional statements such as if-else. Check Key exists in a hash or not There are multiple ways we can check key exists in a hash. use the hash key? method key? method checks hash for key exists or not, return true or false. Here is an example program...


How to check if an element exists in Array or not in Ruby with examples

December 1, 2022 ·  2 min read

The array contains a collection of elements, Sometimes we want to check if a given element exists in an array, resulting in a boolean values checks against conditional statements such as if-else. How to check if an element exists in Array or not in Ruby with examples There are multiple ways we can check key element exists in an array. use the include? method include? method checks element exists in an array, return true or false....


How to check if the variable is defined in Ruby with examples

December 1, 2022 ·  1 min read

This tutorial explains about the variable is defined and initialized with a value. In Ruby language, the Variable is declared and assigned with values. Check if the variable is defined in Ruby Ruby provides defined method. It checks an expression to check whether it is a variable or assignment or expression or method. If it is not unable to resolve, returns empty or nil. number = 11 puts defined?(number)## returns local-variable obj = {} puts defined?...


How to check the type of a variable is Ruby| Ruby on Rails By Example

December 1, 2022 ·  3 min read

Sometimes, We want to check an object type programmatically to categorize or store it into specific variables. Java provides instanceOf, and Javascript provides typeOf methods to detect instance type. Ruby provides different ways and methods for instance or object or variable type at runtime. Sometimes, We want to check a variable type. The type can be an int or any object. Variable can be number, float, boolean, array, or hash....


How to convert Class or Hash Object to JSON Object Ruby| Ruby on Rails By Example

December 1, 2022 ·  1 min read

Sometimes, We want to convert Hash object to JSON object in Ruby language. Convert User-defined class objects into a JSON object in Ruby? First, create an class with fields defined two methods asJson - Convert the class into a hash object toJSON - function convert a hash object into JSOn object using json import json library using require call. Here is a code require 'json' class Employee attr_accessor :name, :id,:salary def as_json(options={}) { name: @name, id: @id, salary: @salary } end def toJson(*options) as_json(*options)....


How to Convert current Unix timestamp epoch to DateTime in Ruby Programming| Ruby on Rails by Example

December 1, 2022 ·  2 min read

This tutorials explains about how to convert currrent unix timestamp to Datetime in Ruby. You can check other about how to get current Epoch Timestamp There are multiple ways we can convert epoch timestamp to Date time. How to convert Unix Timestamp to Datetime in Ruby With example? Time library provides methods to get current timestmap. Time.now.to_i returns the current timestamp number. It is elapsed seconds since unix timestamp. require 'time' # Get Current timestamp with now timestamp=Time....


How to do sum of array numbers in Ruby with examples

December 1, 2022 ·  2 min read

This tutorial explains how to do the sum of array numbers in Ruby. Sum of array numbers Sum of numbers between a and b Ruby sum of array numbers with an example There are a number of ways we can calculate the sum of an array of numbers in Ruby sum method reduce method inject method sum method The sum method is only available in the Ruby 2....


How to Find the minimum and maximum values in an array and hash Ruby with examples

December 1, 2022 ·  2 min read

These tutorials show you how to find out the minimum and maximum values from an array. How to find the minimum and maximum values from a given ruby There are multiple ways used to find the min and max of a given array. One way, using min and max methods in the Ruby array Another way using the Enumerable minmax method Array.min: Returns Minimum value Array.max: Returns Maximum value Enumerable....


How to Generate Random number in Ruby with examples

December 1, 2022 ·  1 min read

This article explains about multiple examples how to generate random number. How to Generate Random Number in Ruby? rand method in used to generate the random number. rand() without argument always returns the random number between 0.0 and 1.0. rand() - returns .5 value puts rand() # return random number between 0.0 to 1.0 Here is an example It takes individual number or start and ending index to generate number....


How to get a ruby version in a project Ruby on Rails with examples

December 1, 2022 ·  1 min read

This short tutorial explains how to find the ruby version installed on a project. How to find the ruby version in a project? In Terminal, you can type the ruby --version command if ruby is installed. C:\Users\Kiran>ruby --version ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x64-mingw32] To know the ruby version in the interactive ruby terminal(IRB) console. C:\Users\abc>irb irb(main):001:0> RUBY_VERSION => "2.7.0" irb(main):002:0> In Ruby, You can use other global constants like the below in the Ruby on Rails application....


How to Get current Unix timestamp or milliseconds since epoch Ruby Programming| Ruby on Rails by Example

December 1, 2022 ·  1 min read

This article shows how to obtain the current timestamp, Unix timestamp, or epoch timestamp in the Ruby programming language. We’ll make use of Time class. The epoch timestamp, often known as the Unix timestamp, is a long integer in milliseconds that represents the time of day. It’s the number of milliseconds that have elapsed since 1970-01-01 PST. How to get Current Epoch Timestamp in Ruby Ruby provides the Time class to provide Date and Time-related functions....


How to get Random Number from an array in Ruby with examples

December 1, 2022 ·  3 min read

This tutorial shows, multiple ways to get a random number from an array. One way is using the sample method, another way using shuffle.first and another way using retrieve an array with index, where index value generated using rand function. You can get single or multiple random values from a given array. Generate Random Number with Array sample method An array contains the sample method which returns random values. It accepts optional arguments....


How to install and uninstall gem in Ruby Programming| Ruby on Rails by Example

December 1, 2022 ·  1 min read

This post explains about to install and uninstall gem with versions in the Ruby application. It also includes a specific range of versions Ruby gem install with a specific version gem install command installs the library. You can use either a specific version or version comparator Specific version using -v or --version You can include -v or --version to install a specific version gem install library -v versionnumber or gem install library --version versionnumber For example, you want to install the package with version 1....


How to pretty format JSON output in Ruby on Rails| Prettier JSON in Ruby with examples

December 1, 2022 ·  2 min read

Sometimes, We have a JSON string, and want to print it to console For example, json string is in the ruby object employee = '{ "id":"1", "name":"john", "age":"30" }' puts employee It prints in a single line without format and indentation. Output: { "id":"1", "name":"john", "age":"30" } This prints json objects in the same line without format. It is difficult to read the object. It is very easy to read If you print the json object in a nice format....


How to print the name of a Ruby class| Ruby on Rails By Example

December 1, 2022 ·  1 min read

Ruby contains classes and objects. Sometimes, We need to get the name of a class for a given class variable or object. How to get the name of a Ruby class? How to find the ruby class name with example Let’s declare an Employee class class Employee def initialize(id, name, salary) @id = id @name = name @salary = salary end end Create an object for this class. emp1 = Employee....


How to remove an element from an array in Ruby Programming| Ruby on Rails by Example

December 1, 2022 ·  2 min read

How to remove an element from an array in Ruby? There are multiple ways we can delete from an array in Ruby. array delete method array delete_at Minus operator delete_if remove an element from an array using the delete method Array delete method removes an element if found, else returns nil. Here is an example code array = [1, 2, 3, 4, 5, 6] array.delete(3) puts array Output:...


How to remove duplicate elements from an array in Ruby with examples

December 1, 2022 ·  1 min read

Arrays contain duplicate elements. Sometimes we want to remove duplicate elements. There are multiple ways to return unique elements in Ruby Array. uniq method to_set method How to return unique elements from an array in Ruby The first method is using the Array uniq method. Array provides a uniq method that returns an array of elements by removing duplicate elements. array = [8,1,7,1,5,3,4,8] array = array.uniq puts array The second way is using the Array....


How to remove white spaces in Ruby with examples| delete leading and trail blank spaces

December 1, 2022 ·  1 min read

This tutorial explains how to remove whitespaces from a given string in Ruby. How to strip whitespaces in a string? How do remove from leading and trailing of a string? How to remove white spaces at begin and end of a string String strip method removes the whitespaces from the start and end of a string. result=" welcome to my website ".strip puts "-#{result}-" Output: -welcome to my website- String lstrip method removes the whitespaces from starting a string....


Ruby How to convert a string into upper and lower case?

December 1, 2022 ·  2 min read

This tutorial explains about how to convert a String into upper case or lower case How to Convert a string into Lower case and uppercase in Ruby? Ruby provides following methods downcase upcase upcase: Convert the string into UPPER CASE Below example, convert the string into upper case and assign the value to variable. result="welcome".upcase puts "#{result}" Or Below example uses exclamation symbol(!...


Subscribe
You'll get a notification every time a post gets published here.