It seems in Ruby, we can't dup all kind of objects, like NilClass, TrueClass, FalseClass. But we can dup `Object`. Below are some Rails API to check if we can dup any specific object or not.
Now, I don't get the idea why Method objects are not allowed to `.dup` ? What is the reason behind it ? And if I want to prevent my custom class being *duped*, what should I need to write for that ?
···
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
nil, true, false, Fixnums, and I believe even Floats in recent versions of
Ruby are implemented as tagged pointers instead of a boxed value. "dup" on
them doesn't make sense, since they're effectively singletons that can only
be represented as tagged pointers.
···
On Sat, Sep 19, 2015 at 8:38 AM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:
Hi,
It seems in Ruby, we can't dup all kind of objects, like NilClass,
TrueClass, FalseClass. But we can dup `Object`. Below are some Rails API to
check if we can dup any specific object or not.
Now, I don't get the idea why Method objects are not allowed to `.dup` ?
What is the reason behind it ? And if I want to prevent my custom class
being *duped*, what should I need to write for that ?
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
Cheers
TypeError? I think it is not intuitive to use a TypeError for this error
type. It is better to use a Security Error, maybe. Or for the best, a new
Error class may be created, named ForbiddenError, maybe. I am just saying.
=)
···
2015-09-21 12:04 GMT+03:00 Robert Klemme <shortcutter@googlemail.com>:
On Sat, Sep 19, 2015 at 5:38 PM, Arup Rakshit > <aruprakshit@rocketmail.com> wrote:
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
TypeError is for consistency with Fixnum#dup and others:
irb(main):001:0> 2.dup
TypeError: can't dup Fixnum
from (irb):1:in `dup'
from (irb):1
from /usr/bin/irb:11:in `<main>'
···
On Mon, Sep 21, 2015 at 1:15 PM, İsmail Arılık <arilik.ismail@gmail.com> wrote:
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
Cheers
TypeError? I think it is not intuitive to use a TypeError for this error
type. It is better to use a Security Error, maybe. Or for the best, a new
Error class may be created, named ForbiddenError, maybe. I am just saying.
=)
2015-09-21 12:04 GMT+03:00 Robert Klemme <shortcutter@googlemail.com>:
On Sat, Sep 19, 2015 at 5:38 PM, Arup Rakshit >> <aruprakshit@rocketmail.com> wrote:
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
Just for giggles, here is how you circumvent this idiotic idea
class Dummy
def dup
raise TypeError, "cannot dup #{self.class}"
end
end
d = Dummy.new
d.instance_eval do
def dup
grandparent = self.class.superclass
meth = grandparent.instance_method(:dup)
meth.bind(self).call
end
end
c = d.dup
p d
p c
···
On 21 September 2015 at 12:33, Greg Navis <contact@gregnavis.com> wrote:
TypeError is for consistency with Fixnum#dup and others:
irb(main):001:0> 2.dup
TypeError: can't dup Fixnum
from (irb):1:in `dup'
from (irb):1
from /usr/bin/irb:11:in `<main>'
On Mon, Sep 21, 2015 at 1:15 PM, İsmail Arılık <arilik.ismail@gmail.com> > wrote:
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
Cheers
TypeError? I think it is not intuitive to use a TypeError for this error
type. It is better to use a Security Error, maybe. Or for the best, a new
Error class may be created, named ForbiddenError, maybe. I am just saying.
=)
2015-09-21 12:04 GMT+03:00 Robert Klemme <shortcutter@googlemail.com>:
On Sat, Sep 19, 2015 at 5:38 PM, Arup Rakshit >>> <aruprakshit@rocketmail.com> wrote:
> And if I want to prevent my custom class being *duped*, what should I
need to write for that ?
Easy:
def dup; raise TypeError, "cannot dup #{self.class}" end
Can we please do away with that phrase? Either you mean it - or you
don't. If you don't then why would you post in the first place? Sorry,
I have seen this written too often when people did not really give
much thought about what they are writing. And don't get me started on
the suggestion to use a SecurityError for this...
Cheers
robert
···
On Mon, Sep 21, 2015 at 1:15 PM, İsmail Arılık <arilik.ismail@gmail.com> wrote: