Ruby on rails 如何在If-else状态检查中分配变量
如何在If-Else语句check中分配局部变量,然后在所述If-Else块中使用该局部变量。这就是我的意思:Ruby on rails 如何在If-else状态检查中分配变量,ruby-on-rails,ruby,Ruby On Rails,Ruby,如何在If-Else语句check中分配局部变量,然后在所述If-Else块中使用该局部变量。这就是我的意思: 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_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
谢谢 一种方法是分为两种方法:
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
还要确保在方法名称上遵循一些约定,因为这会使眼睛流血…一种方法是将其分为两种方法:
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
还要确保在方法名称上遵循一些约定,因为这会使眼睛流血…您的思路是正确的,但括号位置错误,因此您将
响应设置为确定。相反,您希望:
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
您在正确的轨道上,但括号位置不正确,因此您将响应设置为“确定”
。相反,您希望:
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
我会这样做:
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
在这里,我们只是循环遍历这些方法,返回的第一个方法也将转换并分配给response。如果未找到任何内容,则返回默认值我将这样实现:
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
在这里,我们只是循环遍历这些方法,返回的第一个方法也将转换并分配给response。如果未找到任何内容,则返回默认值我喜欢这种方法,因为它很简洁。我对这些方法的名称太难过了——我正试图将它们与我调用的不同Google Maps API端点进行标准化,但经过二次考虑,可能会将它们更改为更容易接受的名称。我喜欢这种方法,因为它简洁。我对这些方法的名称太难过了——我正试图根据我调用的不同Google Maps API端点对它们进行标准化,但经过二次考虑,可能会将它们更改为更令人满意的名称。