Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 624 of 3363
690 Views
Methods in Ruby, as in other programming languages, bundle one or more repeatable statements into a single unit. Method names in Ruby should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.Methods should be defined before calling them, otherwise Ruby will raise an exception for invoking an undefined method.There are different ways in which we can invoke methods in Ruby. When we are invoking a method in Ruby, the use of the parentheses is optional and hence they can ... Read More
311 Views
There are several methods in Ruby that can be used on numbers. In this article, we will explore some of these useful methods and how to use them.number.even in RubyThis method is used when we want to check if a particular number is even or not. Consider the code shown below −ExampleConsider the code shown below.num = 14 puts num.even? num = 19 puts num.even?OutputIt will produce the following output.true falsenumber.odd in RubyThis method is used when we want to check if a particular number is odd or not. Consider the code shown below −ExampleConsider the code shown below.num ... Read More
330 Views
The Float class in Ruby is a subclass of the Numeric class. Its objects are the representations of real numbers, using the native architecture representation of floating-point numbers.Let's consider the different methods that are available in the float class in Ruby.== Method in RubyThe == method is used when we want to return True, if two objects are equal.ExampleConsider the code shown below.puts 3.7 == 4 puts 3.7 == 3.7OutputIt will produce the following output.false trueabs Method in RubyThe abs method is used when we want to return the absolute value of a float.ExampleConsider the code shown below.puts (-50.56).abs puts ... Read More
5K+ Views
Ruby provides us with different methods that we can use to handle files. In simple terms, file handling involves different processes such as creating a new file, reading the contents present in a file, writing some content to a file, appending content to a file, deleting a file and more.There are different modes that one can use to do file handling in Ruby. These are −r = Read-only moder+ = Read-Write modew = Write-mode onlyw+ = Read-write modeAnd some more. These four mentioned modes are the most commonly used ones, when it comes to file handling in Ruby. In this article, ... Read More
220 Views
Exceptions in programming are errors that occur at runtime. They are hard to handle in an effective manner, but it is important to handle them, else they halt the program execution.Some of the common exceptions that one can encounter are −trying to open a file that is not there, dividing the number with zero, running out of memory etc.Now let's take a couple of examples to demonstrate how exceptions halt the execution of a Ruby program.Example 1Consider the code shown below.taking two integer value $First = 10; $Second = 0; # divide by zero error $Third = $First / ... Read More
429 Views
In this article, we will explore some useful string functions in Ruby that are widely used to format data.Get the Length of the StringTo find the length of the string, we will use the size method.ExampleConsider the code shown below.str = "TutorialsPoint" puts str.sizeOutputIt will produce the following output.14 To Check Empty stringTo check if a string is empty, we use the size method and comparison operator.ExampleConsider the code shown below.str = "TutorialsPoint" puts str.size == 0OutputIt will produce the following output.false To Extract a Substring from StringTo extract a substring from a string, we will use square bracket notation.ExampleConsider ... Read More
1K+ Views
Encapsulation is the ability to wrap the data into a single unit. In simple terms, it's a mechanism to wrap data and the code that manipulates the data. In Ruby, we can achieve encapsulation with the help of classes.Let's consider a very simple example where we will implement encapsulation.Example 1Consider the code shown belowclass Document attr_accessor :name def initialize(name) @name = name end def set_name(name) @name = name end end d = Document.new('TP') d.set_name('TutorialsPoint') puts d.nameOutputIt will produce the following output −TutorialsPointExample 2Let's consider one ... Read More
1K+ Views
Abstraction is an object-oriented programming concept, where the essential attributes of something are shown and all the unnecessary ones are hidden. With this approach, one can hide the implementation details and provide only the important interface.A very good example of abstraction is your car. It's a great example of abstraction. When you start the car by turning the key or pressing the start button, you don't necessarily need to know how the engine works or how it gets started, also what components are present in that car or what else is there. These details that are irrelevant to the driver ... Read More
276 Views
In addition to loops, conditionals, and iterators, Ruby has some statements that are used to change the control flow in a program. In other words, these statements are pieces of code that execute one after the other until a condition is met.In this article, we will explore the following control flow alterations in Ruby −break statementnext statementredo statementretry statementLet's consider each of these one by one.break statementWhen a condition is True in Ruby, the break statement terminates a loop.ExampleConsider the code shown below.# break statement example itr = 1 while true if itr * 6 >= 35 ... Read More
2K+ Views
break Statement in RubyIn Ruby, we use the break statement in order to make sure that we exit a certain loop after a condition. For example, suppose we want to print the numbers from 1 to 10, but once we hit the number 5, we just don't want the loop to print any numbers that come after it. In such a case, we can use the break statement.Example 1Let's take an example and understand how the break statement works in Ruby. Consider the code shown below.# break statement in Ruby #!/usr/bin/ruby -w itr = 1 # while Loop ... Read More