# Def method help

**URL:** <https://www.ruby-forum.com/t/def-method-help/113180>\
**Category:** Rails\
**Created:** [August 28, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/def-method-help/113180 "2007-08-28T12:57:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naveenpalani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecd19e/32.png) [@naveenpalani](https://www.ruby-forum.com/u/naveenpalani)\
**Post date:** [August 28, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/def-method-help/113180/1 "2007-08-28T12:57:39Z")

</div>

I have problems in getting the value from one def method to another.

For example

For Example:

class UserController \< ApplicationController

def dialout(id) # “id” - is taken from database table field   
temp = id  
end

def speak  
How can i get the value of temp here  
end

end

I created an object for dialout as shown below from which i get the  
value id above.

obj = UserController.new  
obj.dialout(user.id)

---

<div class="post-metadata">

**Author:** ![naveenpalani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecd19e/32.png) [@naveenpalani](https://www.ruby-forum.com/u/naveenpalani)\
**Post date:** [August 28, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/def-method-help/113180/2 "2007-08-28T17:36:18Z")

</div>

@temp ?

---

<div class="post-metadata">

**Author:** ![naveenpalani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecd19e/32.png) [@naveenpalani](https://www.ruby-forum.com/u/naveenpalani)\
**Post date:** [August 29, 2007, 8:46am UTC](https://www.ruby-forum.com/t/def-method-help/113180/3 "2007-08-29T08:46:16Z")

</div>

temp is a variable which stores the value of id

---

<div class="post-metadata">

**Author:** ![naveenpalani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecd19e/32.png) [@naveenpalani](https://www.ruby-forum.com/u/naveenpalani)\
**Post date:** [August 30, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/def-method-help/113180/4 "2007-08-30T19:34:35Z")

</div>

As Raffael suggested, you would need to use an instance variable  
(@temp) rather than a local variable. The local variable would only be  
available from the context within which it is called, while the  
instance variable would be available instance-wide. This sort of  
situation is where a basic understanding of Ruby comes in handy.

A question that I won’t go into, but one that you might want to ask  
yourself, is whether or not these methods actually belong in your  
controller. Typically controller methods are either actions or private  
methods used in filters. Your methods might be better put in either  
the helper (if they relate to presentation) or the model (if they  
relate to business or database logic).

On Aug 28, 5:57 am, Naveen P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naveenpalani](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecd19e/32.png) [@naveenpalani](https://www.ruby-forum.com/u/naveenpalani)\
**Post date:** [August 29, 2007, 9:24am UTC](https://www.ruby-forum.com/t/def-method-help/113180/5 "2007-08-29T09:24:57Z")

</div>

I basically integrate Asterisk with RAGI (Ruby Asterisk Gateway  
Interface). I use this program inside the handlers directory which is  
the controller for RAGI.

The following is my program:

Code : - fold - unfold

1. #ivr\_handler.rb
2. 
3. require ‘rubygems’
4. require ‘active\_record’
5. require ‘ragi/call\_handler’
6. require ‘ragi/call\_connection’
7. require ‘action\_controller’
8. 
9. class IvrHandler \< RAGI::CallHandler
10. 
11. def dialout(arg,id)
12. @params = {}
13. #place\_call(phoneNumber, callerID, urn, hashData, callDate,  
uniqueID, maxRetries, retryTime, waitTime, extraChannelVars)
14. @params[:id] = id
15. 

RAGI::CallInitiate.place\_call(arg,‘naveen’,’/ivr/speak’,@params[:id],nil,‘5’,‘0’,‘1’,‘90’,‘1’)  
16. @usermaster = Usermaster.find(id)  
17. @usermaster.ivr\_status = 1  
18. @usermaster.save  
19. puts 'title from the table usermaster ---------dailout  
-------'removed\_email\_address@domain.invalid\_s  
20.  
21. end  
22.  
23. def speak  
24. count = 0  
25. flag = true  
26. msgr = true  
27. while (flag == true)  
28.  
29. if msgr == true  
30. puts ‘This is naveen from the asterisk group.’  
31. speak\_text (“Hi , This is a reminder on your appointment with  
Dr. John on wednesday 23rd of August”)  
32. play\_sound (“beep”)  
33. end  
34.  
35.  
36. speak\_text (“To confirm your appointment, Press 1. To request  
for reschedule, Press 3. To replay the message from begining, Press 9”)  
37. keypresses = get\_data(“beep”,5000,1)  
38. puts (‘ekyeie  
----------------------------------’+keypresses.to\_s)  
39. if (keypresses == “1”)  
40. speak\_text ‘Your appointment is confirmed. Have a goodday.’  
41. break  
42. elsif (keypresses == “3”)  
43. speak\_text ‘Doctors office will get in touch with you  
shortly’  
44. break  
45. end  
46. end  
47. flag =false  
48. end  
49.  
50. end

I call the method speak from the asterisk pbx configuration file by  
giving the url direction.

usermasters is the table i created in the database and generated a  
usermaster.rb model file in the models directory. I use another program  
in the lib directory to get the values from database. It is shown below:

Code : - fold - unfold

1. require “rubygems”
2. 
3. require “active\_record”
4. 
5. require “…/app/handlers/ivr\_handler”
6. 
# accessing the database.yml file which is located in config

folder  
7.  
8. db\_config = YAML::load(File.open("…/config/database.yml"))  
9.  
10. # accessing the database configuration in development mode using  
database.yml file  
11. ActiveRecord::Base.establish\_connection(db\_config[‘development’])  
12.  
13. # access the data using model in app/models  
14. require “…/app/models/usermaster”  
15.  
16. # substracting n number of days from current date  
17.  
18. #date = DateTime.now - 3  
19.  
20. # finding users created on the above date  
21. @users = Usermaster.find(:all,:conditions =\>[“ivr\_status=?”,“0”])  
22.  
23. # creating object for ivrhandler  
24. ivr\_obj = IvrHandler.new  
25.  
26. # iterating each user in users table  
27. @users.each do |user|  
28. # passing phone\_number to dialout method in ivrhandler class  
29.  
30. ivr\_obj.dialout(user.contact\_number.to\_s,user.id)  
31. end

I hope this explanation is more clear enough to you. Please suggest me  
now how will the speak method get the id from usermaster table. Iam  
already passing the value of id to dialout method, you can see that in  
the ivr\_handler.rb file.

Thanks and appreciate your response.
