Skip to content

Instantly share code, notes, and snippets.

@patcullen
Created September 2, 2015 06:13
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 patcullen/baef42a6ab78a36230b7 to your computer and use it in GitHub Desktop.
Save patcullen/baef42a6ab78a36230b7 to your computer and use it in GitHub Desktop.
def firstchar_map(input)
return input.split.map(&:chr)*''
end
puts firstchar("hello world")
puts firstchar("what you see is what you get")
# this is my very first ruby function, so here's where i learned from...
# - commenting: http://www.tutorialspoint.com/ruby/ruby_comments.htm
# - functions in ruby: http://www.howtogeek.com/howto/programming/ruby/ruby-function-method-syntax/
# - spliting strings: http://www.dotnetperls.com/split-ruby
# - mapping and &,. wow:http://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby
# - ruby string docs: http://ruby-doc.org/core-2.2.0/String.html
# here is a test to check performance agasint using regex
def firstchar_map(input)
return input.split.map(&:chr)*''
end
def firstchar_regex(input)
return input.scan(/\b(\w)/)*''
end
t1 = Time.now
(0..1e5).each do |i|
firstchar_map("There are some words here, and this number #{i} makes the string unique'ish")
end
puts "Using mapping: #{Time.now - t1}"
t1 = Time.now
(0..1e5).each do |i|
firstchar_regex("There are some words here, and this number #{i} makes the string unique'ish")
end
puts "Using regex: #{Time.now - t1}"
# >Using mapping: 0.842458
# >Using regex: 1.980652
# So I choose door number one. Splitting and mapping.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment