如何在If Else语句 check 中分配局部变量,然后在所说的If Else block 中使用该变量。这就是我的意思:
def try_if_else(foo)
if bar = other_method(foo) == true
do_something_with_bar(bar)
else
do_something_else(foo)
end
end
这是我尝试使用的实际方法,但没有成功:
def try_to_find_site(location)
if (response = findplacefromtext_textquery(location)[:status]) == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = findplacefromtext_phonenumber(location)[:status]) == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = textsearch(location)[:status]) == 'OK'
convert_textsearch_to_struct(response)
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
这是我设法开始工作的方法,但并不理想,因为它两次调用了外部API来返回我要查找的结果:
def try_to_find_site(location)
if findplacefromtext_textquery(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_textquery(location))
elsif findplacefromtext_phonenumber(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_phonenumber(location))
elsif textsearch(location)[:status] == 'OK'
convert_textsearch_to_struct(textsearch(location))
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
如果需要查看我要做什么,这里是整个文件:
# Handles consuming the Google Api.
# require 'httparty'
module ApiHandlers
module GoogleMaps
class GoogleBusinessFinder
include HTTParty
debug_output $stdout
base_uri 'https://maps.googleapis.com'
default_params key: Rails.application.credentials.development[:google][:secret_key]
def initialize(location)
@location = location
end
def call
try_to_find_site(location)
end
# THESE BE PRIVATE Matey! YARR!!
private
LocationStruct = Struct.new(:name, :formatted_address, :place_id, :rating)
# Tries various the various (findplacefromtext_textquery, findplacefromtext_phonenumber, textsearch)
# methods of finding the business and returns an array of structs with findings.
def try_to_find_site(location)
if findplacefromtext_textquery(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_textquery(location))
elsif findplacefromtext_phonenumber(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_phonenumber(location))
elsif textsearch(location)[:status] == 'OK'
convert_textsearch_to_struct(textsearch(location))
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
def convert_findplacefromtext_to_struct(response)
response = response[:candidates][0]
[LocationStruct.new(response[:name], response[:formatted_address], response[:place_id], response[:rating])]
end
def convert_textsearch_to_struct(response)
response = response[:results]
response.map { |response| LocationStruct.new(response[:name], response[:formatted_address], response[:place_id], response[:rating]) }
end
# Tries to find the business using the business name and address
def findplacefromtext_textquery(location)
@options = {
query: { inputtype: 'textquery',
input: "#{location.location_name} #{readable_address(location)}",
fields: 'name,formatted_address,name,types,rating,place_id,id' }
}
self.class.get('/maps/api/place/findplacefromtext/json', @options).parsed_response.deep_symbolize_keys
end
# Tries to find the business using the business phone number
def findplacefromtext_phonenumber(location)
@options = {
query: { inputtype: 'phonenumber',
input: "+1#{location.phone_number}",
fields: 'name,formatted_address,name,types,rating,place_id' }
}
self.class.get('/maps/api/place/findplacefromtext/json', @options).parsed_response.deep_symbolize_keys
end
# Finds an array of businesses that match the parameters. Last chance to find it.
def textsearch(location)
@options = {
query: { query: "#{location.location_name} #{location.city} #{location.country}",
fields: 'name,formatted_address,name,types,rating,place_id,id' }
}
self.class.get('/maps/api/place/textsearch/json', @options).parsed_response.deep_symbolize_keys
end
def readable_address(location)
"#{location.address_line_1} #{location.city} #{location.region} #{location.country} #{location.postal_code}"
end
attr_reader :location
# is equal to:
# def location
# @location
# end
end
end
end
谢谢!
答案 0 :(得分:1)
一种方法是分为两种方法:
def try_to_find_site(location)
find_site(location) || [LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
def find_site(location)
text_query = findplacefromtext_textquery(location)
return convert_findplacefromtext_to_struct(text_query) if text_query[:status] == 'OK'
phone_number = findplacefromtext_phonenumber(location)
return convert_findplacefromtext_to_struct(phone_number) if phone_number[:status] == 'OK'
text_search = textsearch(location)
return convert_textsearch_to_struct(text_search) if text_search[:status] == 'OK'
end
还要确保对方法名称遵循一些约定,因为这会使眼睛流血...
答案 1 :(得分:1)
您在正确的轨道上,但是括号放在错误的位置,因此您将response
设置为"OK"
。相反,您想要:
def try_to_find_site(location)
if (response = findplacefromtext_textquery(location))[:status] == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = findplacefromtext_phonenumber(location))[:status] == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = textsearch(location))[:status] == 'OK'
convert_textsearch_to_struct(response)
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
答案 2 :(得分:0)
我会这样实现:
FINDERS = {findplacefromtext_textquery: :convert_findplacefromtext_to_struct,
findplacefromtext_phonenumber: :convert_findplacefromtext_to_struct,
convert_textsearch_to_struct: :textsearch}
def try_to_find_site(location)
response = FINDERS.detect do |finder,converter|
r = send(finder,location)
break send(converter,r) if r[:status] == 'OK'
end
response || [LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
在这里,我们只是循环遍历方法,返回的第一个方法也将转换并分配给响应。如果未找到任何内容,则返回默认值