How to use split method?

I’m trying to use split method and here is my sample code.When I use
irb I gett following output

irb(main):001:0> result = “OK:1234”
=> “OK:1234”
irb(main):002:0> result.split(’:’)
=> [“OK”, “1234”]
irb(main):003:0> result[1]
=> 75

I am not able to figure out when am trying to access 2nd element of
the result array, why does it return “75”, instead of “1234”.

Am I missing something over here? Any help is much appreciated

Thanks,
Sushil

On 3 June 2010 12:43, SushilKarwa [email protected] wrote:

I’m trying to use split method and here is my sample code.When I use
irb I gett following output

irb(main):001:0> result = “OK:1234”
=> “OK:1234”
irb(main):002:0> result.split(‘:’)
=> [“OK”, “1234”]
irb(main):003:0> result[1]
=> 75

You need to assign the result of the split to something:

irb(main):002:0> result = result.split(‘:’)

Otherwise your “result[1]” operation is returning the code of the
second character of the original “OK:1234” string.

BTW This seems to be a purely Ruby question, and nothing to do with
Rails.

On Jun 3, 1:43 pm, SushilKarwa [email protected] wrote

why does it return “75”, instead of “1234”.
Because 75 if the character code of the second character in the
string.
Use split with a bang intead (split!) if you wish to change the
variable itself.

On 3 June 2010 13:09, Sharagoz [email protected] wrote:

On Jun 3, 1:43 pm, SushilKarwa [email protected] wrote

why does it return “75”, instead of “1234”.
Because 75 if the character code of the second character in the
string.

Correct identification of the problem.

Use split with a bang intead (split!) if you wish to change the
variable itself.

Incorrect solution (unless you’ve overloaded Ruby with your own split!
method)
http://ruby-doc.org/core/classes/String.html#M000803

Thanks for your help. I was not assigning the result of split.

I know this is simple Ruby question, but I was looking for a quick
help and that’ why posted my question in this group. I am not a member
of “Ruby” group.

Thanks agian,
Sushil

SushilKarwa wrote:

Thanks for your help. I was not assigning the result of split.

I know this is simple Ruby question, but I was looking for a quick
help and that’ why posted my question in this group. I am not a member
of “Ruby” group.

That doesn’t make it OK to ask off-topic questions here.

Thanks agian,
Sushil

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]