# Rb\_str\_replace not public?

**URL:** <https://www.ruby-forum.com/t/rb-str-replace-not-public/49930>\
**Category:** Ruby\
**Created:** [December 10, 2005, 8:13am UTC](https://www.ruby-forum.com/t/rb-str-replace-not-public/49930 "2005-12-10T08:13:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djberg96](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@djberg96](https://www.ruby-forum.com/u/djberg96)\
**Post date:** [December 10, 2005, 8:13am UTC](https://www.ruby-forum.com/t/rb-str-replace-not-public/49930/1 "2005-12-10T08:13:26Z")

</div>

Hi all,

I wanted to use rb\_str\_replace for a destructive method in a C  
extension I’m using. However, it doesn’t appear to be public. Why  
not? And what should I use instead?

I poked around intern.h a bit, and saw a few potential candidates, but  
wasn’t sure if there was a suitable replacement. I suppose I could use  
rb\_funcall, but that seems wrong, given that I know rb\_str\_replace  
exists in string.c.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![djberg96](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@djberg96](https://www.ruby-forum.com/u/djberg96)\
**Post date:** [December 10, 2005, 10:31am UTC](https://www.ruby-forum.com/t/rb-str-replace-not-public/49930/2 "2005-12-10T10:31:46Z")

</div>

Daniel B. wrote:

> Hi all,

Hi,

> I wanted to use rb\_str\_replace for a destructive method in a C  
> extension I’m using. However, it doesn’t appear to be public. Why  
> not? And what should I use instead?
> 
> I poked around intern.h a bit, and saw a few potential candidates, but  
> wasn’t sure if there was a suitable replacement. I suppose I could  
> use rb\_funcall, but that seems wrong, given that I know rb\_str\_replace  
> exists in string.c.

Why not simply :

VALUE str1; // string to replace  
VALUE str2; // replacement string

str1 = str2;  
[or, if you want a different object]  
str1 = rb\_str\_dup( str2 );

Maybe I missed something…

---

<div class="post-metadata">

**Author:** ![djberg96](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@djberg96](https://www.ruby-forum.com/u/djberg96)\
**Post date:** [December 10, 2005, 5:04pm UTC](https://www.ruby-forum.com/t/rb-str-replace-not-public/49930/3 "2005-12-10T17:04:11Z")

</div>

Harpo wrote:

> > 
> 
> str1 = str2;  
> [or, if you want a different object]  
> str1 = rb\_str\_dup( str2 );
> 
> Maybe I missed something…

In my case I need to replace ‘self’ within a subclass of String, so I  
don’t want to dup and I can’t do a direct assignment:

irb(main):001:0\> class Foo \< String; def test; self = “hello”; end; end  
SyntaxError: compile error  
(irb):1: Can’t change the value of self  
class Foo \< String; def test; self = “hello”; end; end

Thus, I cheat by using String#replace. 🙂

For now I guess I’ll just use rb\_funcall, which seems to work fine.

Regards,

Dan
