Explore all the latest Ruby interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Ruby interview for FREE!
In Ruby, an object is an instance of a class that encapsulates data and behavior. Every value in Ruby is an object, including numbers, strings, arrays, and even classes themselves.
When you create a new object in Ruby, you're essentially creating a new instance of a particular class. For example, you can create a new instance of the `String` class like this:
my_string = String.new("hello, world")
In this code, we're creating a new instance of the `String` class and assigning it to the variable `my_string`. The string "hello, world" is passed as an argument to the `String.new` method, which creates the new string object.
Once you've created an object in Ruby, you can use its methods to interact with its data and behavior. For example, you can call the `length` method on our `my_string` object to get its length:
puts my_string.length # prints "12"
This code calls the `length` method on our `my_string` object and prints the result to the console. Because `my_string` contains the string "hello, world", which is 12 characters long, the output is `12`.
In Ruby, objects can be passed as arguments to methods, returned as values from methods, and even defined as attributes of other objects. This makes Ruby a highly object-oriented language, where everything is treated as an object with its own behavior and data.


