Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Numbers

Ruby Number

shape Introduction

This chapter demonstrates about the Ruby Number which represent many ways in Ruby and the most basic form is an integer and following are the concepts covered in this chapter.
  • nil
  • Operations

nil

shape Description

Numbers are represent in numerous ways in Ruby. The most fundamental type of a number is called an integer. Which is represented by the entire number only, with no decimal point. A more complex type of a number is known as a float. A float is a number which contains a decimal. The code below demonstrates the Numbers as shown below. [ruby] # Example of integers 1, 2, 3, 50, 10, 4345098098 # Example of floats 1.2345, 2345.4267, 98.2234 [/ruby] In programming, User require an approach to express "nothing", and in Ruby, user can do through something called nil. A variable with a value of nil could be portrayed as having "nothing" or being 'completely empty', or even just essentially 'not any specific type'. A circumstance where which output is expected but none is returned the code below demonstrates the nil as shown. [ruby] irb :001 > puts "Hello, World!" Hello, World! => nil [/ruby] In above code puts method prints a string and return nothing so user can see nil being returned after the string is displayed.

Operations

shape Description

Ruby have several operations to perform by using Ruby Number which can perform several basic operations, some operations are listed below. Adding, Subtracting and Multiplying User can perform Basic mathematical operations in Ruby are quite simple user need to add two integers together just by using the '+' operator as shown in below code. [ruby] irb :001 > 1 + 1 => 2 [/ruby] User can perform Basic mathematical operations in Ruby are quite simple user can subtract two integers together just by using the '-' operator as shown in below code. [ruby] irb :001 > 1 - 1 => 0 [/ruby] User can perform Basic mathematical operations in Ruby are quite simple user can multiply two integers together just by using the '*' operator as shown in below code. [ruby] irb :001 > 4 * 4 => 16 [/ruby] Division vs Modulus User cab divide the integers by using the '/' operator the code below demonstrates the Division as shown below. [ruby] irb :001 > 16 / 4 => 4 [/ruby] Ruby also have an operator called as modulo operator is represent by % symbol. Which can be referred to as the reminder also. The modulo operator gives the reminder of a division operation. The code below demonstrates the Modulus a shown below. [ruby] irb :001 > 16 % 4 => 0 [/ruby] Equality Comparison There are times when user need to check if the estimations of two objects are the same. To test the correspondence of two things user can utilize the == operator. Which analyzes the object on the left of the == with the object of the right and returns true or false any one. genuine and false are called Boolean values in most programming languages. The code below demonstrates the equality comparison. [ruby] irb :001 > 4 == 4 => true irb :002 > 4 == 5 => false [/ruby]

Summary

shape Key Points

  • In Ruby user can easily perform the basic operations.
  • By using nil user can express nothing or completely empty.
  • A float can be referred as decimal value.