Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Rob

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Detect if your running on windows

It's an unfortunate necessity that you need to write different code for windows sometimes or at the very least load different libraries since there is no fork(), etc. This is how you can tell.

# Returns true if we are running on a MS windows platform, false otherwise.

def Kernel.is_windows?
  processor, platform, *rest = RUBY_PLATFORM.split("-")
  platform == 'mswin32'
end

Random Alphanumeric String Generator

This allows you to do String.random_alphanumeric(num) to get a random string that is num characters long. Useful for generating random identifier keys.

# Returns a random alphanumeric string of arbitrary size.

def String.random_alphanumeric(size=16)
  s = ""
  size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
  s
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS