combine array(string) to string?

P

Pat Kiatchaipipat

str= "ABCDE"
arr = Array.new

arr = text.split ""

and how can I cambine array 'arr' into string again??
 
T

Todd Benson

str= "ABCDE"
arr = Array.new

You really don't need to declare your type. The arr = Array.new
statement is unnecessary.
arr = text.split ""

I'm not sure, but I think you meant arr = str.split ""
and how can I cambine array 'arr' into string again??

If the global $, is okay, you can just do arr.join, but if you can't
be sure, then arr.join ""

Todd
 
R

Rick DeNatale

You really don't need to declare your type. The arr = Array.new
statement is unnecessary.

In fact, there's no way to declare types of variables in Ruby.

At any given time a variable is bound to a particular object, but
which object (with its 'type', whatever that means) can change by
reassignment to the variable.

Somehow, it seems that this notion of 'declaring' a variable type by
assigning a 'prototype' object before setting the variable to the
'real' value seems to have become a common misconception in several
threads here on ruby-talk. Not sure why.
 
R

Robert Dober

If the global $, is okay, you can just do arr.join, but if you can't
be sure, then arr.join ""

There is no need to use the empty string as parameter it is the
default, OP should however know that split and join do not have the
same default parameters

space = " "
empty = ""
split is the same as split( space )
and
join is the same as join( empty )
:(
Cheers
Robert
 
R

Robert Klemme

You really don't need to declare your type. The arr = Array.new
statement is unnecessary.

Sorry to be nitpicky, this is in no way a type declaration. It is just
a superfluous object creation and assignment.

Kind regards

robert
 
T

Todd Benson

Sorry to be nitpicky, this is in no way a type declaration. It is just
a superfluous object creation and assignment.

Okay, "the instantiation of the Array object is unnecessary". From
some newbies, I've seen them pull this type of code practice from
fortran, pascal, c, etc... so I used the phrase "declare your type".
 
R

Robert Klemme

Okay, "the instantiation of the Array object is unnecessary". From
some newbies, I've seen them pull this type of code practice from
fortran, pascal, c, etc... so I used the phrase "declare your type".

I can see where that comes from and I did not want to disregard the
point you were trying to make. I just thought it a bit unfortunate that
your wording kind of encourages the practice by insinuating that there
is indeed something like a variable type declaration in Ruby. :)

Cheers

robert
 
T

Todd Benson

I can see where that comes from and I did not want to disregard the
point you were trying to make. I just thought it a bit unfortunate that
your wording kind of encourages the practice by insinuating that there
is indeed something like a variable type declaration in Ruby. :)

Cheers

robert

Ahh, I see. After reading my post again, I realize it wasn't that
clear. I was trying to cut down static practice.

There exists, however, typing in Ruby; it's just not temporally
static. It's interesting, I've even noticed people use the word
"type" to describe a prototype (class) on this list.

Todd
 
W

William James

There is no need to use the empty string as parameter it is the
default,

No, $, is the default.

irb(main):001:0> [3,4].join
=> "34"
irb(main):002:0> $, = '-'
=> "-"
irb(main):003:0> [3,4].join
=> "3-4"
 
R

Robert Klemme

Ahh, I see. After reading my post again, I realize it wasn't that
clear. I was trying to cut down static practice.

Absolutely agree.
There exists, however, typing in Ruby; it's just not temporally
static.

Yes, that's true. I would have to think hard to find a typeless
programming language. Can't remember any one - and it probably would
not be useful anyway: if there is not at least a single type there are
no values. And without values there is no state to manipulate... :)
It's interesting, I've even noticed people use the word
"type" to describe a prototype (class) on this list.

Yeah, the subtleties of all types of type related terms in CS. :)

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top