Overriding parent's singleton method2019 Community Moderator ElectionWhat does Ruby have that Python doesn't, and vice versa?ruby inheritance vs mixinsRe-Include ModuleOverriding instance variable array’s operators in Ruby and scopingHow to discover the overrided methods in Ruby/Rails?Understanding private methods in RubyRuby uninitialized constant error when loading all files in a directoryRuby: overriding the puts methodIn Ruby, is there a way to 'override' a constant in a subclass so that inherited methods use the new constant instead of the old?How to override Kernel.load
How is the Swiss post e-voting system supposed to work, and how was it wrong?
Humans have energy, but not water. What happens?
Am I not good enough for you?
What is the likely impact on flights of grounding an entire aircraft series?
Coworker uses her breast-pump everywhere in the office
Make a transparent 448*448 image
Time travel short story where dinosaur doesn't taste like chicken
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
If Invisibility ends because the original caster casts a non-concentration spell, does Invisibility also end on other targets of the original casting?
What does おとこえしや mean?
The three point beverage
Is all copper pipe pretty much the same?
Plywood subfloor won't screw down in a trailer home
Should QA ask requirements to developers?
Why doesn't the EU now just force the UK to choose between referendum and no-deal?
"One can do his homework in the library"
Is "history" a male-biased word ("his+story")?
Is it illegal in Germany to take sick leave if you caused your own illness with food?
Replacing Windows 7 security updates with anti-virus?
How does Dispel Magic work against Stoneskin?
validation vs test vs training accuracy, which one to compare for claiming overfit?
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
Word for a person who has no opinion about whether god exists
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
Overriding parent's singleton method
2019 Community Moderator ElectionWhat does Ruby have that Python doesn't, and vice versa?ruby inheritance vs mixinsRe-Include ModuleOverriding instance variable array’s operators in Ruby and scopingHow to discover the overrided methods in Ruby/Rails?Understanding private methods in RubyRuby uninitialized constant error when loading all files in a directoryRuby: overriding the puts methodIn Ruby, is there a way to 'override' a constant in a subclass so that inherited methods use the new constant instead of the old?How to override Kernel.load
In C#, a regular class cannot inherit from a static class. A similar case for Ruby seems to be:
class Foo
class << self
def test
"this is test"
end
end
end
class Bar < Foo
def test
puts super
end
end
Bar.new.test # >> error
Is there a way for a regular class Bar
to inherit from Foo
and override test
?
ruby
add a comment |
In C#, a regular class cannot inherit from a static class. A similar case for Ruby seems to be:
class Foo
class << self
def test
"this is test"
end
end
end
class Bar < Foo
def test
puts super
end
end
Bar.new.test # >> error
Is there a way for a regular class Bar
to inherit from Foo
and override test
?
ruby
1
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.
– Sergio Tulentsev
Mar 6 at 17:26
@SergioTulentsev Is there a way for me to callsuper
so that"this is test"
would be printed?
– Adrian Wydmanski
Mar 6 at 17:40
Yes, definetest
in Bar the same way, as singleton method. This would makeFoo.test
its proper super method.
– Sergio Tulentsev
Mar 6 at 17:41
There's nothing wrong with the way you've definedFoo::test
(the "::" meaning it's a class method, as opposed toFoo#test
if it were an instance method), but the normal (shorthand) way it's written isdef self.test; ... ; end
(which here is the same asdef Foo.test; ... ; end
).
– Cary Swoveland
Mar 6 at 20:39
add a comment |
In C#, a regular class cannot inherit from a static class. A similar case for Ruby seems to be:
class Foo
class << self
def test
"this is test"
end
end
end
class Bar < Foo
def test
puts super
end
end
Bar.new.test # >> error
Is there a way for a regular class Bar
to inherit from Foo
and override test
?
ruby
In C#, a regular class cannot inherit from a static class. A similar case for Ruby seems to be:
class Foo
class << self
def test
"this is test"
end
end
end
class Bar < Foo
def test
puts super
end
end
Bar.new.test # >> error
Is there a way for a regular class Bar
to inherit from Foo
and override test
?
ruby
ruby
edited Mar 7 at 7:52
sawa
132k29205305
132k29205305
asked Mar 6 at 17:22
Adrian WydmanskiAdrian Wydmanski
742415
742415
1
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.
– Sergio Tulentsev
Mar 6 at 17:26
@SergioTulentsev Is there a way for me to callsuper
so that"this is test"
would be printed?
– Adrian Wydmanski
Mar 6 at 17:40
Yes, definetest
in Bar the same way, as singleton method. This would makeFoo.test
its proper super method.
– Sergio Tulentsev
Mar 6 at 17:41
There's nothing wrong with the way you've definedFoo::test
(the "::" meaning it's a class method, as opposed toFoo#test
if it were an instance method), but the normal (shorthand) way it's written isdef self.test; ... ; end
(which here is the same asdef Foo.test; ... ; end
).
– Cary Swoveland
Mar 6 at 20:39
add a comment |
1
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.
– Sergio Tulentsev
Mar 6 at 17:26
@SergioTulentsev Is there a way for me to callsuper
so that"this is test"
would be printed?
– Adrian Wydmanski
Mar 6 at 17:40
Yes, definetest
in Bar the same way, as singleton method. This would makeFoo.test
its proper super method.
– Sergio Tulentsev
Mar 6 at 17:41
There's nothing wrong with the way you've definedFoo::test
(the "::" meaning it's a class method, as opposed toFoo#test
if it were an instance method), but the normal (shorthand) way it's written isdef self.test; ... ; end
(which here is the same asdef Foo.test; ... ; end
).
– Cary Swoveland
Mar 6 at 20:39
1
1
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.
test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.– Sergio Tulentsev
Mar 6 at 17:26
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.
test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.– Sergio Tulentsev
Mar 6 at 17:26
@SergioTulentsev Is there a way for me to call
super
so that "this is test"
would be printed?– Adrian Wydmanski
Mar 6 at 17:40
@SergioTulentsev Is there a way for me to call
super
so that "this is test"
would be printed?– Adrian Wydmanski
Mar 6 at 17:40
Yes, define
test
in Bar the same way, as singleton method. This would make Foo.test
its proper super method.– Sergio Tulentsev
Mar 6 at 17:41
Yes, define
test
in Bar the same way, as singleton method. This would make Foo.test
its proper super method.– Sergio Tulentsev
Mar 6 at 17:41
There's nothing wrong with the way you've defined
Foo::test
(the "::" meaning it's a class method, as opposed to Foo#test
if it were an instance method), but the normal (shorthand) way it's written is def self.test; ... ; end
(which here is the same as def Foo.test; ... ; end
).– Cary Swoveland
Mar 6 at 20:39
There's nothing wrong with the way you've defined
Foo::test
(the "::" meaning it's a class method, as opposed to Foo#test
if it were an instance method), but the normal (shorthand) way it's written is def self.test; ... ; end
(which here is the same as def Foo.test; ... ; end
).– Cary Swoveland
Mar 6 at 20:39
add a comment |
1 Answer
1
active
oldest
votes
Is there a way for
Bar
to inherit fromFoo
You've done that part correctly
and override
test
?
You need to define Bar.test
as a class method, but what you have written is an instance method definition (and invocation). Try
class Bar < Foo
class << self
def test
super + ", man"
end
end
end
Bar.test
#=> "this is a test, man"
There is no such thing as a static class or method in Ruby, so the instance method invocation Bar.new.test
will never refer to the same thing as the class method invocation Bar.test
as it might in languages with the concept of static methods.
So the only way to make it work is to maketest
method insideBar
singleton as well?
– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:Foo.new.test
will not call your method defined on the classFoo
. (Protip: don't use the nametest
since that's a method onKernel
so the error messages may be more confusing than they need to be.)
– Andrew Schwartz
Mar 6 at 21:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55028858%2foverriding-parents-singleton-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is there a way for
Bar
to inherit fromFoo
You've done that part correctly
and override
test
?
You need to define Bar.test
as a class method, but what you have written is an instance method definition (and invocation). Try
class Bar < Foo
class << self
def test
super + ", man"
end
end
end
Bar.test
#=> "this is a test, man"
There is no such thing as a static class or method in Ruby, so the instance method invocation Bar.new.test
will never refer to the same thing as the class method invocation Bar.test
as it might in languages with the concept of static methods.
So the only way to make it work is to maketest
method insideBar
singleton as well?
– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:Foo.new.test
will not call your method defined on the classFoo
. (Protip: don't use the nametest
since that's a method onKernel
so the error messages may be more confusing than they need to be.)
– Andrew Schwartz
Mar 6 at 21:19
add a comment |
Is there a way for
Bar
to inherit fromFoo
You've done that part correctly
and override
test
?
You need to define Bar.test
as a class method, but what you have written is an instance method definition (and invocation). Try
class Bar < Foo
class << self
def test
super + ", man"
end
end
end
Bar.test
#=> "this is a test, man"
There is no such thing as a static class or method in Ruby, so the instance method invocation Bar.new.test
will never refer to the same thing as the class method invocation Bar.test
as it might in languages with the concept of static methods.
So the only way to make it work is to maketest
method insideBar
singleton as well?
– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:Foo.new.test
will not call your method defined on the classFoo
. (Protip: don't use the nametest
since that's a method onKernel
so the error messages may be more confusing than they need to be.)
– Andrew Schwartz
Mar 6 at 21:19
add a comment |
Is there a way for
Bar
to inherit fromFoo
You've done that part correctly
and override
test
?
You need to define Bar.test
as a class method, but what you have written is an instance method definition (and invocation). Try
class Bar < Foo
class << self
def test
super + ", man"
end
end
end
Bar.test
#=> "this is a test, man"
There is no such thing as a static class or method in Ruby, so the instance method invocation Bar.new.test
will never refer to the same thing as the class method invocation Bar.test
as it might in languages with the concept of static methods.
Is there a way for
Bar
to inherit fromFoo
You've done that part correctly
and override
test
?
You need to define Bar.test
as a class method, but what you have written is an instance method definition (and invocation). Try
class Bar < Foo
class << self
def test
super + ", man"
end
end
end
Bar.test
#=> "this is a test, man"
There is no such thing as a static class or method in Ruby, so the instance method invocation Bar.new.test
will never refer to the same thing as the class method invocation Bar.test
as it might in languages with the concept of static methods.
answered Mar 6 at 17:50
Andrew SchwartzAndrew Schwartz
1,85811132
1,85811132
So the only way to make it work is to maketest
method insideBar
singleton as well?
– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:Foo.new.test
will not call your method defined on the classFoo
. (Protip: don't use the nametest
since that's a method onKernel
so the error messages may be more confusing than they need to be.)
– Andrew Schwartz
Mar 6 at 21:19
add a comment |
So the only way to make it work is to maketest
method insideBar
singleton as well?
– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:Foo.new.test
will not call your method defined on the classFoo
. (Protip: don't use the nametest
since that's a method onKernel
so the error messages may be more confusing than they need to be.)
– Andrew Schwartz
Mar 6 at 21:19
So the only way to make it work is to make
test
method inside Bar
singleton as well?– Adrian Wydmanski
Mar 6 at 18:20
So the only way to make it work is to make
test
method inside Bar
singleton as well?– Adrian Wydmanski
Mar 6 at 18:20
Yes, otherwise you're not overriding anything. Another way to see this:
Foo.new.test
will not call your method defined on the class Foo
. (Protip: don't use the name test
since that's a method on Kernel
so the error messages may be more confusing than they need to be.)– Andrew Schwartz
Mar 6 at 21:19
Yes, otherwise you're not overriding anything. Another way to see this:
Foo.new.test
will not call your method defined on the class Foo
. (Protip: don't use the name test
since that's a method on Kernel
so the error messages may be more confusing than they need to be.)– Andrew Schwartz
Mar 6 at 21:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55028858%2foverriding-parents-singleton-method%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There are no static classes in ruby. This inheritance here works just fine. Bar#test fails to find super method because there's none.
test
that is in Foo lives on another level. Not instances of the class, but the class object itself. Overall, it's not clear what you're trying to ask here.– Sergio Tulentsev
Mar 6 at 17:26
@SergioTulentsev Is there a way for me to call
super
so that"this is test"
would be printed?– Adrian Wydmanski
Mar 6 at 17:40
Yes, define
test
in Bar the same way, as singleton method. This would makeFoo.test
its proper super method.– Sergio Tulentsev
Mar 6 at 17:41
There's nothing wrong with the way you've defined
Foo::test
(the "::" meaning it's a class method, as opposed toFoo#test
if it were an instance method), but the normal (shorthand) way it's written isdef self.test; ... ; end
(which here is the same asdef Foo.test; ... ; end
).– Cary Swoveland
Mar 6 at 20:39