Donato Donato - 11 months ago 92
Ruby Question

ruby string interpolation issue with net/imap library

I am using ruby's built-in imap/net library. There seems to be an issue with string interpolation. The following works fine:

imap ='',993, true)
ids =["SINCE", "26-Sep-2016"])
=> [1, 2]

I used a string literal above. However, when I replace the string literal with the following, an error occurs:

imap ='',993, true)
ids =["SINCE",'%d-%b-%y')])
=> Net::IMAP::BadResponseError: Could not parse command

What does only a string literal work when the result of is a string literal? This is a standard library in ruby, so for sure it must work. What am I missing?

Answer Source

%y in Time#strftime is the 2-digit year, and Net::IMAP#search requires a 4-digit year. You want %Y:'%d-%b-%y') # => "28-Sep-16"'%d-%b-%Y') # => "28-Sep-2016"

This isn't just a requirement of Net::IMAP, it's actually defined by the IMAP specification on pages 84 and 85.