Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What is the preferred way to call a method in a Ruby class, that lives in it's parent module?
like this..
#!/usr/bin/ruby
module Foo
def baz
123123
end
class Bar
def test
puts baz
end
end
class Bar
end
end
bar = Foo::Bar.new
bar.test
I can get this to work if I do this..
#!/usr/bin/ruby
module Foo
def baz
123123
end
class Bar
include Foo
def test
puts baz
end
end
class Bar
end
end
bar = Foo::Bar.new
bar.test
But is this the best way?
module Foo
def self.baz
123123
end
class Bar
def test
puts Foo.baz
end
end
end
bar = Foo::Bar.new
bar.test
Related
class Foo
include Bar
include Baz
end
module Bar
def do_something
end
end
module Baz
do_something
end
Baz module does not have access to Bar. Is there a way for it to call a method in Bar?
One approach is to extend Baz with Bar, but I want to include them all in Foo instead.
If for whatever reason you don't want to extend Baz with Bar, you can create an object that extends Bar inside Baz:
module Bar
def do_something
puts 42
end
end
module Baz
Object.new.extend(Bar).do_something # prints 42
end
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is wrong with this code?
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{name}"
end
end
Write the code as
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{#name}" # <~~ you missed `#` before name.
end
end
If you write only name(instead of #name), Ruby will try to look for a local var named as name, but you didn't define any. Then it will try to check if any method you have defined as name or not, that also not present. So finally you will get
undefined local variable or method `name'
Here is an example after the fix :
#!/usr/bin/env ruby
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{#name}"
end
end
Person.new("Raju").greet('Welcome!') # => "Hi Welcome!, my name is Raju"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's consider this:
class Container
def function_one
...
end
def function_two
...
end
def function_three
...
end
attr_accessor :result_from_function_one
attr_accessor :result_from_function_two
attr_accessor :result_from_function_three
end
Since I can't create separate algorithm body for other classes, I created four separate classes. When I need to run algorithm one, I create a class with function one, and so on:
class Container
...
end
class ContainerWithFunctionOne < Container
def function_one
...
end
attr_accessor :result_from_function_one
end
class ContainerWithFunctionTwo < Container
def function_two
...
end
attr_accessor :result_from_function_two
end
class ContainerWithFunctionThree < Container
def function_three
...
end
attr_accessor :result_from_function_three
end
But when I combine function_one with function_two, I have an issue because they need to use the same data structure. So I was thinking about dividing the class Container into modules:
module FunctionOne
class Container
def function_one
...
end
attr_accessor :result_from_function_one
end
end
module FunctionTwo
class Container
def function_two
...
end
attr_accessor :result_from_function_two
end
end
module FunctionThree
class Container
def function_three
...
end
attr_accessor :result_from_function_three
end
end
But when I try to run it:
require_relative 'FunctionOne'
require_relative 'FunctionTwo'
require_relative 'FunctionThree'
containter = Container.new
container.function_one
container.function_two
container.function_three
it gave a run time error:
in `<top (required)>': uninitialized constant Container (NameError)
and I don't know how to fix this problem.
You can try
container = FunctionOne::Container.new
to create a new container
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In the example below, I want to have an instance variable in class B that is created/set whenever B is instantiated. Obviously I don't want to have to go redefine all the initialize methods of A.
class A
def initialize(a)
end
def initialize(a, b)
end
end
class B < A
# Here I want an instance variable created without
# redefining the initialize methods
#iv = "hey" #<-- Obviously does not work
# And I don't want to have to do #iv |= "hey" all over the place
end
I'm not sure what you have against defining initialize methods, but this is how it should be done.
class A
def initialize a
#a = a
end
attr_accessor :a
end
class B < A
def initialize a, b
#b = b
super(a)
end
attr_accessor :b
end
b = B.new 1, 2
b.a # => 1
b.b # => 2