writing destructive string methods in Ruby
Example:
def decapitalize! replace (self.reverse.chop + self.reverse.last.downcase).reverse end def decapitalize dup.decapitalize! end
TextSnippets > deckard > ruby
2824 users tagging and storing useful source code snippets
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 (or not, you can keep them private!)
def decapitalize! replace (self.reverse.chop + self.reverse.last.downcase).reverse end def decapitalize dup.decapitalize! end
require 'enumerator'
class Array def process(method_obj, batch_size = 10, fin = []) if (this_batch = self.first(batch_size)).size > 0 fin.concat method_obj.call(this_batch) (self - this_batch).process(method_obj, batch_size, fin) else return fin end end def proc_array(method_obj, batch_size = 2) result = [] self.each_slice(batch_size) do |batch| result.concat method_obj.call(batch) end result end end def munge(ary) ary.collect { |e| e.+ 10 } end my_meth = method(:munge) BIGARY = Array.new(100) {|i| i} require 'benchmark' include Benchmark bmbm(2) do |test| test.report("recursive") do 10000.times {BIGARY.process(my_meth, 2)} end test.report("block") do 10000.times {BIGARY.proc_array(my_meth)} end end
load 'filename.rb'
ruby my_testing_file.rb --name test_casename
require 'test/unit' include Test::Unit::Assertions
self.<attribute> = val
<attribute> = val
class Billy include Bully end
class Billy extend Bully end Billy.fight(others)
a = Billy.new() a.extend Bully a.fight(weaklings)
def child_method(arg1, arg2) super(arg1, arg2) end
chars = ("a".."z").to_a + ("1".."9").to_a newpass = Array.new(8, '').collect{chars[rand(chars.size)]}.join
module ActiveRecord class Base class << self alias old_establish_connection establish_connection end def self.establish_connection(arg) logger.debug("Establishing connection") self.old_establish_connection(arg) end end end
module ActiveRecord class Base def self.establish_connection(arg) logger.debug("Establishing connection") super arg end end end
debug(@instance_var.pretty_inspect)
require 'cgi' CGI.escape([your junk])