<feed xmlns='http://www.w3.org/2005/Atom'>
<title>ruby.git/test/ruby/test_undef.rb, branch v3_4_9</title>
<subtitle>The Ruby Programming Language</subtitle>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/'/>
<entry>
<title>Lazily create singletons on instance_{exec,eval} (#5146)</title>
<updated>2021-12-02T23:53:39+00:00</updated>
<author>
<name>John Hawthorn</name>
<email>john@hawthorn.email</email>
</author>
<published>2021-12-02T23:53:39+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=733500e9d02b11ff60fbbdb8daa43c2e9cfbd750'/>
<id>733500e9d02b11ff60fbbdb8daa43c2e9cfbd750</id>
<content type='text'>
* Lazily create singletons on instance_{exec,eval}

Previously when instance_exec or instance_eval was called on an object,
that object would be given a singleton class so that method
definitions inside the block would be added to the object rather than
its class.

This commit aims to improve performance by delaying the creation of the
singleton class unless/until one is needed for method definition. Most
of the time instance_eval is used without any method definition.

This was implemented by adding a flag to the cref indicating that it
represents a singleton of the object rather than a class itself. In this
case CREF_CLASS returns the object's existing class, but in cases that
we are defining a method (either via definemethod or
VM_SPECIAL_OBJECT_CBASE which is used for undef and alias).

This also happens to fix what I believe is a bug. Previously
instance_eval behaved differently with regards to constant access for
true/false/nil than for all other objects. I don't think this was
intentional.

    String::Foo = "foo"
    "".instance_eval("Foo")   # =&gt; "foo"
    Integer::Foo = "foo"
    123.instance_eval("Foo")  # =&gt; "foo"
    TrueClass::Foo = "foo"
    true.instance_eval("Foo") # NameError: uninitialized constant Foo

This also slightly changes the error message when trying to define a method
through instance_eval on an object which can't have a singleton class.

Before:

    $ ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': no class/module to add method (TypeError)

After:

    $ ./ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

IMO this error is a small improvement on the original and better matches
the (both old and new) message when definging a method using `def self.`

    $ ruby -e '123.instance_eval{ def self.foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;

* Remove "under" argument from yield_under

* Move CREF_SINGLETON_SET into vm_cref_new

* Simplify vm_get_const_base

* Fix leaf VM_SPECIAL_OBJECT_CONST_BASE

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
* Lazily create singletons on instance_{exec,eval}

Previously when instance_exec or instance_eval was called on an object,
that object would be given a singleton class so that method
definitions inside the block would be added to the object rather than
its class.

This commit aims to improve performance by delaying the creation of the
singleton class unless/until one is needed for method definition. Most
of the time instance_eval is used without any method definition.

This was implemented by adding a flag to the cref indicating that it
represents a singleton of the object rather than a class itself. In this
case CREF_CLASS returns the object's existing class, but in cases that
we are defining a method (either via definemethod or
VM_SPECIAL_OBJECT_CBASE which is used for undef and alias).

This also happens to fix what I believe is a bug. Previously
instance_eval behaved differently with regards to constant access for
true/false/nil than for all other objects. I don't think this was
intentional.

    String::Foo = "foo"
    "".instance_eval("Foo")   # =&gt; "foo"
    Integer::Foo = "foo"
    123.instance_eval("Foo")  # =&gt; "foo"
    TrueClass::Foo = "foo"
    true.instance_eval("Foo") # NameError: uninitialized constant Foo

This also slightly changes the error message when trying to define a method
through instance_eval on an object which can't have a singleton class.

Before:

    $ ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': no class/module to add method (TypeError)

After:

    $ ./ruby -e '123.instance_eval { def foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

IMO this error is a small improvement on the original and better matches
the (both old and new) message when definging a method using `def self.`

    $ ruby -e '123.instance_eval{ def self.foo; end }'
    -e:1:in `block in &lt;main&gt;': can't define singleton (TypeError)

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;

* Remove "under" argument from yield_under

* Move CREF_SINGLETON_SET into vm_cref_new

* Simplify vm_get_const_base

* Fix leaf VM_SPECIAL_OBJECT_CONST_BASE

Co-authored-by: Matthew Draper &lt;matthew@trebex.net&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>Test for undef'ed method in class hierachy</title>
<updated>2018-11-16T04:21:57+00:00</updated>
<author>
<name>k0kubun</name>
<email>k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e</email>
</author>
<published>2018-11-16T04:21:57+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=b90bac322b133d7e03eb0a2d9f4fe55b2a4fafa0'/>
<id>b90bac322b133d7e03eb0a2d9f4fe55b2a4fafa0</id>
<content type='text'>
This probably should have been `#bar` on the instance of class `Undef2` all along
[Fix GH-2015]

From: Alex Snaps &lt;alex.snaps@gmail.com&gt;

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This probably should have been `#bar` on the instance of class `Undef2` all along
[Fix GH-2015]

From: Alex Snaps &lt;alex.snaps@gmail.com&gt;

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</pre>
</div>
</content>
</entry>
<entry>
<title>Add frozen_string_literal: false for all files</title>
<updated>2015-12-16T05:07:31+00:00</updated>
<author>
<name>naruse</name>
<email>naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e</email>
</author>
<published>2015-12-16T05:07:31+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=3e92b635fb5422207b7bbdc924e292e51e21f040'/>
<id>3e92b635fb5422207b7bbdc924e292e51e21f040</id>
<content type='text'>
When you change this to true, you may need to add more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
When you change this to true, you may need to add more tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</pre>
</div>
</content>
</entry>
<entry>
<title>forgot to add test_undef.rb in the previous revision.</title>
<updated>2009-11-18T01:14:54+00:00</updated>
<author>
<name>shugo</name>
<email>shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e</email>
</author>
<published>2009-11-18T01:14:54+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=4e073fbb93ea74a14c617127eeee928634aa875d'/>
<id>4e073fbb93ea74a14c617127eeee928634aa875d</id>
<content type='text'>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25832 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25832 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
</pre>
</div>
</content>
</entry>
</feed>
