[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

combine array(string) to string?

Pat Kiatchaipipat

3/20/2008 9:45:00 AM

str= "ABCDE"
arr = Array.new

arr = text.split ""

and how can I cambine array 'arr' into string again??
--
Posted via http://www.ruby-....

10 Answers

Stefano Crocco

3/20/2008 9:47:00 AM

0

On Thursday 20 March 2008, Pat Kiatchaipipat wrote:
> str= "ABCDE"
> arr = Array.new
>
> arr = text.split ""
>
> and how can I cambine array 'arr' into string again??

arr.join ''

Stefano


Todd Benson

3/20/2008 10:49:00 AM

0

On Thu, Mar 20, 2008 at 4:44 AM, Pat Kiatchaipipat <hb.pat87@hotmail.com> wrote:
> 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

Rick DeNatale

3/20/2008 11:16:00 AM

0

On 3/20/08, Todd Benson <caduceass@gmail.com> wrote:
> On Thu, Mar 20, 2008 at 4:44 AM, Pat Kiatchaipipat <hb.pat87@hotmail.com> wrote:
> > str= "ABCDE"
> > arr = Array.new
>
>
> 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.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Dober

3/20/2008 12:09:00 PM

0

>
> 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


--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert Klemme

3/20/2008 5:29:00 PM

0

On 20.03.2008 11:49, Todd Benson wrote:
> On Thu, Mar 20, 2008 at 4:44 AM, Pat Kiatchaipipat <hb.pat87@hotmail.com> wrote:
>> str= "ABCDE"
>> arr = Array.new
>
> 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

Todd Benson

3/20/2008 8:49:00 PM

0

On Thu, Mar 20, 2008 at 12:29 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 20.03.2008 11:49, Todd Benson wrote:
> > On Thu, Mar 20, 2008 at 4:44 AM, Pat Kiatchaipipat <hb.pat87@hotmail.com> wrote:
> >> str= "ABCDE"
> >> arr = Array.new
> >
> > 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.

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".

>
> Kind regards
>
> robert
>

Todd
>

Robert Klemme

3/21/2008 8:38:00 AM

0

On 20.03.2008 21:48, Todd Benson wrote:
> On Thu, Mar 20, 2008 at 12:29 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> On 20.03.2008 11:49, Todd Benson wrote:
>> > On Thu, Mar 20, 2008 at 4:44 AM, Pat Kiatchaipipat <hb.pat87@hotmail.com> wrote:
>> >> str= "ABCDE"
>> >> arr = Array.new
>> >
>> > 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.
>
> 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

Todd Benson

3/22/2008 2:45:00 AM

0

On Fri, Mar 21, 2008 at 3:39 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>
> > On Thu, Mar 20, 2008 at 12:29 PM, Robert Klemme
> > <shortcutter@googlemail.com> wrote:
> 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

William James

3/22/2008 8:19:00 AM

0

On Mar 20, 6:09 am, Robert Dober <robert.do...@gmail.com> wrote:
> > 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,

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"

Robert Klemme

3/22/2008 10:28:00 AM

0

On 22.03.2008 03:45, Todd Benson wrote:
> On Fri, Mar 21, 2008 at 3:39 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> > On Thu, Mar 20, 2008 at 12:29 PM, Robert Klemme
>> > <shortcutter@googlemail.com> wrote:
>> 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. :-)

> 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