| |
 |
|
| Computers Forum Index » Computer Languages (Ruby) » array question... |
|
Page 1 of 1 |
|
| Author |
Message |
| Ruby Mokx... |
Posted: Tue Oct 27, 2009 4:38 am |
|
|
|
Guest
|
Hey everyone I'm a 14 year-old beginner in ruby and I discovered that I
need to separate strings into arrays for a program that I am making.
For example, if I have the string 'abc', I'll want to make it into the
array ['a', 'b', 'c']. Is there any way to do this? I am using version
1.8.6, by the way. All help is welcome.
Thanks in advance!
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Phrogz... |
Posted: Tue Oct 27, 2009 4:38 am |
|
|
|
Guest
|
On Oct 26, 6:46 pm, "David A. Black" <dbl... at (no spam) rubypal.com> wrote:
Quote: need to separate strings into arrays for a program that I am making.
For example, if I have the string 'abc', I'll want to make it into the
array ['a', 'b', 'c']. Is there any way to do this? I am using version
1.8.6, by the way. All help is welcome.
The method you want is String#split:
'abc'.split(//) # => ['a', 'b', 'c']
Here are two other techniques, just for fun:
all_characters = "abc".scan(/./)
all_characters=[]
"abc".each_char{ |x| all_characters.push(x) } |
|
|
| Back to top |
|
|
|
| David A. Black... |
Posted: Tue Oct 27, 2009 4:46 am |
|
|
|
Guest
|
Hi --
On Tue, 27 Oct 2009, Ruby Mokx wrote:
Quote: Hey everyone I'm a 14 year-old beginner in ruby and I discovered that I
Welcome!
Quote: need to separate strings into arrays for a program that I am making.
For example, if I have the string 'abc', I'll want to make it into the
array ['a', 'b', 'c']. Is there any way to do this? I am using version
1.8.6, by the way. All help is welcome.
The method you want is String#split:
'abc'.split(//) # => ['a', 'b', 'c']
Split takes a regular expression or string as its argument, and splits
on that pattern. The empty regular expression causes it to split the
string into individual characters.
There are some more bells and whistles to it but that's the basic
idea.
David
--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com) |
|
|
| Back to top |
|
|
|
| Bertram Scharpf... |
Posted: Tue Oct 27, 2009 5:17 am |
|
|
|
Guest
|
Hi,
Am Dienstag, 27. Okt 2009, 09:38:30 +0900 schrieb Ruby Mokx:
Quote: Hey everyone I'm a 14 year-old beginner in ruby and I discovered that I
need to separate strings into arrays for a program that I am making.
For example, if I have the string 'abc', I'll want to make it into the
array ['a', 'b', 'c']. Is there any way to do this? I am using version
1.8.6, by the way. All help is welcome.
Hey Mokx, the answer has been given. Let me start to disturb you.
There's a difference between
str.scan /./ # and
str.scan /./u
Your enthusiasm ist delighting me but the real world is rough.
Cheer up.
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Mon Nov 23, 2009 5:29 pm
|
|