-
-
Save naveedkakal/63b93b1325e5db11569d1f578a5c27b7 to your computer and use it in GitHub Desktop.
2017-01-03 Lightning Talk
This file contains 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
# Credit: Ted Price, Lightning talk - 1-05-2017 | |
# `warn` | |
# Use to write to stderr | |
puts "Hello stdout!" | |
warn "Hello stderr!" | |
# ruby __FILE__ 1> stdout.txt 2> stderr.txt | |
#################################################################### | |
# `included_modules` | |
module A;end | |
module B;end | |
module C include A;end | |
class Foo; include B, C; end | |
Foo.ancestors | |
# => [Foo, B, C, A, Object, Kernel, BasicObject] | |
Foo.included_modules | |
#=> [B, C, A, Kernel] | |
#################################################################### | |
# ~ as a method calls intself | |
class Foo | |
def ~; puts "Happy New Year"; end | |
end | |
~Foo.new | |
# Happy New Year | |
# => nil | |
#################################################################### | |
# Hash#dig | |
hsh = { a: { b: { c: "2017" } } } | |
hsh[:a][:b][:c] | |
hsh.dig(:a, :b, :c) | |
(hsh[:a][:oops][:c] rescue 'Oops!') | |
hsh.dig(:a, :oops, :c) | |
#################################################################### | |
# ~HEREDOC | |
# Removes whitespace | |
<<~HEREDOC | |
This | |
whitespace | |
will | |
be | |
removed | |
HEREDOC | |
<<~HEREDOC | |
This | |
will | |
not | |
HEREDOC | |
#################################################################### | |
# "Lonely Operator" (Safe Navigation) | |
quux = nil | |
quux.try(:foo).try(:bar) | |
quux&.foo&.bar | |
#################################################################### | |
# Hash#to_proc | |
hsh = { a: 1, b: 2, c: 3 } | |
hsh.values_at(:a, :b, :c) | |
# => [1, 2, 3] | |
[:a, :b:, :c].map(&hsh) | |
# => [1, 2, 3] | |
#################################################################### | |
# Lambda#curry | |
add = -> (a, b) { a + b } | |
add[5,20] | |
# => 25 | |
add5 = add.curry[5] | |
add5[10] | |
# => 15 | |
#################################################################### | |
# Range#lazy | |
range = (0..Float::INFINITY).lazy | |
# => #<Enumerator::Lazy: 0..Infinity> | |
range.next | |
# => 0 | |
range.next | |
# => 1 | |
range.next | |
# => 2 | |
range = (0..10).lazy | |
# => #<Enumerator::Lazy: 0..10> | |
range.force | |
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
#################################################################### | |
# Object#enum_for | |
ary = (1..10).to_a.enum_for | |
# => #<Enumerator: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:each> | |
ary.next | |
# => 1 | |
ary.next | |
# => 2 | |
#################################################################### | |
# Kernel#freeze | |
Foo = Stuct.new(:name) | |
f = foo.new "Ted" | |
f.freeze | |
f.name = "Price" | |
# => RuntimeError: can't modify frozen Foo | |
dup_f = f.dup | |
dup_f.name = "Price" | |
# => "Price" | |
clone_f = f.clone | |
clone_f.name = "Price, Jr." | |
# => RuntimeError: can't modify frozen Foo | |
#################################################################### | |
# Method#owner | |
class A | |
def hello | |
puts "hello" | |
end | |
end | |
class B < A | |
B.new.method(:hello).owner | |
# => A | |
Object.method(:freeze).owner | |
# => Module | |
Object.new.method(:freeze).owner | |
# => Kernel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment