Skip to content

Instantly share code, notes, and snippets.

@yuki24
Created April 23, 2020 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki24/fd59577395fb176f270c17acd499b9b0 to your computer and use it in GitHub Desktop.
Save yuki24/fd59577395fb176f270c17acd499b9b0 to your computer and use it in GitHub Desktop.
One-liner method definition in Ruby
module OneLinerExamples
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/request.rb#L150-L155
def body := get_header(RACK_INPUT)
def script_name := get_header(SCRIPT_NAME).to_s
def script_name=(s) := set_header(SCRIPT_NAME, s.to_s)
def path_info := get_header(PATH_INFO).to_s
def path_info=(s) := set_header(PATH_INFO, s.to_s)
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L126-L129
def has_header?(key) := headers.key? key
def get_header(key) := headers[key]
def set_header(key, v) := headers[key] = v
def delete_header(key) := headers.delete key
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L135-L156
def invalid? := status < 100 || status >= 600
def informational? := status >= 100 && status < 200
def successful? := status >= 200 && status < 300
def redirection? := status >= 300 && status < 400
def client_error? := status >= 400 && status < 500
def server_error? := status >= 500 && status < 600
def ok? := status == 200
def created? := status == 201
def accepted? := status == 202
def no_content? := status == 204
def moved_permanently? := status == 301
def bad_request? := status == 400
def unauthorized? := status == 401
def forbidden? := status == 403
def not_found? := status == 404
def method_not_allowed? := status == 405
def precondition_failed? := status == 412
def unprocessable? := status == 422
def redirect? := [301, 302, 303, 307, 308].include? status
# https://github.com/rails/rails/blob/24c1429a/actionpack/lib/action_dispatch/http/response.rb#L219-L221
def sending? := synchronize { @sending }
def committed? := synchronize { @committed }
def sent? := synchronize { @sent }
end
module OneLinerExamples
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/request.rb#L150-L155
def body:: get_header(RACK_INPUT)
def script_name:: get_header(SCRIPT_NAME).to_s
def script_name=(s):: set_header(SCRIPT_NAME, s.to_s)
def path_info:: get_header(PATH_INFO).to_s
def path_info=(s):: set_header(PATH_INFO, s.to_s)
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L126-L129
def has_header?(key):: headers.key? key
def get_header(key):: headers[key]
def set_header(key, v):: headers[key] = v
def delete_header(key):: headers.delete key
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L135-L156
def invalid?:: status < 100 || status >= 600
def informational?:: status >= 100 && status < 200
def successful?:: status >= 200 && status < 300
def redirection?:: status >= 300 && status < 400
def client_error?:: status >= 400 && status < 500
def server_error?:: status >= 500 && status < 600
def ok?:: status == 200
def created?:: status == 201
def accepted?:: status == 202
def no_content?:: status == 204
def moved_permanently?:: status == 301
def bad_request?:: status == 400
def unauthorized?:: status == 401
def forbidden?:: status == 403
def not_found?:: status == 404
def method_not_allowed?:: status == 405
def precondition_failed?:: status == 412
def unprocessable?:: status == 422
def redirect?:: [301, 302, 303, 307, 308].include? status
# https://github.com/rails/rails/blob/24c1429a/actionpack/lib/action_dispatch/http/response.rb#L219-L221
def sending?:: synchronize { @sending }
def committed?:: synchronize { @committed }
def sent?:: synchronize { @sent }
end
module OneLinerExamples
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/request.rb#L150-L155
def body: get_header(RACK_INPUT)
def script_name: get_header(SCRIPT_NAME).to_s
def script_name=(s): set_header(SCRIPT_NAME, s.to_s)
def path_info: get_header(PATH_INFO).to_s
def path_info=(s): set_header(PATH_INFO, s.to_s)
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L126-L129
def has_header?(key): headers.key? key
def get_header(key): headers[key]
def set_header(key, v): headers[key]: v
def delete_header(key): headers.delete key
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L135-L156
def invalid?: status < 100 || status >= 600
def informational?: status >= 100 && status < 200
def successful?: status >= 200 && status < 300
def redirection?: status >= 300 && status < 400
def client_error?: status >= 400 && status < 500
def server_error?: status >= 500 && status < 600
def ok?: status == 200
def created?: status == 201
def accepted?: status == 202
def no_content?: status == 204
def moved_permanently?: status == 301
def bad_request?: status == 400
def unauthorized?: status == 401
def forbidden?: status == 403
def not_found?: status == 404
def method_not_allowed?: status == 405
def precondition_failed?: status == 412
def unprocessable?: status == 422
def redirect?: [301, 302, 303, 307, 308].include? status
# https://github.com/rails/rails/blob/24c1429a/actionpack/lib/action_dispatch/http/response.rb#L219-L221
def sending?: synchronize { @sending }
def committed?: synchronize { @committed }
def sent?: synchronize { @sent }
end
module OneLinerExamples
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/request.rb#L150-L155
def body = get_header(RACK_INPUT)
def script_name = get_header(SCRIPT_NAME).to_s
def script_name=(s) = set_header(SCRIPT_NAME, s.to_s)
def path_info = get_header(PATH_INFO).to_s
def path_info=(s) = set_header(PATH_INFO, s.to_s)
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L126-L129
def has_header?(key) = headers.key? key
def get_header(key) = headers[key]
def set_header(key, v) = headers[key] = v
def delete_header(key) = headers.delete key
# https://github.com/rack/rack/blob/f9ad97fd/lib/rack/response.rb#L135-L156
def invalid? = status < 100 || status >= 600
def informational? = status >= 100 && status < 200
def successful? = status >= 200 && status < 300
def redirection? = status >= 300 && status < 400
def client_error? = status >= 400 && status < 500
def server_error? = status >= 500 && status < 600
def ok? = status == 200
def created? = status == 201
def accepted? = status == 202
def no_content? = status == 204
def moved_permanently? = status == 301
def bad_request? = status == 400
def unauthorized? = status == 401
def forbidden? = status == 403
def not_found? = status == 404
def method_not_allowed? = status == 405
def precondition_failed? = status == 412
def unprocessable? = status == 422
def redirect? = [301, 302, 303, 307, 308].include? status
# https://github.com/rails/rails/blob/24c1429a/actionpack/lib/action_dispatch/http/response.rb#L219-L221
def sending? = synchronize { @sending }
def committed? = synchronize { @committed }
def sent? = synchronize { @sent }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment