Skip to content

Instantly share code, notes, and snippets.

@stereosupersonic
Last active February 18, 2016 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stereosupersonic/5d5fef557fd4000a3194 to your computer and use it in GitHub Desktop.
Save stereosupersonic/5d5fef557fd4000a3194 to your computer and use it in GitHub Desktop.
code_warrior_code
# key word arguemtents with options
def foo(a, **b)
[a,b]
end
foo(1, a: 2, b:3) #=> [1, {:a=>2, :b=>3}]
# === regex
# http://www.codewars.com/kata/did-she-say-hallo/ruby
# my solution
def validate_hello(greetings)
!!(greetings =~ /hello|ciao|salut|hallo|czesc|ahoj|Hola/i)
end
# best
def validate_hello(greetings)
/hello|ciao|salut|hallo|hola|ahoj|czesc/i === greetings
end
# === http://ruby-doc.org/core-2.3.0/Regexp.html#method-i-3D-3D-3D
#/^[a-z]*$/ === "HELLO" #=> false
#/^[A-Z]*$/ === "HELLO" #=> true
# http://www.codewars.com/kata/grouped-by-commas/ruby
# 1 -> "1"
# 10 -> "10"
# 100 -> "100"
#1000 -> "1,000"
#10000 -> "10,000"
#100000 -> "100,000"
#1000000 -> "1,000,000"
#35235235 -> "35,235,235"
def solution(n)
n.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
end
# http://www.codewars.com/kata/adding-ordinal-indicator-suffixes-to-numbers/solutions/ruby
def number_to_ordinal(number)
return '0' if number == 0
rules = { 1 => 'st', 2 => 'nd', 3 => 'rd'}
rules.default = 'th'
number.to_s + ((number % 100).between?(11,13) ? 'th' : rules[number % 10])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment