From: Robert Klemme Date: 2011-01-14T02:12:15+09:00 Subject: Re: Weird error using String#[] On Thu, Jan 13, 2011 at 6:01 PM, Jason Mcdonald wrote: > Thanks, Robert. The original post has the script with both expected and > unexpected outcomes. I thought more of a small script which does not need network connection etc. and rather works with static text. > What you show with the encoding screwing up the > offsets makes total sense. :-) > What I'm at a loss for is why it affects the hard coded portion of the > string passed to puts: > > Example: > puts "ret: #{ret}" > > Output: > Oet: [part but not all of the expected string - 2 chars too short] Well, that's easy: irb(main):014:0> s = "\rA\tB" => "\rA\tB" irb(main):015:0> puts "ret: #{s}" Aet: B => nil irb(main):016:0> p "ret: #{s}" "ret: \rA\tB" => "ret: \rA\tB" irb(main):017:0> s = "\rAet: B" => "\rAet: B" irb(main):018:0> puts "ret: #{s}" Aet: B => nil irb(main):019:0> p "ret: #{s}" "ret: \rAet: B" => "ret: \rAet: B" To debug you should use p and not puts. > At this point I plan on using Nokogiri but I am really curious what is > causing what I describe above. This is a weirdness for how strings / > puts works that I'd like to understand and keep in mind going forward. It's probably rather about how your terminal works than how strings work. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/