# Pre determine the size of a string

**URL:** <https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535>\
**Category:** Ruby\
**Created:** [June 2, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535 "2010-06-02T17:10:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 2, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535/1 "2010-06-02T17:10:26Z")

</div>

Hi all,

please, is there a way to pre-determine the size (or capacity) of a  
string in ruby?

i.e if a have a string “str” , i will set it to contain characters not  
more than 10.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 2, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535/2 "2010-06-02T17:19:48Z")

</div>

PLEASE DO NOT SHOUT.

2010/6/2 kevid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> please, is there a way to pre-determine the size (or capacity) of a  
> string in ruby?
> 
> i.e if a have a string “str” , i will set it to contain characters not  
> more than 10.

No, strings in Ruby are unbounded. If you need that functionality you  
will have to create a class which implements this.

(Note: Ruby != C) 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 2, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535/3 "2010-06-02T17:20:27Z")

</div>

kevid wrote:

> please, is there a way to pre-determine the size (or capacity) of a  
> string in ruby?

Nope, strings are resized dynamically as required. But you could make  
your own class which does this.

It could, for example, delegate to an underlying String object,  
intercepting calls which could make the string bigger, and either raise  
an error or truncate the error if it exceeds the chosen size.

---

<div class="post-metadata">

**Author:** ![kevid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kevid](https://www.ruby-forum.com/u/kevid)\
**Post date:** [June 2, 2010, 10:51pm UTC](https://www.ruby-forum.com/t/pre-determine-the-size-of-a-string/189535/4 "2010-06-02T22:51:06Z")

</div>

On 2 June 2010 17:20, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

But you likely do not need that 😉  
What is your aim ?
