From: 7stud -- Date: 2009-09-07T13:06:15+09:00 Subject: Re: i need to strip \n and nil 7stud -- wrote: > Bigmac Turdsplash wrote: >> 7stud -- wrote: >>> Michael Fellinger wrote: >>>> >>>> >>>> ["59023\n", nil].join.to_i >>> >>> You're kidding, right? >> >> I had it figured out a few minutes after this post... i didnt know the >> differnce between a string and array... i convert the array to a string >> then used compact >> > > Ridiculous. You would never compact an array just to get the first > element. If you want the first element, then grab it. See Bertram's > posts. > > >> i thought i was working the string class so i was reading threw the >> wrong page... >> >> any ways... because of this premature post, i spent a few hours try to >> solve a new problem before i ask for help... >> >> strings="a9digh0t5" >> # every 3 bytes i need broke into separate strings[x] >> puts(string[1]) #==> a9d >> puts(string[2]) #==> ihg >> puts(string[3]) #==> 0t5 >> >> how can i brake a long string into blocks equal to 3 bytes? > > If the length of the string will always be a multiple of 3, you can do > this: > > str = "a9digh0t5" > > str.scan(/.{3}/) do |substr| > puts substr > end > > --output:-- > a9d > igh > 0t5 > > > Otherwise, you'll have to deal with ruby's infernal each_byte() method. Actually, you can take advantage of a regex's greedy nature, and do this instead: str = "a9digh0t5xx" str.scan(/.{1,3}/) do |substr| puts substr end --output:-- a9d igh 0t5 xx -- Posted via http://www.ruby-forum.com/.