Discussion:
Method define new question
Jackson Will
2018-05-31 13:25:51 UTC
Permalink
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
Ryan Davis
2018-06-01 09:18:02 UTC
Permalink
Post by Jackson Will
My question is why following code's class X can directly refer to state (for defining method block) without any include or require keyword?
class X; end
=> nil
Post by Jackson Will
X.class
=> Class
Post by Jackson Will
X.class.superclass
=> Module
Post by Jackson Will
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”?
Post by Jackson Will
(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.


Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-ta
Jackson Will
2018-06-01 12:05:40 UTC
Permalink
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 ‐‐‐‐‐‐‐
Post by Ryan Davis
Post by Jackson Will
My question is why following code's class X can directly refer to state (for defining method block) without any include or require keyword?
class X; end
=> nil
Post by Jackson Will
X.class
=> Class
Post by Jackson Will
X.class.superclass
=> Module
Post by Jackson Will
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”?
Post by Jackson Will
(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.
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-tal
Sebastian Hungerecker
2018-06-02 09:41:17 UTC
Permalink
Post by Jackson Will
X.class.superclass is `Module`. Can I say that Module is like Object in Java which is implicitly inherited by all classes?
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.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Loading...