Classes in Ruby can be easily modified. The above code adds the like_a_cow method to String... So now the whole program can make a string "moo", like above.
One of the most salient features of our Tech Hiring culture is that there is so much bullshit. Everyone knows this. Each of us contributes his share. But we tend to take the situation for granted.
EDIT: A big advantage with for example the JavaScript version monkey-patching String.prototype, is that the String class is not modified everywhere in your codebase.
Hi! I'm an aspiring Software Engineer currently studying at the University of St Andrews in Scotland. I'm mostly interested in full-stack web development and anything that involves lots of data.
Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
He/Him; Senior Software Developer, IT Swiss-army-knife, 3 programming blades, 1 hardware, 1 networking and infrastructure and a corkscrew. The tweezers have long since been lost. (Recent ADHD diag.)
I use extensions a lot on sealed library classes to add Quality Of Life things. One example is creating cleaner methods for adding parameters to SQLClient ClientCommand objects as one liners instead of multi-command monstrosities. (ok, they aren't that big, but do look unclean)
I think it's different (safer) in C# though, right? Like you need to add using <namespace> for the new methods to be accessible. So it's not true monkey patching, which is generally dangerous and best avoided.
He/Him; Senior Software Developer, IT Swiss-army-knife, 3 programming blades, 1 hardware, 1 networking and infrastructure and a corkscrew. The tweezers have long since been lost. (Recent ADHD diag.)
Indeed you would need to use the containing namespace. And are likely safer as it would be pretty hard to modify the behaviour of code outside of your intended changes. (I think you would have to try very hard to have any affect outside of your explicit calls to the extension methods)
I won't claim to know much about TRUE monkey patching, but wikipedia's Extension Methods and Monkey Patching articles do reference each other in suggestive ways. ;)
In Rust you can add functions to other types with traits. This actually adds the like_a_cow function for all types that are printable, including strings. You have to use the trait though.
pubtraitLikeACow{fnlike_a_cow(&self)->String;}impl<T:std::fmt::Display>LikeACowforT{fnlike_a_cow(&self)->String{format!("Moo {} mooooo",&self)}}fnmain(){// need to `use LikeACow;` if used in other modules, but not here.lets="hello";println!("{}",s.like_a_cow());}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'll go first with Ruby
Classes in Ruby can be easily modified. The above code adds the
like_a_cow
method toString
... So now the whole program can make a string "moo", like above.Same thing in Kotlin, super handy.
EDIT: A big advantage with for example the JavaScript version monkey-patching String.prototype, is that the
String
class is not modified everywhere in your codebase.It works like a normal function, you import the new function only if and where you need it. See kotlinlang.org/docs/extensions.htm...
Nice example, but I think it could be refined somewhat
Similar in Scala:
In Dart it's
In JavaScript it's:
I wonder how many languages allow this?
I'll do you one better: if you define a dynamic getter using
Object.defineProperty
, you can make it look exactly like the Ruby example:Python:
I should add that this is not good practice.
str
is a built in function and you should not override it.C# allows this,
you can now use this as
I use extensions a lot on sealed library classes to add Quality Of Life things. One example is creating cleaner methods for adding parameters to SQLClient ClientCommand objects as one liners instead of multi-command monstrosities. (ok, they aren't that big, but do look unclean)
I think it's different (safer) in C# though, right? Like you need to add
using <namespace>
for the new methods to be accessible. So it's not true monkey patching, which is generally dangerous and best avoided.Indeed you would need to use the containing namespace. And are likely safer as it would be pretty hard to modify the behaviour of code outside of your intended changes. (I think you would have to try very hard to have any affect outside of your explicit calls to the extension methods)
I won't claim to know much about TRUE monkey patching, but wikipedia's Extension Methods and Monkey Patching articles do reference each other in suggestive ways. ;)
It is bad practice to do such things. Better use
and add in you class
In Rust you can add functions to other types with traits. This actually adds the
like_a_cow
function for all types that are printable, including strings. You have touse
the trait though.