(noob) cast string to array?


(Simon Strandgaard) #1

Hi. What a fantastic language. This is my first day learning using it
and I love it so far.

Great… Welcome to Ruby.

[snip]

result = result.sub(/{/,’[’)
result = result.sub(/}/,’]’)

What you has written is the same as (notice the ‘!’ suffix)

result.sub!(/{/,’[’)
result.sub!(/}/,’]’)

Above can be chained, so it becomes

result.sub!(/{/,’[’).sub!(/}/,’]’)

Any suggestions are more than welcome.

Try ‘gsub’

result.gsub!(/{/,’[’).gsub!(/}/,’]’)

···

On Thu, 12 Feb 2004 00:31:29 +0000, Koncept wrote:


Simon Strandgaard


(Bermejo, Rodrigo) #2

sub { block way! }

result.sub!(/\{(.+)\}/) { "["+$1+"]" }

-ronnie.

Simon Strandgaard wrote:

···

On Thu, 12 Feb 2004 00:31:29 +0000, Koncept wrote:

Hi. What a fantastic language. This is my first day learning using it
and I love it so far.

Great… Welcome to Ruby.

[snip]

result = result.sub(/{/,’[’)
result = result.sub(/}/,’]’)

What you has written is the same as (notice the ‘!’ suffix)

result.sub!(/{/,’[’)
result.sub!(/}/,’]’)

Above can be chained, so it becomes

result.sub!(/{/,’[’).sub!(/}/,’]’)

Any suggestions are more than welcome.

Try ‘gsub’

result.gsub!(/{/,’[’).gsub!(/}/,’]’)


Simon Strandgaard


(Robert) #3

“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:pan.2004.02.12.00.39.08.258666@adslhome.dk

···

On Thu, 12 Feb 2004 00:31:29 +0000, Koncept wrote:

Hi. What a fantastic language. This is my first day learning using it
and I love it so far.

Great… Welcome to Ruby.

[snip]

result = result.sub(/{/,’[’)
result = result.sub(/}/,’]’)

What you has written is the same as (notice the ‘!’ suffix)

result.sub!(/{/,’[’)
result.sub!(/}/,’]’)

Above can be chained, so it becomes

result.sub!(/{/,’[’).sub!(/}/,’]’)

I always feel unconfortable chaining (g)sub! since the result here may
be nil.

I’d rather do

array = eval( result.gsub( /{([^}]*)}/, ‘[\1]’ ) )

robert

(Simon Strandgaard) #4

[snip]

I am curious to when ‘nil’ may be returned?

···

On Thu, 12 Feb 2004 12:59:21 +0100, Robert Klemme wrote:

“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag

result.sub!(/{/,’[’)
result.sub!(/}/,’]’)

Above can be chained, so it becomes

result.sub!(/{/,’[’).sub!(/}/,’]’)

I always feel unconfortable chaining (g)sub! since the result here may
be nil.


Simon Strandgaard


(Gavin Sinclair) #5

Here you go:

irb(main):001:0> “xyz”.gsub!(/a/, “b”)
=> nil

Gavin

···

On Thursday, February 12, 2004, 11:09:58 PM, Simon wrote:

I always feel unconfortable chaining (g)sub! since the result here may
be nil.

[snip]

I am curious to when ‘nil’ may be returned?


(Simon Strandgaard) #6

Bommer, I feel sleepy :slight_smile:

···

On Thu, 12 Feb 2004 21:37:24 +0900, Gavin Sinclair wrote:

On Thursday, February 12, 2004, 11:09:58 PM, Simon wrote:

I always feel unconfortable chaining (g)sub! since the result here may
be nil.

[snip]

I am curious to when ‘nil’ may be returned?

Here you go:

irb(main):001:0> “xyz”.gsub!(/a/, “b”)
=> nil


Simon Strandgaard