# Redefining rb\_str\_init()

**URL:** <https://www.ruby-forum.com/t/redefining-rb-str-init/78204>\
**Category:** Ruby\
**Created:** [December 4, 2006, 12:57am UTC](https://www.ruby-forum.com/t/redefining-rb-str-init/78204 "2006-12-04T00:57:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 12:57am UTC](https://www.ruby-forum.com/t/redefining-rb-str-init/78204/1 "2006-12-04T00:57:51Z")

</div>

Hi all,

I’m just messing around with a custom String class, so I did this to  
string.c:

static VALUE rb\_str\_init(int argc, VALUE\* argv, VALUE v\_str){  
VALUE v\_orig;

rb\_scan\_args(argc, argv, “01”, &v\_orig);

if(RTEST(v\_orig))  
rb\_str\_replace(v\_str, v\_orig);

printf(“Hello!\n”);

return v\_str;  
}

I rebuilt Ruby and installed it without issue. But, the “Hello” only  
prints if you use the long form of the constructor:

s1 = String.new(‘test’) # “Hello!”  
s2 = “test” # Nothing printed

What do I need to change to make the constructor shortcut work as  
expected?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 3:20am UTC](https://www.ruby-forum.com/t/redefining-rb-str-init/78204/2 "2006-12-04T03:20:06Z")

</div>

Hi,

In message “Re: Redefining rb\_str\_init()”  
on Mon, 4 Dec 2006 08:55:08 +0900, “Daniel B.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|What do I need to change to make the constructor shortcut work as  
|expected?

I don’t think it’s a good idea to invoke function for every string  
allocation. But if you really want to do, modify str\_alloc() in  
string.c.

In general, #initialize is not called for literals.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 4:22am UTC](https://www.ruby-forum.com/t/redefining-rb-str-init/78204/3 "2006-12-04T04:22:15Z")

</div>

Daniel B. wrote:  
…

> I take it there’s a bunch of setup happening within the interpreter that  
> makes a lot of calls to str\_alloc() before the interpeter ever runs?  
> That, or I’ve messed something up.

Strings like this…

$ RUBYLIB=’’ RUBYOPT=’’ ruby -e ‘a=[];ObjectSpace.each\_object(String)  
{|s| a\<\<s}; puts a’  
a=[];ObjectSpace.each\_object(String) {|s| a\<\<s}; puts a  
.  
/usr/local/lib/ruby/1.8/i686-linux  
/usr/local/lib/ruby/1.8  
/usr/local/lib/ruby/site\_ruby  
/usr/local/lib/ruby/site\_ruby/1.8/i686-linux  
/usr/local/lib/ruby/site\_ruby/1.8

-e  
a=[];ObjectSpace.each\_object(String) {|s| a\<\<s}; puts a  
ruby  
i686-linux  
2005-12-24  
1.8.4  
failed to allocate memory  
stack level too deep  
exception reentered  
Process::Sys  
.  
. (snip)  
.  
Errno::ESRCH  
Errno::ENOENT  
Errno::EPERM  
Errno  
NameError::message  
NameError

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 4, 2006, 4:04am UTC](https://www.ruby-forum.com/t/redefining-rb-str-init/78204/4 "2006-12-04T04:04:13Z")

</div>

Yukihiro M. wrote:

> string.c.  
> Well, I’m just tinkering around for now. However, I did realize  
> something interesting. I put a printf in the str\_alloc() function as  
> you mentioned and ran an empty Ruby program. Even an empty Ruby program  
> appears to invoke the str\_alloc function 107 times!

I take it there’s a bunch of setup happening within the interpreter that  
makes a lot of calls to str\_alloc() before the interpeter ever runs?  
That, or I’ve messed something up.

> In general, #initialize is not called for literals.
> 
> ```
> matz.
> 
> ```

Good to know, thanks,

- Dan
