Project

General

Profile

Actions

Bug #11086

closed

Symbol#instance_eval gives 'TypeError: can't define singleton' when coerced with String#to_sym

Added by ThijsWouters (Thijs Wouters) about 9 years ago. Updated almost 9 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]
[ruby-core:68961]

Description

When I coerce a String into a Symbol with String#to_sym, a call to Symbol#instance_eval fails with 'TypeError: can't define singleton'. If I first define the Symbol, it works as expected.

Failure:

2.2.1 :001 > "test_symbol".to_sym.instance_eval{true}
TypeError: can't define singleton
	from (irb):1:in `instance_eval'
	from (irb):1
	from /home/xxx/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
2.2.1 :002 > :test_symbol.instance_eval{true}
TypeError: can't define singleton
	from (irb):2:in `instance_eval'
	from (irb):2
	from /home/xxx/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
2.2.1 :003 > exit

Other way around:

2.2.1 :001 > :test_symbol.instance_eval{true}
 => true 
2.2.1 :002 > "test_symbol".to_sym.instance_eval{true}
 => true 
2.2.1 :003 > exit 

This also fails in 2.2.0, but does not in 2.1.5.


Files

test.patch (444 Bytes) test.patch ThijsWouters (Thijs Wouters), 04/22/2015 02:00 PM

Updated by ThijsWouters (Thijs Wouters) about 9 years ago

I added a patch with a failing test.

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED
Actions #3

Updated by nobu (Nobuyoshi Nakada) about 9 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r50372.


vm_eval.c: allow symbols to instance_eval/exec

  • vm_eval.c (rb_obj_instance_eval, rb_obj_instance_exec): allow
    symbols to just instance_eval/exec, execept for definition of
    singletons. [ruby-core:68961] [Bug #11086]
Actions #4

Updated by nagachika (Tomoyuki Chikanaga) almost 9 years ago

  • Backport changed from 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: REQUIRED to 2.0.0: DONTNEED, 2.1: DONTNEED, 2.2: DONE

Backported into ruby_2_2 branch at r50561.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0