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

Good morning

···

On Fri, Dec 05, 2014 at 08:46:48PM -0500, Jorge Colon wrote:

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

------------------
class Thing
  def initialize()
    puts "baeh"
  end

  def do_something(arg = nil)
    puts "Doo! " << arg.to_s
  end

  def self::do_it()
    Thing.new().do_something('Did it!')
  end
end

# dumbly overwrite the previous class-definition
class Thing
  Thing.new().do_something
end

# or explicitly call a method on the defined class
Thing::do_it
---------------------

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>>

--
GnuPG/OpenPGP 4096R/3216CF02 2013-11-15 [expires: 2015-11-15]
Michael Uplawski (privat) <michael.uplawski@uplawski.eu>
sub 4096R/2751C550 2013-11-15 [expires: 2015-11-15]
[Next key will use elliptic-curve algorithm! Get GnuPG!!]

This doesn't overwrite anything, nor is it dumb. It is just reopening a class.

···

On Dec 6, 2014, at 00:59, Michael Uplawski <bat.guano@don.blech.e4ward.com> wrote:

# dumbly overwrite the previous class-definition
class Thing
   Thing.new().do_something
end