Last active
January 17, 2019 14:51
-
-
Save serradura/9ce559fe80db1bd199a8a237fea0a469 to your computer and use it in GitHub Desktop.
Examples of how Ruby 2.6 is more functional than ever! πππ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
raise 'Wrong Ruby version!' unless RUBY_VERSION >= '2.6.0' | |
module Strings | |
Trim = -> str { String(str).strip } | |
Replace = -> (sub, new_sub, str) { String(str).gsub(sub, new_sub) } | |
LowerCase = -> str { String(str).downcase } | |
end | |
# -- Alternative syntax -- | |
Slugify = # Slugify = | |
Strings::Trim # Strings::Trim \ | |
.>> Strings::LowerCase # >> Strings::LowerCase \ | |
.>> Strings::Replace.curry[' ', '-'] # >> Strings::Replace.curry[' ', '-'] | |
# -- Alternative syntax -- | |
slug = Slugify.(' I WILL be a url slug ') # Slugify.call(' I WILL be a url slug ') | |
# Slugify[' I WILL be a url slug '] | |
p slug # => "i-will-be-a-url-slug" | |
def slugify(value) | |
String(value) | |
.then(&Strings::Trim) | |
.then(&Strings::LowerCase) | |
.then(&Strings::Replace.curry[' ', '-']) | |
end | |
p slugify(' I will be a url SLUG ') # => "i-will-be-a-url-slug" | |
##################### | |
# Fuctional Objects # | |
##################### | |
# | |
# -- Static/Singleton methods -- | |
# | |
module Slugify2 | |
extend self | |
def call(value) | |
String(value) | |
.then(&method(:trim)) | |
.then(&method(:lower_case)) | |
.then(&method(:replace_spaces_by_dashes)) | |
end | |
private | |
def trim(str) | |
str.strip | |
end | |
def lower_case(str) | |
str.downcase | |
end | |
def replace_spaces_by_dashes(str) | |
str.gsub(' ', '-') | |
end | |
end | |
# -- Alternative syntax -- | |
p Slugify2.(' I will BE a url slug ') # Slugify2.call(' I will BE a url slug ') | |
# | |
# -- Instances -- | |
# | |
class Slugifier | |
def initialize(sub, new_sub) | |
@sub = sub | |
@new_sub = new_sub | |
end | |
def call(value) | |
String(value) | |
.then(&method(:trim)) | |
.then(&method(:lower_case)) | |
.then(&method(:replace)) | |
end | |
private | |
def trim(str) | |
str.strip | |
end | |
def lower_case(str) | |
str.downcase | |
end | |
def replace(str) | |
str.gsub(@sub, @new_sub) | |
end | |
end | |
Slugify3 = Slugifier.new(' ', '-') | |
# -- Alternative syntax -- | |
p Slugify3.(' I will be a URL slug ') # Slugify3.call(' I will be a URL slug ') | |
# | |
# -- Instances and Lambdas -- | |
# | |
class Slugifier2 | |
Trim = -> str { String(str).strip } | |
Replace = -> (sub, new_sub, str) { String(str).gsub(sub, new_sub) } | |
LowerCase = -> str { String(str).downcase } | |
def initialize(sub, new_sub) | |
@sub = sub | |
@new_sub = new_sub | |
end | |
def call(value) | |
String(value) | |
.then(&Trim) | |
.then(&LowerCase) | |
.then(&Replace.curry[' ', '-']) | |
end | |
end | |
Slugify4 = Slugifier2.new(' ', '-') | |
# -- Alternative syntax -- | |
p Slugify4.(' I will Be a urL slug ') # Slugify4.call(' I will Be a urL slug ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment