From: kevid on
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.
From: Robert Klemme on
PLEASE DO NOT SHOUT.

2010/6/2 kevid <alumsimportant(a)yahoo.ca>:

> 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


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Brian Candler on
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.
--
Posted via http://www.ruby-forum.com/.

From: Benoit Daloze on
[Note: parts of this message were removed to make it a legal post.]

On 2 June 2010 17:20, Brian Candler <b.candler(a)pobox.com> wrote:

> 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.
>
>
But you likely do not need that ;)
What is your aim ?