class_exec !!

Hi Rubyists,

Can

What is the use of class_exec? and When to use class_exec?

···

--
Thanks with Regards,

Vinay KP
*'Work speaks louder than words'*

irb(main):001:0> class C; end
=> nil
irb(main):002:0> C.instance_exec { def foo; 123; end }
=> :foo
irb(main):003:0> C.new.foo
NoMethodError: undefined method `foo' for #<C:0x000000027133b8>
from (irb):3
from /usr/bin/irb:11:in `<main>'
irb(main):004:0> C.class_exec { def foo; 456; end }
=> :foo
irb(main):005:0> C.new.foo
=> 456

Cheers

robert

···

On Mon, Nov 20, 2017 at 6:14 PM, vinay KP <vinaykp3@gmail.com> wrote:

What is the use of class_exec? and When to use class_exec?

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

Well, I think in most cases

    MyClass.class_exec { ... }

is the same as

    class MyClass
      ...
    end

But you can't use `class ...` with anonymous classes:

    c = Class.new
    class c; end # ==> SyntaxError

And don't forget that in case of usage `#class_exec` you are passing **block**,
so you can get it from elsewhere:

    MyClass.class_exec(&some_block)

Personally I don't see any useful application for that but still... Maybe it can be
used when writing DSL.

I think that is pretty it. Good luck!

···

--------------------------------------
Dmitriy Non
non.dmitriy@gmail.com

On 20 Nov 2017, at 20:14, vinay KP <vinaykp3@gmail.com> wrote:

Hi Rubyists,

Can

What is the use of class_exec? and When to use class_exec?

--
Thanks with Regards,

Vinay KP
'Work speaks louder than words'

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

oh, yeah. Totally forgot.

Don't forget that when you use block you create a closure:

    x = 123
    class A
      puts x # NoMethodError
    end
   
    A.class_exec { puts x } # ok

···

--------------------------------------
Dmitriy Non
non.dmitriy@gmail.com

On 21 Nov 2017, at 00:16, Dmitriy Non <non.dmitriy@gmail.com> wrote:

Well, I think in most cases

   MyClass.class_exec { ... }

is the same as

   class MyClass
     ...
   end

But you can't use `class ...` with anonymous classes:

   c = Class.new
   class c; end # ==> SyntaxError

And don't forget that in case of usage `#class_exec` you are passing **block**,
so you can get it from elsewhere:

   MyClass.class_exec(&some_block)

Personally I don't see any useful application for that but still... Maybe it can be
used when writing DSL.

I think that is pretty it. Good luck!
--------------------------------------
Dmitriy Non
non.dmitriy@gmail.com

On 20 Nov 2017, at 20:14, vinay KP <vinaykp3@gmail.com> wrote:

Hi Rubyists,

Can

What is the use of class_exec? and When to use class_exec?

--
Thanks with Regards,

Vinay KP
'Work speaks louder than words'

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

Yup rspec does exactly that and as you said, it's involving additions to the DSL:

···

On 11/20/17 4:16 PM, Dmitriy Non wrote:

And don't forget that in case of usage `#class_exec` you are passing **block**,
so you can get it from elsewhere:

     MyClass.class_exec(&some_block)

Personally I don't see any useful application for that but still... Maybe it can be
used when writing DSL.