Skip to content

Instantly share code, notes, and snippets.

@VelizarHristov
Last active August 29, 2015 14:17
Show Gist options
  • Save VelizarHristov/9868159d68615e2bb830 to your computer and use it in GitHub Desktop.
Save VelizarHristov/9868159d68615e2bb830 to your computer and use it in GitHub Desktop.
Return optional argument
# those are two implementations of the same thing, there might be more ways to do it
# I'm trying to decide which one would be best practice
# obviously those won't be nil in a real application
val1 = nil
opt1 = nil
def optional_argument1
[val1, opt1]
end
def optional_argument2(include_optional? = false)
if include_optional?
[val1, opt1]
else
val1
end
end
def usage_without_optional1
needed_value = optional_argument1[0]
# use needed_value somehow
end
def usage_with_optional1
[needed_value1, needed_value2] = optional_argument1
# use needed_values 1 and 2 somehow
end
def usage_without_optional2
needed_value = optional_argument2
# use needed_value1
end
def usage_with_optional2
[needed_value1, needed_value2] = optional_argument2(include_optional? = true)
# use needed_values 1 and 2 somehow
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment