Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/epitools/core_ext/misc.rb,
lib/epitools/core_ext/object.rb,
lib/epitools/core_ext/truthiness.rb
Class Method Summary collapse
-
.attrs(*names) ⇒ Object
Creates attr_accessors and an initialize method that accepts the attrs as arguments.
-
.memoize(*methods) ⇒ Object
Cache (memoize) the result of an instance method the first time it’s called, storing this value in the “@methodname” instance variable, and always return this value on subsequent calls.
-
.using(*args) ⇒ Object
Return a copy of the class with modules mixed into it.
Instance Method Summary collapse
-
#bench(*args, &block) ⇒ Object
Quick and easy benchmark.
-
#dmsg(msg) ⇒ Object
Emit a quick debug message (only if $DEBUG is true).
-
#float? ⇒ Boolean
Default “float?” behaviour.
-
#integer? ⇒ Boolean
Default “integer?” behaviour.
-
#locals ⇒ Object
Return a hash of local variables in the caller’s scope: :variable_name=>value.
-
#marshal ⇒ Object
(also: #to_marshal)
Serialize this object to a binary String, using Marshal.dump.
-
#not ⇒ Object
Negates a boolean, chained-method style.
-
#number? ⇒ Boolean
Default “number?” behaviour.
- #present? ⇒ Boolean
-
#time(message = nil) ⇒ Object
Time a block.
-
#to_json(pretty = true) ⇒ Object
Serialize this object to JSON (defaults to “pretty” indented JSON).
-
#to_yaml ⇒ Object
Serialize this object to YAML.
-
#truthy? ⇒ Boolean
‘truthy?` means `not blank?`.
-
#with(options = {}) ⇒ Object
Gives you a copy of the object with its attributes changed to whatever was passed in the options hash.
Class Method Details
.attrs(*names) ⇒ Object
Creates attr_accessors and an initialize method that accepts the attrs as arguments. (It’s kinda like an inline version of Struct.new(*args))
55 56 57 58 59 60 61 62 |
# File 'lib/epitools/core_ext/object.rb', line 55 def self.attrs(*names) attr_accessor *names define_method :initialize do |*vals| names.zip(vals) do |name, val| instance_variable_set("@#{name}", val) end end end |
.memoize(*methods) ⇒ Object
Cache (memoize) the result of an instance method the first time it’s called, storing this value in the “@methodname” instance variable, and always return this value on subsequent calls.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/epitools/core_ext/object.rb', line 82 def self.memoize(*methods) methods.each do |meth| old_method = instance_method(meth) if old_method.arity == 0 ivarname = "@#{meth}" define_method meth do instance_variable_get(ivarname) or instance_variable_set(ivarname, old_method.bind(self).call) end else raise "Can't memoize methods with arguments. (YET.)" end end end |
.using(*args) ⇒ Object
Return a copy of the class with modules mixed into it.
67 68 69 70 71 72 73 74 75 |
# File 'lib/epitools/core_ext/object.rb', line 67 def self.using(*args) if block_given? yield using(*args) else copy = self.dup args.each { |arg| copy.send(:include, arg) } copy end end |
Instance Method Details
#bench(*args, &block) ⇒ Object
Quick and easy benchmark.
Examples:
bench { something }
bench(90000000) { something }
bench :fast => proc { fast_method }, :slow => proc { slow_method }
In Ruby 1.9:
bench fast: ->{ fast_method }, slow: ->{ slow_method }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/epitools/core_ext/object.rb', line 158 def bench(*args, &block) # Shitty perl-esque hack to let the method take all the different kind of arguments. opts = Hash === args.last ? args.pop : {} n = args.first || 100 if opts.any? raise "Error: Supply either a do/end block, or procs as options. Not both." if block_given? raise "Error: Options must be procs." unless opts.all? { |k, v| v.is_a?(Proc) } benchblock = proc do |bm| opts.each do |name, blk| bm.report(name.to_s) { n.times &blk } end end elsif block_given? benchblock = proc do |bm| bm.report { n.times &block } end else raise "Error: Must supply some code to benchmark." end puts "* Benchmarking #{n} iterations..." Benchmark.bmbm(&benchblock) end |
#dmsg(msg) ⇒ Object
Emit a quick debug message (only if $DEBUG is true)
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/epitools/core_ext/object.rb', line 123 def dmsg(msg) if $DEBUG case msg when String puts msg else puts msg.inspect end end end |
#float? ⇒ Boolean
Default “float?” behaviour.
12 |
# File 'lib/epitools/core_ext/truthiness.rb', line 12 def float?; false; end |
#integer? ⇒ Boolean
Default “integer?” behaviour.
7 |
# File 'lib/epitools/core_ext/truthiness.rb', line 7 def integer?; false; end |
#locals ⇒ Object
Return a hash of local variables in the caller’s scope: :variable_name=>value
6 7 8 9 10 11 12 |
# File 'lib/epitools/core_ext/object.rb', line 6 def locals require 'binding_of_caller' caller = binding.of_caller(1) vars = caller.eval("local_variables").reject{|e| e[/^_/]} vals = caller.eval("[ #{vars.join(",")} ]") Hash[ vars.zip(vals) ] end |
#marshal ⇒ Object Also known as: to_marshal
Serialize this object to a binary String, using Marshal.dump.
101 102 103 |
# File 'lib/epitools/core_ext/object.rb', line 101 def marshal Marshal.dump self end |
#not ⇒ Object
Negates a boolean, chained-method style.
Example:
>> 10.even?
=> true
>> 10.not.even?
=> false
122 123 124 |
# File 'lib/epitools/core_ext/misc.rb', line 122 def not NotWrapper.new(self) end |
#number? ⇒ Boolean
Default “number?” behaviour.
17 |
# File 'lib/epitools/core_ext/truthiness.rb', line 17 def number?; false; end |
#present? ⇒ Boolean
189 190 191 |
# File 'lib/epitools/core_ext/object.rb', line 189 def present? true end |
#time(message = nil) ⇒ Object
Time a block.
137 138 139 140 141 142 143 144 145 |
# File 'lib/epitools/core_ext/object.rb', line 137 def time(=nil) start = Time.now result = yield elapsed = Time.now - start print "[#{}] " if puts "elapsed time: %0.5fs" % elapsed result end |
#to_json(pretty = true) ⇒ Object
Serialize this object to JSON (defaults to “pretty” indented JSON).
116 117 118 |
# File 'lib/epitools/core_ext/object.rb', line 116 def to_json(pretty=true) pretty ? JSON::pretty_generate(self) : JSON.dump(self) end |
#to_yaml ⇒ Object
Serialize this object to YAML.
109 110 111 |
# File 'lib/epitools/core_ext/object.rb', line 109 def to_yaml YAML::dump(self) end |
#truthy? ⇒ Boolean
‘truthy?` means `not blank?`
22 23 24 25 26 27 28 |
# File 'lib/epitools/core_ext/truthiness.rb', line 22 def truthy? if respond_to? :blank? not blank? else not nil? end end |
#with(options = {}) ⇒ Object
Gives you a copy of the object with its attributes changed to whatever was passed in the options hash.
Example:
>> cookie = Cookie.new(:size=>10, :chips=>200)
=> #<Cookie:0xffffffe @size=10, @chips=200>
>> cookie.with(:chips=>50)
=> #<Cookie:0xfffffff @size=10, @chips=50>
(All this method does is dup the object, then call “key=(value)” for each key/value in the options hash.)
BONUS FEATURE! ==
If you supply a block, it just gives you the object, and returns whatever your block returns.
Example:
>> {a: 10, b: 2}.with { |hash| hash[:a] / hash[:b] }
=> 5
Good for chaining lots of operations together in a REPL.
39 40 41 42 43 44 45 46 47 |
# File 'lib/epitools/core_ext/object.rb', line 39 def with(={}) if block_given? yield self else obj = dup .each { |key, value| obj.send "#{key}=", value } obj end end |