Forum: Ruby Dynamic string (depending on a variable)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-19 01:39
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 :)
770048af205ed307b8cf35ae2282ee2f?d=identicon&s=25 Michael Malone (Guest)
on 2009-02-19 01:56
(Received via mailing list)
Guillaume Loader 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 :)
>
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.
=======================================================================
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-19 02:56
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!
770048af205ed307b8cf35ae2282ee2f?d=identicon&s=25 Michael Malone (Guest)
on 2009-02-19 03:05
(Received via mailing list)
Guillaume Loader 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.
=======================================================================
007bac1f27905cd4d5d88315e9b0093a?d=identicon&s=25 Guillaume Loader (loader)
on 2009-02-19 03:27
Thx it works!
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2009-02-19 03:36
(Received via mailing list)
From: Guillaume Loader [mailto:picpic72@hotmail.com]
# 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...
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-02-19 04:06
(Received via mailing list)
sentence = "Your ticket expired " + (nb_of_days>0? nb_of_days.to_s + "
days
ago." : "today.")
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-20 15:34
(Received via mailing list)
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
This topic is locked and can not be replied to.