[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamic string (depending on a variable

Guillaume Loader

2/19/2009 12:39:00 AM

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 :)
--
Posted via http://www.ruby-....

7 Answers

Michael Malone

2/19/2009 12:56:00 AM

0

Guillaume Loader wrote:
> 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.
=======================================================================


Guillaume Loader

2/19/2009 1:56:00 AM

0

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!


--
Posted via http://www.ruby-....

Michael Malone

2/19/2009 2:04:00 AM

0

Guillaume Loader wrote:
> 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.
=======================================================================


Guillaume Loader

2/19/2009 2:27:00 AM

0

Thx it works!
--
Posted via http://www.ruby-....

Peña, Botp

2/19/2009 2:36:00 AM

0

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

Dylan Evans

2/19/2009 3:04:00 AM

0

[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.")

On Thu, Feb 19, 2009 at 10:39 AM, Guillaume Loader <picpic72@hotmail.com>wrote:

> 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 :)
> --
> Posted via http://www.ruby-....
>
>


--
The UNIX system has a command, nice ... in order to be nice to the other
users. Nobody ever uses it." - Andrew S. Tanenbaum

Robert Klemme

2/20/2009 2:33:00 PM

0

2009/2/19 Dylan Evans <dylan.star@gmail.com>:
> 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

--
remember.guy do |as, often| as.you_can - without end