1

Following is my code:

md5 = Digest::MD5.new
md5 << "!@#$"

Then comes the error:

SyntaxError: (irb):46: unterminated string meets end of file

What is wrong? And how can I calculate the md5 hash of the string "!@#$"?

sawa
  • 160,959
  • 41
  • 265
  • 366
HanXu
  • 5,357
  • 5
  • 48
  • 73
  • I can't tell you exactly what is going on but a workaround would be to either escape the pound character: `"!@\#$"` or use single-quotes: `'!@#$'`. The problem may arise from the fact that `$` is the prefix for global variables in Ruby and you can use interpolation without curly braces on them. http://stackoverflow.com/questions/10091156/why-does-string-interpolation-work-in-ruby-when-there-are-no-curly-braces – Philipp Antar Aug 05 '14 at 08:05

3 Answers3

3

The hash # sign in double quoted strings is used for variable and expression substitution. In this case, you are substituting the value of the global variable $" into the string, but you are not closing the string. The syntactically correct way of expressing that would be

"!@#$"" # Note the extra closing quotes

However, it seems that you actually don't want to do variable substitution anyway, in which case you should always use single quoted strings:

'!@#$'
Jörg W Mittag
  • 351,196
  • 74
  • 424
  • 630
1

Your problem is the string you got is in a double apostrophe (") - so it is interpreted. And you have a hash (#) inside, so it is trying to do expression substitution. Put the string in a single apostrophe:

md5 << '!@#$'
Grych
  • 2,781
  • 11
  • 21
1

Seems like you need to quote #:

> puts "!@\#$"
!@#$
konsolebox
  • 66,700
  • 11
  • 93
  • 101