# Dynamic string (depending on a variable)

**URL:** <https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706>\
**Category:** Ruby\
**Created:** [February 19, 2009, 1:39am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706 "2009-02-19T01:39:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 1:39am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/1 "2009-02-19T01:39:36Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 1:56am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/2 "2009-02-19T01:56:39Z")

</div>

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 🙂

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.

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 2:56am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/3 "2009-02-19T02:56:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 3:27am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/4 "2009-02-19T03:27:18Z")

</div>

Thx it works!

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 3:05am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/5 "2009-02-19T03:05:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 4:06am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/6 "2009-02-19T04:06:45Z")

</div>

sentence = “Your ticket expired " + (nb\_of\_days\>0? nb\_of\_days.to\_s + "  
days  
ago.” : “today.”)

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 19, 2009, 3:36am UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/7 "2009-02-19T03:36:15Z")

</div>

From: Guillaume L. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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…

---

<div class="post-metadata">

**Author:** ![loader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5daacb/32.png) [@loader](https://www.ruby-forum.com/u/loader)\
**Post date:** [February 20, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/dynamic-string-depending-on-a-variable/160706/8 "2009-02-20T15:34:06Z")

</div>

2009/2/19 Dylan E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
