Skip to content

Instantly share code, notes, and snippets.

@grohiro
Last active May 30, 2019 03:19
Show Gist options
  • Select an option

  • Save grohiro/d85f3cd02f9cb7f3b634a44bd2c063fb to your computer and use it in GitHub Desktop.

Select an option

Save grohiro/d85f3cd02f9cb7f3b634a44bd2c063fb to your computer and use it in GitHub Desktop.
Hash argument of Ruby
require 'json'
def print_json(data)
puts JSON.dump(data)
end
print_json(1) #=> 1
print_json([1]) #=> [1]
print_json({a:1}) #=> {"a":1}
print_json({a:1, b: 2}) #=> {"a":1,"b":2}
print_json 1 #=> 1
print_json [1] #=> [1]
#print_json {a: 1} #=> syntax error
#print_json {a: 1, b: 2} #=> syntax error
print_json a: 1 #=> {"a":1}
print_json a: 1, b: 2 #=> {"a":1,"b":2}
# why?
print_json [1] #=> OK
print_json({a: 1}) #=> OK
#print_json {a: 1} #=> NG
print_json a: 1 #=> OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment