Why does `class << self` in Ruby 'break' inheritance? [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Suppose I have:
class Parent
def foo
"foo"
end
end
class Child < Parent
class << self
end
Then
c = Child.new.foo
does not give me "foo". Why not?
The question above came when I was messing around trying to figure out why some code with the class << self idiom was behaving in a way I didn't understand. Since I had a syntax error in this post it is obvious that the error in the code I was looking at had nothing to do with class << self breaking inheritance. I want to delete this Q. Stack overflow won't let me. Hope to post a better question later.

There is a syntax error here. You never close the singleton class. Should be:
class Child < Parent
class << self
end
end
Works perfectly for me:
irb(main):008:0> class Parent
irb(main):009:1> def foo
irb(main):010:2> "foo"
irb(main):011:2> end
irb(main):012:1> end
=> :foo
irb(main):013:0> class Child < Parent
irb(main):014:1> class << self
irb(main):015:2> def foo
irb(main):016:3> "class-foo"
irb(main):017:3> end
irb(main):018:2> end
irb(main):019:1> end
=> :foo
irb(main):020:0> c = Child.new
=> #<Child:0x000001021eae30>
irb(main):021:0> c.foo
=> "foo"
irb(main):022:0> Child.foo
=> "class-foo"

class Child < Parent
class << self
end
This definition is syntactically invalid, and doesn't make sense. The correct definition is
class Child < Parent
class << self
end
end
But even in this case, it's completely useless unless you need to define something inside the singleton class. The following code is more than enough.
class Parent
def foo
"foo"
end
end
class Child < Parent
end
Here's an example
2.1.1 :010 > Child.new.foo
=> "foo"

You're missing an "end". class << self is not a one-liner, and requires you to close the block. I just ran your code with an extra end and it worked.

Related

Nested class initialize not being called [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am creating a custom Trie as follows:
# frozen_string_literal: true
class CustomTrie
attr_accessor :trie
def initialize(items)
end
def self.parse_to_trie(items)
end
def get(path)
end
class Node
attr_accessor :key, :parent, :children,
def initialize(key: '', parent: nil, children: [])
# This isn't being called, why?
#key = key
#parent = parent
#children = children
end
def is_parent?
end
def is_leaf?
end
def inspect
{key: #key, parent: #parent, children: #children}
end
end
class Trie
attr_accessor :root
def initialize(root = Node.new)
#root = root
end
def add(path)
end
def get(path)
end
end
end
However when I try calling CustomTrie::Node.new everything is initialized to nil instead of the default values, and when I try calling the constructor with values I get the error: "ArgumentError (wrong number of arguments (given x, expected 0))"
I'm sure I'm missing something obvious, but I haven't been able to identify what I'm doing wrong.
:facepalm:
It turns out it was because I had a comma after :children in my attr_accessor call.

What does "self" mean in a module? [duplicate]

This question already has answers here:
Ruby self in layman terms?
(3 answers)
Closed 7 years ago.
What is the difference between:
module Math
def self.square(num)
num**2
end
end
puts Math.square(6)
and
module Math
def square(num)
num**2
end
end
puts Math.square(6)
What is "self" in the first example? I'm only used to using self inside of a class, where self refers to the instance of the class.
Well, let’s see:
module Foo
p self
end
# prints: Foo
So self is the module itself. This allows us to define methods directly on Foo, rather than instances of it:
module Foo
def self.bar
42
end
end
Foo.bar #=> 42
class A; include Foo; end
A.new.respond_to?(:bar) #=> false
Note that this is not unique to Modules, and is the same for Classes. And since self == Foo, nothing is stopping us from doing:
def Foo.baz
3.14
end
Foo.baz #=> 3.14

Access super variables using class object [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Heey I am new to Ruby. I need to create a factory method, which will return me an object of a class. Using that object I should be able to access the variables of the class. I have written the following code, but I surely have miss something.
class Super
##super_temp = 1
def Super.get_instance(world)
platform = world
if ##instance == nil
if platform==1
##instance = BaseA.new
else
##instance = BaseB.new
end
end
return ##instance
end
end
class BaseA < Super
##base_temp = 2
end
class BaseB < Super
##base_temp = 3
end
class Demo
def Demo.call_demo
obj = Super.get_instance(0)
puts "---------temp is #{obj.base_temp}"
end
end
Demo.call_demo
I need to retrieve the value of base_temp in class Demo.
Don't use ## (Why should we avoid using class variables ## in rails?) - # solves your problem just as easily.
Aside from that, all that is missing in your code is a getter:
class Super
#super_temp = 1
def Super.get_instance(world)
platform = world
if #instance == nil
if platform==1
#instance = BaseA.new
else
#instance = BaseB.new
end
end
return #instance
end
def base_temp
self.class.base_temp
end
def self.base_temp
#base_temp
end
end
class BaseA < Super
#base_temp = 2
end
class BaseB < Super
#base_temp = 3
end
class Demo
def Demo.call_demo
obj = Super.get_instance(0)
puts "---------temp is #{obj.base_temp}"
end
end
Demo.call_demo
# ---------temp is 3
The instance getter (implemented as self.class.base_temp) calls the class method base_temp of the instance's class. If we add prints of the internal products of the function, you can have some insights about its internals:
class Super
def base_temp
p self
p self.class
p self.class.base_temp
end
end
BaseA.new.base_temp
# #<BaseA:0x000000027df9e0>
# BaseA
# 2
BaseB.new.base_temp
# #<BaseB:0x000000027e38b0>
# BaseB
# 3

Ruby: class << self; def method VS. def self.method [duplicate]

This question already has answers here:
class << self vs self.method with Ruby: what's better?
(6 answers)
Closed 8 years ago.
My understanding is that:
class Example
class << self
def my_method; end
end
end
is equivalent to:
class Example
def self.my_method; end
end
but is that correct?
They are equivalent, but choose the latter for clarity reasons. When you have a class that is many lines long you may miss the class method definition when using class << self.
Another good reason to use class << self is when you need accessors on the class level:
class Foo
class << self
attr_accessor :bar
end
end
Beware that this is often not what you want as it is not thread safe. But that's a design problem. If you need it, you need it.
In the case of class << self all methods defined below will be class methods up until the class << self is closed. For a class method at a singular level or multiple ones if you wish you can define the method as self.foo.
class Test
def self.foo
end
def bar
end
end
class Test
class << self
def foo
end
end
def bar
end
end
In both of these cases you will end up with a class method "foo" and an instance method "bar". Both ways accomplish the same thing.

What does self mean in Ruby? [duplicate]

This question already has answers here:
Ruby Definition of Self
(3 answers)
Closed 7 years ago.
What does ruby self represent? what is it? what does it mean? Could some please explain it to me? in simple terms please
And what is its function in a class?
class MyClass
def method.self
end
end
self refers to the object that is currently in context.
In your example, self is the class itself and def self.method is defining a class method. For example:
class MyClass
def self.method
puts "Hello!"
end
end
> MyClass.method
#=> "Hello"
You can also use self on instances of a class.
class MyClass
def method_a
puts "Hello!"
end
def method_b
self.method_a
end
end
> m = MyClass.new
> m.method_b
#=> "Hello!"
In this case, self refers to the instance of MyClass.
There is a good blog post on self in Ruby here, or, as it was pointed out in the comments, there is some more on this in the Ruby documentation.

Resources