Class: Object
- Inherits:
- BasicObject
- Includes:
- RMTools
- Defined in:
- lib/rmtools/core/b.rb,
lib/rmtools.rb,
lib/rmtools/core/object.rb,
lib/rmtools/dev/present.rb,
lib/rmtools/core/aliases.rb,
lib/rmtools/conversions/json.rb,
lib/rmtools/core/deprecation.rb,
lib/rmtools/functional/unfold.rb
Overview
-> self or false with python logic Since 2.3.8 ActiveSupport has similar method: #presence Though, #b is obviously more usable due to it’s length, and still intuitive: “Boolean”, “Being”, “to Be” Differs from #presence in that String#b do not use #strip for check
Constant Summary
Constants included from RMTools
RMTools::ANSI2UTF, RMTools::ASCII_readable, RMTools::Alphanum, RMTools::Chars, RMTools::ENCODINGS_PATTERNS, RMTools::ICONVS, RMTools::IgnoreFiles, RMTools::Numbers, RMTools::RuAlphanum, RMTools::RuChars, RMTools::UTF2ANSI, RMTools::VERSION
Instance Method Summary collapse
- #b ⇒ Object
-
#cached(method, *args) ⇒ Object
def hard_method(args) …
- #cached?(method, *args) ⇒ Boolean
- #clear_cache(method, *args) ⇒ Object
- #deep_clone ⇒ Object
-
#define!(new_obj) ⇒ Object
Assign new value by variable or constant address.
-
#deprecate_method(message = "", *argv) ⇒ Object
class Klass def old_method *argv deprecate_method :new_method, *argv end end Klass.new.old_method gives: DEPRECATION from /path/to/libs/lib.rb: Klass#old_method is deprecated.
-
#ifndef(ivar = caller(1)[0].parse(:caller).func) ⇒ Object
def result_of_hard_calculation ifndef hard_calculation … end == def result_of_hard_calculation if defined? @result_of_hard_calculation return @result_of_hard_calculation else …
- #in(*container) ⇒ Object
- #inspect_instance_variables ⇒ Object
- #is(klass) ⇒ Object
- #kinda(klass) ⇒ Object
- #load_from(obj) ⇒ Object
- #my_methods(filter = //) ⇒ Object
- #my_methods_with(params) ⇒ Object
- #personal_methods(filter = //) ⇒ Object
- #personal_methods_with(params) ⇒ Object
- #present ⇒ Object
- #readable_variables ⇒ Object
-
#to_json_safe(options = nil, timeout: 10) ⇒ Object
handy when data may appear non-encodable (active_record objects, recursive structures, etc).
-
#unfold(breaker = lambda{|x|x.b}, &splitter) ⇒ Object
@ breaker must be callable that returns true value if that’s it @ &splitter must return a pair.
Methods included from RMTools
#dump_recurse, format_trace, format_trace_to_html, highlighted_line, prepare_write, putdate, puttime, randarr, randstr, read, read_lines, require, rw, tail, tail_n, threadify, tick!, tick_while, timer, write
Instance Method Details
#b ⇒ Object
8 |
# File 'lib/rmtools/core/b.rb', line 8 def b; self end |
#cached(method, *args) ⇒ Object
def hard_method(args)
... hard_calculation ...
end
object.cached(:hard_method, *args1) > … hard calculation …
> result with args1
object.cached(:hard_method, *args1)
> instant result with args1
object.cached?(:hard_method, *args2)
> false
object.cached(:hard_method, *args2) > … hard calculation …
> result with args2
object.cached?(:hard_method, *args2)
> true
object.clear_cache(:hard_method, *args1)
> result with args1
object.cached?(:hard_method, *args1)
> false
object.cached(:hard_method, *args1) > … hard calculation …
> result with args1
object.cached(:hard_method, *args2)
> instant result with args2
object.clear_cache(:hard_method)
> => result with args1, args2 => result with args2
- object.cached?(:hard_method, *args1), object.cached?(:hard_method, *args2)
-
> [false, false]
104 105 106 |
# File 'lib/rmtools/core/object.rb', line 104 def cached(method, *args) ((@__method_cache__ ||= {})[method.to_sym] ||= {})[args] ||= __send__ method, *args end |
#cached?(method, *args) ⇒ Boolean
118 119 120 |
# File 'lib/rmtools/core/object.rb', line 118 def cached?(method, *args) ((@__method_cache__ ||= {})[method.to_sym] ||= {}).include? args end |
#clear_cache(method, *args) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/rmtools/core/object.rb', line 108 def clear_cache(method, *args) if @__method_cache__ if args.empty? @__method_cache__.delete method.to_sym elsif method_cache = @__method_cache__[method.to_sym] method_cache.delete args end end end |
#deep_clone ⇒ Object
125 126 127 |
# File 'lib/rmtools/core/object.rb', line 125 def deep_clone _deep_clone({}) end |
#define!(new_obj) ⇒ Object
Assign new value by variable or constant address. Can’t process numeric, nil, false and true. Though, it can process containers with all of that shit.
a = 'Object.new'
=> "Object.new"
def inspire x
x.define! eval x
end
=> nil
inspire a
=> #<Object:0xb790bed0>
a
=> #<Object:0xb790bed0>
It’s quite buggy, you can get sudden segfault sometime after using method ^_^‘\ Maybe it could mark used objects for GC not to collect
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'ext/rmtools.cpp', line 20
static VALUE object_define_new_value(VALUE self, VALUE new_obj)
{
if (FIXNUM_P(self) || self == Qnil || self == Qfalse || self == Qtrue || self == Qundef) {
VALUE tmp = rb_mod_name(rb_obj_class(self));
const char* msg = StringValuePtr(tmp);
rb_raise(rb_eTypeError, "can't redefine %s", msg);
}
if (FIXNUM_P(new_obj) || new_obj == Qnil || new_obj == Qfalse || new_obj == Qtrue || new_obj == Qundef) {
VALUE tmp = rb_mod_name(rb_obj_class(self));
const char* msg = StringValuePtr(tmp);
rb_raise(rb_eTypeError, "can't define object as %s", msg);
}
// Place the definition of the new object in the slot of self
memcpy(reinterpret_cast<void*>(self), reinterpret_cast<void*>(rb_funcall(new_obj, rb_intern("clone"), 0)), SLOT_SIZE);
return self;
}
|
#deprecate_method(message = "", *argv) ⇒ Object
class Klass
def old_method *argv
deprecate_method :new_method, *argv
end
end Klass.new.old_method
gives:
DEPRECATION from /path/to/libs/lib.rb: Klass#old_method is deprecated. Use #new_method instead.
> <new_method(*argv) result>
Same shit: class Klass
deprecate_method :old_method, :new_method
end
17 18 19 20 21 22 23 24 25 |
# File 'lib/rmtools/core/deprecation.rb', line 17 def deprecate_method ="", *argv caler = caller[0].parse(:caller) sym = nil if .is Symbol sym, = , "Use ##{} instead." end STDERR.puts "DEPRECATION from #{caler.path}: #{self.class}##{caler.func} is deprecated. #{.capitalize}" if caler __send__ sym, *argv if sym end |
#ifndef(ivar = caller(1)[0].parse(:caller).func) ⇒ Object
def result_of_hard_calculation
ifndef {... hard_calculation ...}
end
def result_of_hard_calculation
if defined? @result_of_hard_calculation
return @result_of_hard_calculation
else
... hard_calculation
res = ...
@result_of_hard_calculation = res
end
end
68 69 70 71 72 |
# File 'lib/rmtools/core/object.rb', line 68 def ifndef(ivar=caller(1)[0].parse(:caller).func) ivar = :"@#{ivar}" return instance_variable_get ivar if instance_variable_defined? ivar instance_variable_set ivar, yield end |
#in(*container) ⇒ Object
47 48 49 |
# File 'lib/rmtools/core/object.rb', line 47 def in(*container) container.size == 1 ? container[0].include?(self) : container.include?(self) end |
#inspect_instance_variables ⇒ Object
51 52 53 |
# File 'lib/rmtools/core/object.rb', line 51 def inspect_instance_variables instance_eval {binding().inspect_instance_variables} end |
#is(klass) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/rmtools/core/object.rb', line 4 def is klass if Array === klass instance_of?(Array) && self[0].instance_of?(klass[0]) else instance_of? klass end end |
#kinda(klass) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rmtools/core/object.rb', line 11 def kinda klass if Array === klass kind_of?(Array) && self[0].kind_of?(klass[0]) else kind_of? klass end end |
#load_from(obj) ⇒ Object
42 43 44 45 |
# File 'lib/rmtools/core/object.rb', line 42 def load_from(obj) readable_variables.each {|v| instance_variable_set("@#{v}", obj.instance_variable_get("@#{v}"))} self end |
#my_methods(filter = //) ⇒ Object
18 19 20 |
# File 'lib/rmtools/core/object.rb', line 18 def my_methods filter=// (self.public_methods - Object.public_instance_methods).sort!.grep(filter) end |
#my_methods_with(params) ⇒ Object
26 27 28 29 30 |
# File 'lib/rmtools/core/object.rb', line 26 def my_methods_with params m = my_methods params.each {|p,val| m.reject! {|_| ''.method(_).send(p) != val}} m end |
#personal_methods(filter = //) ⇒ Object
22 23 24 |
# File 'lib/rmtools/core/object.rb', line 22 def personal_methods filter=// (self.public_methods - self.class.superclass.public_instance_methods).sort!.grep(filter) end |
#personal_methods_with(params) ⇒ Object
32 33 34 35 36 |
# File 'lib/rmtools/core/object.rb', line 32 def personal_methods_with params m = personal_methods params.each {|p,val| m.reject! {|_| ''.method(_).send(p) != val}} m end |
#present ⇒ Object
4 5 6 7 |
# File 'lib/rmtools/dev/present.rb', line 4 def present [true, false, nil, Numeric, String, Regexp].each {|klass| return puts inspect if klass === self} Hash[readable_variables.map {|v| [":#{v}", __send__(v)]}].present end |
#readable_variables ⇒ Object
38 39 40 |
# File 'lib/rmtools/core/object.rb', line 38 def readable_variables public_methods.to_ss & instance_variables.map {|v|v[1..-1]} end |
#to_json_safe(options = nil, timeout: 10) ⇒ Object
handy when data may appear non-encodable (active_record objects, recursive structures, etc)
87 88 89 |
# File 'lib/rmtools/conversions/json.rb', line 87 def to_json_safe(=nil, timeout: 10) timeout(timeout) {to_json()} end |
#unfold(breaker = lambda{|x|x.b}, &splitter) ⇒ Object
@ breaker must be callable that returns true value if that’s it @ &splitter must return a pair
6 7 8 9 10 11 12 13 |
# File 'lib/rmtools/functional/unfold.rb', line 6 def unfold(breaker=lambda{|x|x.b}, &splitter) obj, container = self, [] until breaker.call obj obj, next_element = splitter[obj] container.unshift next_element end container end |