# Question about how ruby sources is parsed

**URL:** <https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357>\
**Category:** Ruby\
**Created:** [February 27, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357 "2008-02-27T16:21:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThoML](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@ThoML](https://www.ruby-forum.com/u/ThoML)\
**Post date:** [February 27, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357/1 "2008-02-27T16:21:05Z")

</div>

Hi,

I recently came came up with this piece of (monkey-inspired) code:

```
class String0 < String
    def foo
        123
    end
end

Object.send :remove_const, :String
String = String0

```

This makes the following two statements work as intended:

```
p String0.new("foo").foo # => 123
p String.new("foo").foo # => 123

```

The following doesn’t work though:

```
p "foo".foo
p eval %{"foo".foo}

undefined method `foo' for "foo":String (NoMethodError)

```

Has somebody an idea if it is possible to make the ruby parser use  
the  
new String class somehow? I assume there is no way to make this work  
but  
then: I’d be happy if somebody proves me wrong.

Regards,  
Thomas.

---

<div class="post-metadata">

**Author:** ![ThoML](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@ThoML](https://www.ruby-forum.com/u/ThoML)\
**Post date:** [February 27, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357/2 "2008-02-27T16:34:02Z")

</div>

That’s weird looking stuff, I can’t imaging that it’s good to do thing  
that way.

What’s wrong with

class String  
def foo  
123  
end  
end

?

Jason

---

<div class="post-metadata">

**Author:** ![ThoML](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@ThoML](https://www.ruby-forum.com/u/ThoML)\
**Post date:** [February 27, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357/3 "2008-02-27T16:51:07Z")

</div>

> What’s wrong with
> 
> class String  
> def foo  
> 123  
> end  
> end

Nothing if that’s what you want.

The above question was inspired by certain posts to the monkey-thread  
and the idea of stacked classes/methods, the proposal of module-  
specific hacks and the idea of Module.import/rename (I looked at the  
original library referenced by Pit Captain but didn’t find the  
extended one). Whether it’s useful … Currently I’m rather interested  
in if it’s possible.

Regards,  
Thomas.

---

<div class="post-metadata">

**Author:** ![ThoML](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@ThoML](https://www.ruby-forum.com/u/ThoML)\
**Post date:** [February 27, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357/4 "2008-02-27T21:02:28Z")

</div>

On Feb 27, 2008, at 07:19 AM, ThoML wrote:

> undefined method `foo’ for “foo”:String (NoMethodError)
> 
> Has somebody an idea if it is possible to make the ruby parser use  
> the new String class somehow? I assume there is no way to make this  
> work  
> but then: I’d be happy if somebody proves me wrong.

You can’t do this without modifying the interpreter.

---

<div class="post-metadata">

**Author:** ![ThoML](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@ThoML](https://www.ruby-forum.com/u/ThoML)\
**Post date:** [February 27, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/question-about-how-ruby-sources-is-parsed/130357/5 "2008-02-27T17:02:57Z")

</div>

I think “” is hard-wired to constructing String class objects, as the {}  
is to creating Hash and [] is to creating Array.

Best regards,

Jari W.
