Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby String Object Empty or Not?

I have a string that is the output of Ruby on Rails render_to_string

When in the Ruby debugger this is what I get

pp string.inspect # ->  blank
pp string         # ->  blank
pp string.Class   # ->  String
pp string.empty?  # ->  false
pp string.nil?    # ->  false
pp string.blank?  # ->  false
pp string.to_s    # ->  blank

I know there is content in the string because logger.info string returns the string as expected, but I don't know how to get to it.

Any ideas? Thanks!

EDIT: I figured out that this problem is very localized. It only happens when I am using the ruby-debugger. And it happens to any string that has a percent symbol in it.

For example, If I made a string

string = 'this is a string with a % symbol'

the string can no longer be inspected, printed, or even unpack.

I am on Ruby 1.8 and Rails 2.3.

I think this may be just part of my lack of understanding about something mundane in Ruby.

like image 995
vinhboy Avatar asked Nov 05 '22 13:11

vinhboy


1 Answers

Looks like your string is definitely not empty.

Can you download the hexdump extension for class String, and do this:

require 'hexdump'

#... whatever you do in your program

puts string.hexdump

and post the output in your question?

This way we can see what's in the string, even if those are non-printable characters..

See:

http://www.unixgods.org/~tilo/Ruby/hexdump.html

like image 188
Tilo Avatar answered Nov 14 '22 23:11

Tilo