Upload
simon-courtois
View
221
Download
1
Embed Size (px)
Citation preview
bit.ly/tinci-ruby-23 inci@tincihq
article.try!(:has_tag?, hottest_tag()) # hottest_tag is always called
article&.has_tag?(hottest_tag()) # hottest_tag called only if needed
bit.ly/tinci-ruby-23 inci@tincihq
class Person def say_hello message = <<END Hello,
Delighted to meet you! END puts message end end
bit.ly/tinci-ruby-23 inci@tincihq
class Person def say_hello message = <<-END Hello,
Delighted to meet you! END puts message end end
bit.ly/tinci-ruby-23 inci@tincihq
class Person def say_hello_again message = <<-END Hello again,
Delighted to meet you! END puts message.strip_heredoc end end
bit.ly/tinci-ruby-23 inci@tincihq
class Person def say_hello message = <<~END Hello,
Delighted to meet you! END puts message end end
bit.ly/tinci-ruby-23 inci@tincihq
"hello world".uppcase # undefined method `uppcase' for… # Did you mean? upcase # upcase!
bit.ly/tinci-ruby-23 inci@tincihq
arr[1][3][0] # undefined method `[]' # for nil
arr = [1, [2, 3, [4]]]
bit.ly/tinci-ruby-23 inci@tincihq
arr[1] && arr[1][3] && arr[1][3][0] # => nil
arr = [1, [2, 3, [4]]]
bit.ly/tinci-ruby-23 inci@tincihq
arr.dig(1, 2, 0) # => 4 arr.dig(1, 3, 0) # => nil
arr = [1, [2, 3, [4]]]
bit.ly/tinci-ruby-23 inci@tincihq
arr.dig(1, 1, 0) # Fixnum does not # have #dig method
arr = [1, [2, 3, [4]]]
bit.ly/tinci-ruby-23 inci@tincihq
hash = {article: {author: {name: "Bob"}}}
hash.dig(:article, :author, :name) # => "Bob"
hash.dig(:article, :writer, :name) # => nil
bit.ly/tinci-ruby-23 inci@tincihq
hash.dig(:article, :tags, 1) # => "cool"
hash = {article: {tags: ["ruby", "cool"]}}
bit.ly/tinci-ruby-23 inci@tincihq
hash = { coffee: "black", milk: "white", tomato: "red" }
hash.fetch_values(:coffee, :tomato) # => ["black", "red"]
bit.ly/tinci-ruby-23 inci@tincihq
hash.fetch_values(:coffee, :banana) # key not found: :banana
hash = { coffee: "black", milk: "white", tomato: "red" }
bit.ly/tinci-ruby-23 inci@tincihq
hash.fetch_values(:milk, :banana) { |key| "unknown #{key}" } # => ["white", "unknown banana"]
hash = { coffee: "black", milk: "white", tomato: "red" }
bit.ly/tinci-ruby-23 inci@tincihq
1.positive? # => true -1.positive? # => false
1.negative? # => false -1.negative? # => true
bit.ly/tinci-ruby-23 inci@tincihq
[1, 2, 3].all?(&:positive?) # => true
[1, -2, 3].all?(&:positive?) # => false