Logo

dev-resources.site

for different kinds of informations.

Open class

Published at
9/21/2023
Categories
ruby
class
patch
Author
wuletawwonte
Categories
3 categories in total
ruby
open
class
open
patch
open
Author
12 person written this
wuletawwonte
open
Open class

Do you wonder what would happen to a code in a class definition in Ruby? For example, what would be the output of the following code?

class Movie
  puts "Hello there"
end

# Output
Hello there
Enter fullscreen mode Exit fullscreen mode

One may think because the “puts” code is outside any method, so it wont get executed. But the fact is, in Ruby, there is no real distinction between code that defines a class and code of any other kind. You can put any code you want in a class definition and its going to get executed. Here is another example

3.times do
  class Movie
    puts "Movie class"
  end
end

# Output
Movie
Movie
Movie
Enter fullscreen mode Exit fullscreen mode

Open Class in Ruby is the technique where you can define a class again to add or overwrite its methods dynamically at runtime. That means if you write a code that defines an already existing class Ruby would reopen the existing class and modify it. It is sometimes called Monkey Patching.

Ruby allows Monkey patching even on standard Ruby classes like String, Numeric or any other standard class. For example I can add palindrome method in the String ruby class to check if a string reads the same with its reverse.

class String
  def palindrome?
    self == self.reverse
  end
end

puts "wuletaw".palindrome? # false
puts "rotator".palindrome? # true
Enter fullscreen mode Exit fullscreen mode

As cool as this may seem, however, monkey patching does have a dark side. It can make code harder to maintain, introduce unexpected behavior and make it more difficult for other developers to understand.

patch Article's
30 articles in total
Favicon
Stop Worrying About EC2 Patching – Automate It Like a Pro!
Favicon
Oracle Patch Update: Securing Your Database and Applications
Favicon
How to Manage the Oracle Critical Patch Update
Favicon
Critical Insights About Oracle Critical Patch Update: Enhancing System Security and Functionality
Favicon
A Guide to Oracle Critical Patch Update
Favicon
Key Considerations And Benefits Of Choosing A Testing Solution For Businesses
Favicon
All About Oracle Critical Patch Update
Favicon
How to Apply a Magento 2 Patch
Favicon
How to use LLM for efficient text outputs longer than 4k tokens?
Favicon
How to Leverage Flawless Integration of Oracle Patch Updates in Your System?
Favicon
A Comprehensive Guide to Oracle's Latest Critical Patch Updates
Favicon
Ensure Security in Financial Institutions After Oracle Patch Update Release
Favicon
Oracle Critical Patch Update: Ensuring Software Security
Favicon
Achieving Harmony: Balancing Oracle Patch Testing’s Cost, Time, and Risk
Favicon
Zero-day: Server Message Block (SMB) Server in Linux Kernel 5.15 Has a Critical Vulnerability!
Favicon
How Critical Are Oracle Critical Patch Updates?
Favicon
Maintain High-Quality Oracle Patch Update With Automation Testing
Favicon
Ensuring Security Using Oracle Critical Patch Update and Cloud Testing
Favicon
Oracle Patch Updates: Navigating the Future with Confidence and Innovation
Favicon
Unveiling The Power Of Oracle’s Critical Patch Update
Favicon
Seamless Oracle Patch Testing: Empowering Efficiency
Favicon
Oracle Patch Update: Enhancing Performance and Security
Favicon
Open class
Favicon
Oracle’s Critical Patch Update April 2023 Readiness
Favicon
Patch me up.
Favicon
A tale of tests, greenlets and patches
Favicon
HTTP Request Methods (aka HTTP Verbs)
Favicon
Sharing your git patches
Favicon
Fetch() in JavaScript pt. II, Revenge of the options
Favicon
Run Windows update now!

Featured ones: