Search code examples
rubytext-to-speech

How can I make my Ruby script announce values as it runs?


Say I have captured two values, 1000 and 09/01/2016 (and let's pretend that today is 10/01/2016).

How can I make Ruby convert the string "one thousand for 30 days" to audible speech within the console as it runs?


Solution

  • You could use the gem humanize to convert the int 1000 to its name in english,

    and you'd also nee something like date1 - date2 to get the difference in days.

    Something like:

    number.humanize + ' for ' + (Date.today - myDate).to_i.to_s
    

    In order to make the bash speak this string out loud, you could use the very say command you mentioned; ruby allows for easily calling bash commands from within the language, just do something like this:

    str = number.humanize + ' for ' + (Date.today - myDate).to_i.to_s
    `say #{str}`
    

    Otherwise, if you want a more native solution, you can use the espeak gem:

    speech = Speech.new(str)
    speech.speak # invokes espeak