Skip to content

Instantly share code, notes, and snippets.

@teohm
Last active December 31, 2015 11:59
Show Gist options
  • Save teohm/7982971 to your computer and use it in GitHub Desktop.
Save teohm/7982971 to your computer and use it in GitHub Desktop.

rAndom ruBy tiPs >>--(o_0)-->

do..end vs bracket {}

Bracket syntax {} has higher precedence than do..end syntax [1]

[1] http://stackoverflow.com/questions/2122380/using-do-block-vs-brackets

# In this case
def outer(foo)
  puts "foo: #{foo.inspect}"
  yield if block_given?
end

# Use {} OK!
outer [1,2,3].map {|n| n.to_i+1}
# foo: [2, 3, 4]
# => nil
 
# Use do..end KO!
outer [1,2,3].map do |n|
  n.to_i+1
end
# foo: #<Enumerator: [1, 2, 3]:map>
# => 1

hash.merge(hash){|_,v,_| v*2}
String === "foo"  # true
"foo" === String  # false
->(x) { x == 2 } === 2  # true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment