Built-in Classes Modified

Hello Everyone, I am new in this community and I have good knowledge of python but before some days i was thinking to switch in ruby. So I have checked the comparison between ruby vs python as a future point of view. So I am cleared ruby is little bit better than python but I am confused in built-in classes. In python, built-in classes can’t be modified but Can anyone know in ruby is it modify or not? And Also suggest me which one is better ruby or python?

Also suggest me which one is better ruby or python?

Ruby of course :grinning:

But really, the two as languages are fairly close and it depends on your specific use case. You may want to use python’s specific libraries, for example.

I much prefer ruby syntax and its flexibility, and the existence of jruby means the same ruby language works for the jvm.

In python, built-in classes can’t be modified but Can anyone know in ruby is it modify or not?

Yes, in ruby you can open any class and change it as you want. In this example, we add a method to the String class to put brackets around a string:

$ irb
2.7.0 :001 > class String
2.7.0 :002 >   def add_brackets
2.7.0 :003 >     "("+self+")"
2.7.0 :004 >   end
2.7.0 :005 > end
 => :add_brackets 
2.7.0 :006 > "abc".add_brackets
 => "(abc)" 

If you planning to learn later C++. Ruby will provided you a great fondation for OOP.

And you can add or defined existing methods (but usually it is for debugging purpose) Otherwise it is risky. .And it’s call monkey patching.

https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs