Skip to content

Instantly share code, notes, and snippets.

@dentarg
Created February 8, 2019 12:38
Show Gist options
  • Select an option

  • Save dentarg/af5b50f89aac14075b747137d6c138bd to your computer and use it in GitHub Desktop.

Select an option

Save dentarg/af5b50f89aac14075b747137d6c138bd to your computer and use it in GitHub Desktop.
RubyTapas: 542-abstraction-and-performance-part-2-with-chris-seaton.mp4, 541-abstraction-and-performance-part-1-with-chris-seaton.mp4
num = 50
min = 100
max = 10
def clamp_slow(num, min, max)
print "#{__method__}(num=#{num}, min=#{min}, max=#{max}) => "
print [num, min, max].sort[1]
end
def clamp_fast(num, min, max)
print "#{__method__}(num=#{num}, min=#{min}, max=#{max}) => "
result = num
result = max if num > max
result = min if num < min
print result
end
def clamp_opti(num, min, max)
print "#{__method__}(num=#{num}, min=#{min}, max=#{max}) => "
print num > max ? max : (num < min ? min : num)
end
clamp_slow num, min, max
puts
puts
clamp_fast num, min, max
puts
puts
clamp_opti num, min, max
puts
puts
num = 50
min = 100
max = 1000
clamp_slow num, min, max
puts
puts
clamp_fast num, min, max
puts
puts
clamp_opti num, min, max
puts
@dentarg
Copy link
Author

dentarg commented Feb 8, 2019

$ ruby clamp.rb
clamp_slow(num=50, min=100, max=10) => 50

clamp_fast(num=50, min=100, max=10) => 100

clamp_opti(num=50, min=100, max=10) => 10

clamp_slow(num=50, min=100, max=1000) => 100

clamp_fast(num=50, min=100, max=1000) => 100

clamp_opti(num=50, min=100, max=1000) => 100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment