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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 3 June 2010 12:43, SushilKarwa <sushil.karwa-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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] > => 75You 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 3, 1:43 pm, SushilKarwa <sushil.ka...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 3 June 2010 13:09, Sharagoz <sharagoz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 3, 1:43 pm, SushilKarwa <sushil.ka...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Jun 3, 5:16 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3 June 2010 13:09, Sharagoz <shara...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Jun 3, 1:43 pm, SushilKarwa <sushil.ka...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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, > SushilBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.