{

Learn free ruby tutorials


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

May 10, 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 element exists in hash or not in Ruby with examples

May 8, 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 Generate Random number in Ruby with examples

May 4, 2022 ·  1 min read

This tutorials explains about how to do sum of array numbers in Ruby? Ruby sum of array numbers There are number of ways we can calculate sum of array of numbers in Ruby sum method reduce method inject method sum method: sum method numbers = [1,2,3,4] result = numbers.sum puts "#{result}" result1=0 numbers.sum {|item| result1+=item } puts "#{result1}" result2=numbers.sum {|item| item } puts "#{result2}" Output 10 10 10 numbers = [1,2,3,4,5] result=numbers....


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

May 4, 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....


Difference between put and println in Ruby with examples

May 3, 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 remove white spaces in Ruby with examples| delete leading and trail blank spaces

May 2, 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?

May 2, 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(!...


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

May 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....


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