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
scores = [ | |
Score.new(6), | |
Score.new(17), | |
Score.new(14), | |
Score.new(13), | |
Score.new(11), | |
] | |
p scores.min | |
p scores.max |
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
class Score | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def grade | |
if @value < 10 | |
"failing" |
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
case "/home/denis" | |
when PathPattern.new("/home/*") | |
puts "Home sweet home" | |
else | |
puts "Somewhere, I guess" | |
end |
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
bin_ruby = Path.new("bin", "ruby") | |
var_log = Path.new("var", "log") | |
pattern = PathPattern.new("/bin/*") | |
pattern === bin_ruby # => true | |
pattern === var_log # => false |
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
pattern = PathPattern.new("/bin/*") | |
pattern === "/bin/ruby" # => true | |
pattern === "/var/log" # => false |
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
class PathPattern | |
def initialize(string) | |
@string = string | |
end | |
def ===(other) | |
File.fnmatch(@string, other) | |
end | |
end |
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
phone = "+491573abcde" | |
case phone | |
when /00000/ | |
puts "Too many zeroes!" | |
when /[a-z]/ | |
puts "Your phone number has letters in it?!" | |
end |
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
[4, 2.0, 7, 6.1].grep(Integer) # => [4, 7] | |
[4, 2.0, 7, 6.1].grep(2..6) # => [4, 2.0] | |
# Same, but more verbose: | |
[4, 2.0, 7, 6.1].select { |num| Integer === num } | |
[4, 2.0, 7, 6.1].select { |num| 2..6 === num } |
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
Integer === 15 # => true | |
Integer === 15.5 # => false |
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
if 10..15 === 14 | |
puts "Kinda small!" | |
elsif 80..99 === 14 | |
puts "Kinda large!" | |
end |