Skip to content

Instantly share code, notes, and snippets.

@joesus
joesus / example.rb
Created January 2, 2022 16:56
Assigning methods to variables and invoking them in Ruby
# Assuming we're in an irb terminal.
# Keep in mind:
# * The return value of assignment in Ruby is the thing you assigned.
# * Ruby includes syntactic sugar to invoke methods with no parentheses. So `puts` is equivalent to `puts()`
# So when we write `foo = puts`, we assign `foo` the return value of puts which is always nil.
# What we're actually saying (without the sugar) is:
# `foo = puts()`
var components = URLComponents()
components.port = -8080
components.url!
var components = URLComponents()
components.port = Int.max
components.url!
var components = URLComponents()
components.port = -0
components.url!
var components = URLComponents()
components.port = 0
components.url!
var components = URLComponents()
components.scheme = "https://"
components.url!
var components = URLComponents()
components.scheme = ""
components.url!
var components = URLComponents()
components.scheme = "1234poop"
components.url!
var components = URLComponents()
components.scheme = "poop1234"
components.url!
var components = URLComponents()
components.scheme = "poop1234"
components.host = "\t\t\n\nWHat?#$"
components.path = "/\\users-#what?"
components.port = Int.max
components.url!