The Initialize Method in Ruby Last Updated : 25 Sep, 2019 Comments Improve Suggest changes 5 Likes Like Report The initialize method is useful when we want to initialize some class variables at the time of object creation. The initialize method is part of the object-creation process in Ruby and it allows us to set the initial values for an object. Below are some points about Initialize : We can define default argument. It will always return a new object so return keyword is not used inside initialize method Defining initialize keyword is not necessary if our class doesn’t require any arguments. If we try to pass arguments into new and if we don’t define initialize we are going to get an error. Syntax: def initialize(argument1, argument2, .....) Without Initialize variable - Example : Ruby # Ruby program of Initialize method class Geeks # Method with initialize keyword def initialize(name) end end Output : => :initialize In above example, we add a method called initialize to the class, method have a single argument name. using initialize method will initialize a object. With Initialize Variable - Example : Ruby # Ruby program of Initialize method class Rectangle # Method with initialize keyword def initialize(x, y) # Initialize variable @x = x @y = y end end # create a new Rectangle instance by calling Rectangle.new(10, 20) Output : #<Rectangle:0x0000555e7b1ba0b0 @x=10, @y=20> In above example, Initialize variable are accessed using the @ operator within the class but to access them outside of the class we will use public methods. Create Quiz Comment D DivyaPareek Follow 5 Improve D DivyaPareek Follow 5 Improve Article Tags : Ruby Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like