Trying to understand method calls in a class body. Fundamentally, why doesn't this work?

My understanding is that method calls in a class body are called as soon
as the ruby interpreter interprets them. Are these not working because
method calls in a class body are executed before the definition of the
instance/class methods?

http://pastie.org/private/u19k8wtwapi6jxzozlcpqq#3,13,24,36

Regards,

Jorge

Senior Software Architect/Owner
2UP Media
c: 407.489.2677
[email protected]
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

On Dec 5, 2014, at 20:26, Jorge C. [email protected] wrote:

My understanding is that method calls in a class body are called as soon as the
ruby interpreter interprets them. Are these not working because method calls in a
class body are executed before the definition of the instance/class methods?

http://pastie.org/private/u19k8wtwapi6jxzozlcpqq#3,13,24,36
http://pastie.org/private/u19k8wtwapi6jxzozlcpqq#3,13,24,36

You’re defining instance methods, but method calls in a class body
invoke class methods; there’s no instance to invoke instance methods on.

On Dec 5, 2014, at 20:34, Jorge C. [email protected] wrote:

Bryce,

Since I’m essentially passing in an instance “dude” to the singleton class, is
the class body still in the context of a class and NOT the instance "dude?

Correct:

[3] pry(main)> puts dude.inspect
#
=> nil
[4] pry(main)> class << dude
[4] pry(main)* puts inspect
[4] pry(main)* end
#<Class:#<#Class:0x007fb0d3ec6568:0x007fb0d3ec64f0>>

Ok. That makes sense. Out of pure curiosity, is there any “hacky” way to
call instance methods in a class context?

Regards,

Jorge

Senior Software Architect/Owner
2UP Media
c: 407.489.2677
[email protected]
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

On Dec 5, 2014, at 20:46, Jorge C. [email protected] wrote:

Ok. That makes sense. Out of pure curiosity, is there any “hacky” way to call
instance methods in a class context?

I cant think of one off the top of my head, but at the same time, I dont
normally see the value in metaprogramming with eigenclasses (thats the
per-object class youre adding methods to). Its tricky to write (as youre
finding out), and its even more tricky to test, fix, and debug later.

Bryce,

Since I’m essentially passing in an instance “dude” to the singleton
class, is the class body still in the context of a class and NOT the
instance “dude”?

Regards,

Jorge

Senior Software Architect/Owner
2UP Media
c: 407.489.2677
[email protected]
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

Thanks for the clarification. It was very much appreciated. Hope you
enjoy your weekend.

Regards,

Jorge

Senior Software Architect/Owner
2UP Media
c: 407.489.2677
[email protected]
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

On Sat, Dec 6, 2014 at 2:46 AM, Jorge C. [email protected] wrote:

Ok. That makes sense. Out of pure curiosity, is there any “hacky” way to
call instance methods in a class context?

You’d first need an instance. Without creating one or passing an
instance on it won’t work.

Here is a way that does what I assume you want

Struct.new(:name).new(“John D.”).tap do |dude|
class << dude
attr_accessor :is_single
end

dude.send :is_single=, true
end

That code is still quite odd (esp. considering your advertised
position) and I would not recommend doing this. There are much
simpler ways to go about things.

Person = Struct.new :name, :is_single
dude = Dudish.new “John D.”, true

There are other approaches, depending on what you want to achieve:

anonymous class

dude = Struct.new(:name, :is_single).new “John D.”, true

anonymous class, other constructor call

dude = Struct.new(:name, :is_single)[“John D.”, true]

not a regular Struct member

Person = Struct.new(:name) do
attr_accessor :is_single
end

dude = Person.new “John D.”
dude.is_single = true

both

dude = Struct.new(:name) do
attr_accessor :is_single
end.new “John D.”

dude.is_single = true

variant

dude = Struct.new(:name) do
attr_accessor :is_single
end.new(“John D.”).tap |o|
o.is_single = true
end

Cheers

robert