I am completely new to ruby though I have several years experience in programming. My question is why following code's class X can directly refer to state (for defining method block) without any include or require keyword? And how can I access to state method value (in class X) after those state blocks are initialized (so I can observe values gets defined or executed in that block)? Thanks
class Module
def state(&block)
meth_name = Module.make_state_meth_name(self)
puts "meth name: #{meth_name}"
define_method(meth_name, &block)
end
def self.make_state_meth_name(klass)
@state_meth_id ||= 0
r = "__state#{@state_meth_id}__#{Module.get_class_name(klass)}".to_sym
@state_meth_id += 1
return r
end
def self.get_class_name(klass)
(klass.name.nil? or klass.name == "") \
? "Anon#{klass.object_id}" \
: klass.name.split("::").last
end
end
class X
state do
puts "abc"
end
state do
puts "xyz"
end
end
x = X.new
My question is why following code's class X can directly refer to state (for defining method block) without any include or require keyword?
Because of this property:
class X; end
=> nil
X.class
=> Class
X.class.superclass
=> Module
And how can I access to state method value (in class X) after those state blocks are initialized
Not sure this question makes sense… “State method value”?
(so I can observe values gets defined or executed in that block)?
you can generate a block that observes those changes as they happen… or wrap a setter… or something. Again, your question is vague. Show what you want to do.
···
On May 31, 2018, at 06:25, Jackson Will <jsonw1@protonmail.com> wrote:
X.class.superclass is `Module`. Can I say that Module is like Object in Java which is implicitly inherited by all classes?
With respect to my question about accessing to state method - actually I want to do something like
y = Y.new
y.state
but it throws error `undefined method `state' for #<Y:0x00000002794878>(NoMethodError)`
calling y.superrclass.state throws `undefined method `superclass' for #<Y:0x00000000c24750> (NoMethodError)`
Just curious if anyway I can call state method from child instance i.e. y explicitly.
Thanks
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
···
On June 1, 2018 11:18 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
> On May 31, 2018, at 06:25, Jackson Will jsonw1@protonmail.com wrote:
>
> My question is why following code's class X can directly refer to state (for defining method block) without any include or require keyword?
Because of this property:
> > class X; end
=> nil
> > X.class
=> Class
> > X.class.superclass
=> Module
> And how can I access to state method value (in class X) after those state blocks are initialized
Not sure this question makes sense… “State method value”?
> (so I can observe values gets defined or executed in that block)?
you can generate a block that observes those changes as they happen… or wrap a setter… or something. Again, your question is vague. Show what you want to do.
No, Module is inherited by Class only. BasicObject is the one that's inherited by all classes (and Object by most).
Methods defined in the Class class can be invoked on all classes (but not their instances). And methods in the Module class on all classes and modules.
So in your example you can call Y.state because Y is a class (and Class inherits Module), but you can't call y.state because y is not a class or module.
···
On 01.06.2018 14:05, Jackson Will wrote:
X.class.superclass is `Module`. Can I say that Module is like Object in Java which is implicitly inherited by all classes?