Introduction
In this article we will learn about the various concept of literals and keyword literals in ruby, we will also go through various examples of the same. We will see how Literals and Keyword Literals in ruby are the same or different from any other programming language.

Literals are values such as 1.0, 'hello world', and [] that are embedded directly into your program text. Whereas, Certain Ruby keywords are primary expressions and can be considered keyword literals.
So, let’s get into the details of the literals in ruby.
What are Literals?
The constant values assigned to the constant variables are known as literals. Literals, we can say, represent fixed values that cannot be changed. It has memory as well, but no variables with references.

For example:
Const int =12
Now let’s get started with the various types of Literals and Keyword Literals in ruby.
Booleans and nil
Both nil and false are false values. In conditional statements, nil is used to represent "no value" or "unknown," but it evaluates to false.

True is a valid value. In conditional statements, all objects except nil and false evaluate to true. (The constants TRUE, FALSE, and NIL are also available, but the lowercase literal forms are preferable.)
# Code for Boolean literals
puts(3+6==9); # returns true
puts(3+13!=18); # returns true
puts(3+134==nil); # return false
Output:
true
true
false
Numbers
Integers of any size can be written as follows:
1234
1_234
The value of these integers is the same: 1,234. The underscore could be used to improve human reading. An underscore can be used anywhere in the number.
The following is an example of how to write floating-point numbers:
12.34
1234e-2
1.234E1
The value of these integers is the same: 12.34. In floating-point numbers, underscores are also acceptable.

To write integers in decimal, hexadecimal, octal, or binary formats, you can use a specific prefix. Use a 0d prefix for decimal numbers, a 0x prefix for hexadecimal numbers, a 0 or 0o prefix for octal numbers, and a 0b prefix for binary numbers. The number's alphabetic component is not case-sensitive.
Examples:
0d170, 0D170, 0xaa, 0xAa, 0xAA, 0Xaa, 0XAa, 0XaA, 0252, 0o252, 0O252, 0b10101010, 0B10101010
The decimal value of all of these numbers is 170. For readability, you can use an underscore like you can with integers and floats.
#Code to represent Numbers in Ruby
puts("400+2_00+30_0=#{400+2_00+30_0}");
puts("hexa-#{0xba}");
puts("octal-#{0o232}" );
puts("decimal-#{ 0d470}");
puts("binary-#{ 0b1110}");
puts("Float-#{1.234E2}");
puts("hexa-#{ba}"); # error
Output:
400+2_00+30_0=900
hexa-186
octal-154
decimal-470
binary-14
Float-123.4
main.rb:7:in `<main>': undefined local variable or method `ba' for main:Object (NameError)
Strings
It is identical to Python. The string can be stated using either "" or ", where "" permits the insertion of the escaped characters.
The most common way of writing strings in Ruby is using ":
For example;
"string in ruby."
The string may be many lines long, but it must not contain internal " :
For example;
"Quoted string: \". As we can see, it is escaped"
Escaped characters like \n for newline, \t for tab, etc. are permitted in double-quote strings.

Using #{...}, double-quote strings enable interpolation of other values:
Any phrase may be inserted inside the interpolated area, however, for readability, it is preferable to keep the expression concise.
#code for strings in Ruby
puts( "Twenty multiply three is Six : #{20 * 3}")
puts("Thor\nLove\nand\nthunder");
puts('Will\nrelease\nsoon\n...')
Output:
Twenty multiply three is Six : 60
Thor
Love
and
thunder
Will\nrelease\nsoon\n…
Symbols
Inside the interpreter, a symbol in Ruby corresponds to a name. Symbols are inserted into the Ruby interpreter and are never destroyed. So, if they are created in large quantities or are never released, it affects the size of the interpreter.
We can use a colon to refer to a symbol:
:ruby symbol.
By using interpolation, we can also produce symbolic keys:
#code for symbols in Ruby
puts(:":my_id--->#{23+1_5}")
Output:
:my_id--->38
Array

Arrays are collections of things generated with the '[' and ']' operators.
# Ruby Code for Array Demo
state = ['UP', 'Bihar', 'Hariyana', 'MP', 'Jharkhand']
puts(state[0])
puts(state[2])
puts("\n")
# Like pythin Negative indices are counted from the end of array
print("Negative Index:", state[-3], "\n\n")
# [start_from, count_of_array]
puts("[start, count]:", state[0, 3], "\n")
# Using ranges.
# as range size exceeded it prints till full length
puts("Using range:", state[0..7])
Output:
UP
Hariyana
Negative Index:Hariyana
[start, count]:
UP
Bihar
Hariyana
Using range:
UP
Bihar
Hariyana
MP
Jharkhand
Hashes
Like python, we can generate a hash using symbol keys because they are unchangeable once created and can be used as ideal keys. It is represented as {key:value}.
# Code for hashes in Ruby
# Create new hash in Ruby
hash_1 = Hash.new
# Another way of creating hash
hash_2 = {}
# Initializing values and keys
hash_1 = {"peter" => 200, "tony" => 400, "natasha" => 300}
# Initializing values and keys with symbol keys
hash_2 = {peter:1, natasha:2}
print(hash_1.keys, "\n")
print(hash_2.keys, "\n")
for i in hash_2.keys do
# : Should be used while checking before
# its a part of the symbol key
if i==:peter
# Print the value and key
print(i, "=>", hash_2[i], "\n")
end
end
Output:
["peter", "tony", "natasha"]
[:peter, :natasha]
peter=>1
Range
It is equivalent to the one found in the Python range (). prints any value that could exist between the specified bounds, including the boundary value. It is represented as range1..range2

#code for ranges in Ruby
for i in 7..12 do
puts(i)
end
Output:
7
8
9
10
11
12
Regular Expression
A regular expression is a unique string of characters that uses specific syntax and a pattern to match or find other strings or groups of strings.
A pattern between slashes or between random delimiters followed by % r is referred to as a regular expression literal.
Syntax:
/pattern/
/pattern/im # here option can be specified
%r!/usr/local! # general delimited regular expression
#Code for regular expression
line_1 = "tony is better than peter";
line_2 = "peter is smart too";
if ( line_1 =~ /peter(.*)/ )
puts "yes, peter is in line1"
end
if ( line_2 =~ /tony(.*)/ )
puts "yes, tony is in line2"
end
Output:
yes, peter is in line1