Ruby Literals Last Updated : 23 Feb, 2022 Comments Improve Suggest changes Like Article Like Report Any constant value which can be assigned to the variable is called as literal/constant. we use literal every time when typing an object in the ruby code. Ruby Literals are same as other programming languages, just a few adjustments, and differences here. These are following literals in Ruby. Booleans and nil Numbers or Integers Strings Symbols Ranges Arrays Hashes Regular Expressions Type of Ruby Literals Booleans and nil : These are the boolean constants. Here false and nil has the same behavior although nil represents unknown or empty. It behaves as same as false does in conditional statements, but only true or false constants are returned. true behave the same represents a positive variable. true, false, nil Example: Ruby # Demo for Boolean literals puts(3+7==10);# returns true puts(3+7!=10);# returns false puts(3+7==nil);# return false Output: true false false Numbers or Integers : Ruby supports all types of Integers. we can write integers of any size as 100 or 1_00 or 10_0. Ruby allows any number of '_' in it's numbers as it says for readability purposes. Syntax : decimal(0d or 0D) octal(0o or 0O or 0) hex-decimal(0x or 0X) binary(0b or 0B). float(num- or numE1) Example : Ruby puts("300+1_00+10_0=", 300+1_00+10_0 ); puts("hexa-", 0xaa ); puts("octal-", 0o222 ); puts("decimal-", 0d170, " ", 170); puts("binary-", 0b1010); puts("Float-", 1.234E1); puts("hexa-", aa);# error Output : 300+1_00+10_0=500 hexa-170 octal-146 decimal-170 170 binary-10 Float-12.34 main.rb:9:in `': undefined local variable or method `aa' for main:Object (NameError) String : It is same as python. The string can be expressed with either "" or '', where "" allows the escaped characters for interpolation. Syntax:#{expression} Example: Ruby puts( "Two multiply three is Six : #{2 * 3}") puts("guardians\nof\nthe\ngalaxy"); puts('guardians\nof\nthe\ngalaxy') Output : Two multiply three is Six: 6 guardians of the galaxy guardians\nof\nthe\ngalaxy Symbol : In Ruby, a symbol represents a name inside the interpreter. Symbols are placed inside the ruby's interpreter and never garbage-collected. So, it affects the size of the interpreter, if are created in high amount or never freed. Syntax :ruby_symbol We can also create symbols keys by interpolation : Ruby puts(:":guardian_id#{20+1_5}") Output : :guardian_id35 Ranges : It is similar to the one we have in python range(). Prints all possible values between the given boundaries(including). Syntax :range1..range2 Example: Ruby for i in 2..5 do puts(i) end Output: 2 3 4 5 Array : Arrays is a collection of objects, created using '[' and ']'. Example: Ruby # Code for Array Demo gog = ['Quill', 'Gamora', 'Rocket', 'Groot', 'Drax'] puts(gog[0]) puts(gog[2]) # Negative indices are counted from the end print("Negative Index:", gog[-3], "\n\n") # [start, count] puts("[start, count]:", gog[0, 3], "\n") # Using ranges. # as range size exceeded it prints till full length puts("Using range:", gog[0..7]) Output: Quill Rocket Negative Index:Rocket [start, count]: Quill Gamora Rocket Using range: Quill Gamora Rocket Groot Drax Hashes : It is similar to the one we have in python. we can create a hash using symbol keys as they are not-changeable once they are created, and can as perfect keys. Syntax :{key:value} Example: Ruby # way of creating hash hash1 = Hash.new # way of creating hash hash2 = {} # initializing values and keys hash1 = {"Quill" => 100, "Drax" => 200, "Gamora" => 300} # initializing values and keys with symbol keys hash2 = {Quill:1, Gamora:2} print(hash1.keys, "\n") print(hash2.keys, "\n") for i in hash2.keys do # : Should be used while checking before # its a part of the symbol key if i==:Quill # Printing value and assigned key print(i, "=>", hash2[i], "\n") end end Output: ["Quill", "Drax", "Gamora"] [:Quill, :Gamora] Quill=>1 Regular Expression : It is similar to the one we have in perl {/pattern/}. Regexps in ruby can be created with or without delimiters. Syntax :/pattern/ or %r{pattern} We can create a hash using symbol keys as they not-changeable once created they act as perfect key. Syntax :{key:value} Example: Ruby line1 = "guardians of the galaxy"; line2 = "Doctor Strange"; # Checks whether ‘of’ is in line1 in // format if ( line1 =~ /of(.*)/ ) puts line1 end # Checks whether ‘Doc’ is in line1 in %r{} format. if ( line2 =~ %r{Doc(.*)} ) puts line2 end # Checks whether ‘off’ is in line1 . if ( line2 =~ /off(.*)/ ) puts line2 else puts "nothing" end Output: guardians of the galaxy Doctor Strange nothing Comment T Tejashwi5 Follow Improve T Tejashwi5 Follow Improve Article Tags : Ruby Ruby Collections Explore Ruby Programming Language 4 min read OverviewRuby For Beginners 3 min read Ruby Programming Language (Introduction) 4 min read Comparison of Java with Other Programming Languages 4 min read Similarities and Differences between Ruby and C language 3 min read Similarities and Differences between Ruby and C++ 3 min read Environment Setup in Ruby 3 min read How to install Ruby on Linux? 2 min read How to install Ruby on Windows? 2 min read Interesting facts about Ruby Programming Language 2 min read BasicsRuby | Keywords 4 min read Ruby | Data Types 3 min read Ruby Basic Syntax 3 min read Hello World in Ruby 2 min read Ruby | Types of Variables 4 min read Global Variable in Ruby 2 min read Comments in Ruby 2 min read Ruby | Ranges 4 min read Ruby Literals 4 min read Ruby Directories 5 min read Ruby | Operators 11 min read Operator Precedence in Ruby 2 min read Operator Overloading in Ruby 5 min read Ruby | Pre-define Variables & Constants 5 min read Ruby | unless Statement and unless Modifier 2 min read Control StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 1 3 min read Ruby | Loops (for, while, do..while, until) 5 min read Ruby | Case Statement 3 min read Ruby | Control Flow Alteration 7 min read Ruby Break and Next Statement 2 min read Ruby redo and retry Statement 2 min read BEGIN and END Blocks In Ruby 2 min read File Handling in Ruby 4 min read MethodsRuby | Methods 3 min read Method Visibility in Ruby 3 min read Recursion in Ruby 4 min read Ruby Hook Methods 5 min read Ruby | Range Class Methods 5 min read The Initialize Method in Ruby 2 min read Ruby | Method overriding 2 min read Ruby Date and Time 3 min read OOP ConceptsObject-Oriented Programming in Ruby | Set 1 9 min read Object Oriented Programming in Ruby | Set-2 8 min read Ruby | Class & Object 4 min read Private Classes in Ruby 3 min read Freezing Objects | Ruby 2 min read Ruby | Inheritance 4 min read Polymorphism in Ruby 3 min read Ruby | Constructors 2 min read Ruby | Access Control 8 min read Ruby | Encapsulation 2 min read Ruby Mixins 3 min read Instance Variables in Ruby 3 min read Data Abstraction in Ruby 3 min read Ruby Static Members 3 min read ExceptionsRuby | Exceptions 4 min read Ruby | Exception handling 6 min read Catch and Throw Exception In Ruby 3 min read Raising Exceptions in Ruby 4 min read Ruby | Exception Handling in Threads | Set - 1 2 min read Ruby | Exception Class and its Methods 3 min read Ruby RegexRuby | Regular Expressions 3 min read Ruby Search and Replace 2 min read Ruby ClassesRuby | Float Class 7 min read Ruby | Integer Class 3 min read Ruby | Symbol Class 5 min read Ruby | Struct Class 5 min read Ruby | Dir Class and its methods 3 min read Ruby | MatchData Class 4 min read Ruby ModuleRuby | Module 4 min read Ruby | Comparable Module 3 min read Ruby | Math Module 4 min read Include v/s Extend in Ruby 2 min read Like