![]() ![]() In this example, the inner_closure proc captures the variable x from its surrounding context when it is defined. Puts outer_method # Output: 20 Code language: PHP ( php ) Here’s an example of a closure using a block: def outer_method The closure can then access and manipulate these variables and methods when it is executed, even if they are no longer in scope. When a closure is defined, it captures the variables and methods available at the time of its definition. Closures allow developers to create functions that remember the environment in which they were created, even if they are executed in a different context later. In Ruby, a closure is a block of code (lambda, proc, or block) that captures its surrounding context, including local variables, bindings, and methods. We define a custom setter method age= for the age attribute, using the clamp method to restrict the age within the range of 0 to 120.Įxplain the concept of closures in Ruby and how they capture their surrounding context. In this code, the Person class has attributes name (read-only) and age (read-write). Puts person.age # Output: 120 (Age is restricted to the range 0 to 120) Code language: CSS ( css ) class Person attr_reader :name attr_accessor :age def initialize( name, = name ![]() Implement a custom setter method for the age attribute that restricts the age to be within a specific range (e.g., 0 to 120). The rescue block catches the exception, and the program continues to execute without terminating abruptly.Ĭreate a Ruby class called Person with attributes name and age. In this example, a ZeroDivisionError exception is raised when trying to divide by zero. # Output: Error: divided by 0 Code language: PHP ( php ) It should have methods to deposit, withdraw, and check the balance.Īnswer: class BankAccount def initialize = 0 Write a Ruby class representing a basic bank account. Gems can be installed and managed using the gem command-line tool, and popular gems are hosted on the RubyGems website. By using gems, developers can avoid reinventing the wheel and focus on higher-level application logic. Gems are useful in the development process because they provide a modular and efficient way to manage dependencies and reuse code. Gems can range from simple utility libraries to complex frameworks and tools. Gems allow developers to distribute and share their code with others, making it easier to incorporate pre-built solutions into Ruby applications. In Ruby, a gem is a packaged library or extension that contains reusable code or functionalities. What is a gem in Ruby, and how is it useful in the development process? In this code, the even_numbers function uses the select method to filter the array and only keep elements for which the block ( num.even?) returns true, i.e., even numbers. An instance of the Car class would be a specific car object with its unique make, model, year, and other attribute values.Ĭreate a Ruby function that takes an array of numbers as input and returns a new array containing only the even numbers. The class may define attributes like make, model, and year, as well as methods like start_engine and drive. It represents a unique occurrence of the class with its own set of attribute values.įor example, consider a Car class. An instance of a class, on the other hand, is a specific object created based on the class definition. It acts as a blueprint for creating objects with similar characteristics. In Ruby, a class is a blueprint or a template that defines the properties (attributes) and behaviors (methods) of objects. Feel free to ask your interviewer about more assumptions as neededĮxplain the difference between a Ruby class and an instance of a class. A van can park, but it will take up 3 regular spots.A car can park in a single compact spot, or a regular spot.The parking lot has motorcycle spots, car spots and large spots.The parking lot can hold motorcycles, cars and vans.Tell us how many spots vans are taking up. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |