Skip to content

Instantly share code, notes, and snippets.

View Kameshwaran's full-sized avatar

Kameshwaran Sachithanantham Kameshwaran

View GitHub Profile
@Kameshwaran
Kameshwaran / camelize
Last active August 29, 2015 14:14
Camelize in Ruby
# simple camelize in ruby
class String
def camelize
split("_").map(&:capitalize).join
end
end
# "kamesh_test" => "KameshTest"
@Kameshwaran
Kameshwaran / random.rb
Last active August 29, 2015 14:13
simple random implementation in ActiveRecord instances
class ActiveRecord::Base
class << self
def random
all.at(rand(count))
end
end
end
# User.random => #<User id: 10, ...>
# User.random => #<User id: 59, ...>
@Kameshwaran
Kameshwaran / hash_search.rb
Last active August 29, 2015 14:12
Simple search on Hash
def search(hash, keyword)
if (hash.respond_to?(:keys) && hash.keys.include?(keyword))
hash[keyword]
elsif hash.respond_to?(:find)
result = nil
hash.values.find { |value| result = search(value, keyword) }
result
end
end