Sush
Sush

Reputation: 1457

undefined local varibale in ruby code

in the follwoing code

@vmip_and_port = "173.16.2.80:9090"

def post(url, data)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(data)
  response = http.request(request)

  return response
end

def click_inapp(id)
 res = post("http://" + vmip_and_port + "/" , {"command" => "4x6papper-hp2420-form", "id" => id})
 return res.body
end

click_inapp("1")

am getting

rb:17:in `click_inapp': undefined local variable or method `vmip_and_port' for main:Object (NameError)
    from app_utilities.rb:21:in `<main>'

Upvotes: 0

Views: 66

Answers (3)

pramod
pramod

Reputation: 2318

Instance variable are self bounded. It will vary if self varied. In your case you define a instance variable but you didn't use it. If the IP address is persistent then better to make is constant and use it through out your class.

So make the vmip_and_port as constant as follows:

VmipAndPort = "173.16.2.80:9090"

def click_inapp(id)
 res = post("http://" + VmipAndPort + "/" , {"command" => "4x6papper-hp2420-form", "id" => id})
 return res.body
end

Upvotes: 1

Arie Xiao
Arie Xiao

Reputation: 14082

def click_inapp(id)
 res = post("http://" + @vmip_and_port + "/" , {"command" => "4x6papper-hp2420-form", "id" => id})
 return res.body
end

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You defined an instance variable but you are using a local variable. So better use it inside your method instead

def click_inapp(id)
 vmip_and_port = "173.16.2.80:9090"
 res = post("http://" + vmip_and_port + "/" , {"command" => "4x6papper-hp2420-form", "id" => id})
 return res.body
end

Upvotes: 1

Related Questions