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
info@2upmedia.com
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

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:26, Jorge Colon <2upmedia@gmail.com> 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

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
info@2upmedia.com
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

···

On Dec 5, 2014, at 8:27 PM, Bryce Kerley wrote:

On Dec 5, 2014, at 20:26, Jorge Colon <2upmedia@gmail.com> 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

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

Correct:

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

···

On Dec 5, 2014, at 20:34, Jorge Colon <2upmedia@gmail.com> 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”?

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
info@2upmedia.com
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

···

On Dec 5, 2014, at 8:37 PM, Bryce Kerley wrote:

On Dec 5, 2014, at 20:34, Jorge Colon <2upmedia@gmail.com> 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
#<struct name="John Doe">
=> nil
[4] pry(main)> class << dude
[4] pry(main)* puts inspect
[4] pry(main)* end
#<Class:#<#<Class:0x007fb0d3ec6568>:0x007fb0d3ec64f0>>

I can’t think of one off the top of my head, but at the same time, I don’t normally see the value in metaprogramming with eigenclasses (that’s the per-object class you’re adding methods to). It’s tricky to write (as you’re finding out), and it’s even more tricky to test, fix, and debug later.

···

On Dec 5, 2014, at 20:46, Jorge Colon <2upmedia@gmail.com> 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 Doe").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 Doe", true

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

# anonymous class
dude = Struct.new(:name, :is_single).new "John Doe", true

# anonymous class, other constructor call
dude = Struct.new(:name, :is_single)["John Doe", true]

# not a regular Struct member
Person = Struct.new(:name) do
  attr_accessor :is_single
end

dude = Person.new "John Doe"
dude.is_single = true

# both
dude = Struct.new(:name) do
  attr_accessor :is_single
end.new "John Doe"

dude.is_single = true

# variant
dude = Struct.new(:name) do
  attr_accessor :is_single
end.new("John Doe").tap |o|
  o.is_single = true
end

Cheers

robert

···

On Sat, Dec 6, 2014 at 2:46 AM, Jorge Colon <2upmedia@gmail.com> wrote:

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

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

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
info@2upmedia.com
www.2upmedia.com

LinkedIn
PHP Zend Certified Engineer

···

On Dec 5, 2014, at 8:53 PM, Bryce Kerley wrote:

On Dec 5, 2014, at 20:46, Jorge Colon <2upmedia@gmail.com> wrote:

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

I can’t think of one off the top of my head, but at the same time, I don’t normally see the value in metaprogramming with eigenclasses (that’s the per-object class you’re adding methods to). It’s tricky to write (as you’re finding out), and it’s even more tricky to test, fix, and debug later.