Dynamic string (depending on a variable)

Hello everyone!

Do you have a better way to do this ?

sentence = “Your ticket has expired since #{nb_of_days if nb_of_days<0}
#{‘days’ if nb_of_days<0} #{‘today’ if nb_of_days==0}”

My goal is to print something like this : “Your ticket has expired since
4 days” or “Your ticket has expired since today”

I’ve tried this :

sentence = “Your ticket has expired since #{nb_of_days + ‘days’ if
nb_of_days<0} #{‘today’ if nb_of_days==0}”

But it doesn’t work!

Thank you for your help :slight_smile:

Guillaume L. wrote:

I’ve tried this :

sentence = “Your ticket has expired since #{nb_of_days + ‘days’ if
nb_of_days<0} #{‘today’ if nb_of_days==0}”

But it doesn’t work!

Thank you for your help :slight_smile:

I think you want to change that to a greater than…

sentence = “Your ticket has expired since #{nb_of_days + ’ days’ if
nb_of_days > 0}#{‘today’ if nb_of_days==0}”

And if that message is going to be read in English, then you should
change it to ‘#{nb_of_days} ago’ || ‘today’ without the since. i.e.
‘Your ticket expired 4 days ago’ or ‘Your ticket expired today’

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.

I’m sorry for my english :frowning:
Thanks for correcting!

But still it doesn’t work :
sentence = “Your ticket expired #{nb_of_days + ’ days ago’ if
nb_of_days > 0}#{‘today’ if nb_of_days==0}”

Any suggestions?

Thanks!

Thx it works!

Guillaume L. wrote:

Sorry, I didn’t actually run the code the first time! It was trying to
use Fixnum’s + method rather than String’s
This does work:

sentence = “Your ticket expired #{nb_of_days.to_s + ’ days ago’ if
nb_of_days > 0}#{‘today’ if nb_of_days==0}”

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.

sentence = “Your ticket expired " + (nb_of_days>0? nb_of_days.to_s + "
days
ago.” : “today.”)

From: Guillaume L. [mailto:[email protected]]

But still it doesn’t work :

sentence = "Your ticket expired #{nb_of_days + ’ days ago’ if

nb_of_days > 0}#{‘today’ if nb_of_days==0}"

my initial suggestion is to always start slowly by splitting the problem
into small parts…

eg,

nb_of_days=0
=> 0

time_phrase = nb_of_days==0? “today” : “#{nb_of_days} day(s) ago”
=> “today”

sentence = "Your ticket expired "+time_phrase
=> “Your ticket expired today”

you can then join them later if you want…

2009/2/19 Dylan E. [email protected]:

sentence = “Your ticket expired " + (nb_of_days>0? nb_of_days.to_s + " days
ago.” : “today.”)

irb(main):009:0> nb_of_days = 0
=> 0
irb(main):010:0> sentence = 'Your ticket expired ’ << (nb_of_days == 0
? ‘today’ : “#{nb_of_days} days ago”) << ‘.’
=> “Your ticket expired today.”
irb(main):011:0> nb_of_days = 5
=> 5
irb(main):012:0> sentence = 'Your ticket expired ’ << (nb_of_days == 0
? ‘today’ : “#{nb_of_days} days ago”) << ‘.’
=> “Your ticket expired 5 days ago.”

Or even

15:33:12 Temp$ ruby exp.rb
Your ticket expired today.
Your ticket expired yesterday.
Your ticket expired 2 days ago.
Your ticket expired 3 days ago.
Your ticket expired 4 days ago.
Your ticket expired 5 days ago.
15:33:14 Temp$ cat exp.rb
(0…5).each do |nb_of_days|
sentence = 'Your ticket expired ’ <<
case nb_of_days
when 0: ‘today’
when 1: ‘yesterday’
else “#{nb_of_days} days ago”
end << ‘.’
puts sentence
end
15:33:19 Temp$

Kind regards

robert