Dynamic string (depending on a variable)

G

Guillaume Loader

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 :)
 
M

Michael Malone

Guillaume said:
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 :)
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.
=======================================================================
 
G

Guillaume Loader

I'm sorry for my english :(
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!
 
M

Michael Malone

Guillaume said:
I'm sorry for my english :(
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!
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.
=======================================================================
 
P

Peña, Botp

RnJvbTogR3VpbGxhdW1lIExvYWRlciBbbWFpbHRvOnBpY3BpYzcyQGhvdG1haWwuY29tXSANCiMg
QnV0IHN0aWxsIGl0IGRvZXNuJ3Qgd29yayA6DQojIHNlbnRlbmNlID0gIllvdXIgdGlja2V0IGV4
cGlyZWQgI3tuYl9vZl9kYXlzICsgJyBkYXlzIGFnbycgaWYNCiMgbmJfb2ZfZGF5cyA+IDB9I3sn
dG9kYXknIGlmIG5iX29mX2RheXM9PTB9Ig0KDQpteSBpbml0aWFsIHN1Z2dlc3Rpb24gaXMgdG8g
YWx3YXlzIHN0YXJ0IHNsb3dseSBieSBzcGxpdHRpbmcgdGhlIHByb2JsZW0gaW50byBzbWFsbCBw
YXJ0cy4uLg0KDQplZywNCg0KPiBuYl9vZl9kYXlzPTANCj0+IDANCg0KPiB0aW1lX3BocmFzZSA9
IG5iX29mX2RheXM9PTA/ICJ0b2RheSIgOiAiI3tuYl9vZl9kYXlzfSBkYXkocykgYWdvIg0KPT4g
InRvZGF5Ig0KDQo+IHNlbnRlbmNlID0gIllvdXIgdGlja2V0IGV4cGlyZWQgIit0aW1lX3BocmFz
ZQ0KPT4gIllvdXIgdGlja2V0IGV4cGlyZWQgdG9kYXkiDQoNCg0KeW91IGNhbiB0aGVuIGpvaW4g
dGhlbSBsYXRlciBpZiB5b3Ugd2FudC4uLg0KDQo=
 
D

Dylan Evans

[Note: parts of this message were removed to make it a legal post.]

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

Robert Klemme

2009/2/19 Dylan Evans said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top