Skip to content

Instantly share code, notes, and snippets.

@jinqian
Last active December 11, 2015 08:49
Show Gist options
  • Save jinqian/4575830 to your computer and use it in GitHub Desktop.
Save jinqian/4575830 to your computer and use it in GitHub Desktop.
record of interesting ruby one-liners while I study Ruby on the way
# source code from internet & Why's (Poignant) Guide to Ruby
# fibonacci by ruby one liners
fibonacci = Hash.new { |h, k| x < 2 ? k : h[k] = h[k-1] + h[k-2] }
# print something several times
5.times { print "Odelay!" }
# unless
exit unless "restaurant".include? "aura"
# small trick, do it only when a is true and b is not true
print "We're using plastic 'cause we don't have glass." if plastic_cup unless glass_cup
# print array
['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
# one line to determine if a number is a prime
# http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
def is_prime(n)
("1" * n) !~ /^1?$|^(11+?)\1+$/
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment