1

I am using Twilio Say verbs repeatedly and want to refactor my code. Currently it looks somewhat like this

def sample
    render text: (Twilio::TwiML::Response.new do |r|
        r.Say 'Welcome to ABC Residence', voice: 'alice', language: 'en-GB'
        r.Say 'Thank you for visiting ABC Residence.', voice:'alice', language: 'en-GB'
        r.Hangup
    end).text
end

Notice tht I'm using voice: 'alice' and language: 'en-GB' twice. How can I put these into a variable to be used over and over again?

1 Answer 1

5

Take them in a hash and use it:

def sample
    props = {voice: 'alice', language: 'en-GB'}

    render text: (Twilio::TwiML::Response.new do |r|
        r.Say 'Welcome to ABC Residence', props
        r.Say 'Thank you for visiting ABC Residence.', props
        r.Hangup
    end).text
end
2
  • 1
    I don't think your props hash will be available since it's outside the sample method. Either move it inside or convert it to a method that returns the hash. Feb 17, 2016 at 4:02
  • 1
    Oh, my bad. Fixed it now. Feb 17, 2016 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.