Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb

Overview

lib/ruby/try.rb

Defined Under Namespace

Modules: InstanceExecHelper

List Constructors collapse

Combinators collapse

Instance Method Summary collapse

Instance Method Details

#bind {|_self| ... } ⇒ Object

Yields ‘self` to a block argument

Examples:

nil.bind{|a| a.nil? }   #=> true
100.bind{|a| a.nil? }   #=> false

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



406
407
408
# File 'lib/ruby/jruby_hack.rb', line 406

def bind
  yield self
end

#blank?Boolean

Always ‘false`. Note that NilClass#blank? is overridden to return `true`

Examples:

false.blank?    #=> false
100.blank?      #=> false

Returns:

  • (Boolean)


266
267
268
# File 'lib/ruby/jruby_hack.rb', line 266

def blank?
  false
end

#cons(array = []) ⇒ Array

Prepend the item to the front of a new list

Examples:

1.cons              #=> [1]
1.cons(2.cons)      #=> [1, 2]
1.cons([0, 0, 0])   #=> [1, 0, 0, 0]

Returns:



381
382
383
# File 'lib/ruby/jruby_hack.rb', line 381

def cons(array = [])
  [self] + array
end

#eigenclassClass

Return the “eigenclass” where singleton methods reside

Returns:

  • (Class)


427
428
429
# File 'lib/ruby/jruby_hack.rb', line 427

def eigenclass
  class << self; self; end
end

#instance_exec(*args, &block) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/ruby/jruby_hack.rb', line 293

def instance_exec(*args, &block)
  thread = Thread.current.object_id.abs
  object = object_id.abs
  mname  = "__instance_exec_#{thread}_#{object}"
  InstanceExecHelper.module_eval { define_method(mname, &block) }

  begin
    __send__(mname, *args)
  ensure
    begin
      InstanceExecHelper.module_eval { remove_method(mname) }
    rescue
    end
  end
end

#present?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/ruby/jruby_hack.rb', line 270

def present?
  true
end

#snoc(array = []) ⇒ Array

Append the item to rear of a new list

Examples:

1.snoc              #=> [1]
1.snoc(2.snoc)      #=> [2, 1]
1.snoc([0, 0, 0])   #=> [0, 0, 0, 1]

Returns:



393
394
395
# File 'lib/ruby/jruby_hack.rb', line 393

def snoc(array = [])
  array + [self]
end

#tap {|_self| ... } ⇒ Object

Yields ‘self` to a side-effect block argument and return `self`

@example:

100.tap{|a| puts "debug: #{a}" }   #=> 100

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on

Returns:

  • self



416
417
418
419
# File 'lib/ruby/jruby_hack.rb', line 416

def tap
  yield self
  self
end

#try(*args, &block) ⇒ Object

Sends the arguments to ‘self` or yields `self` (when `self` is non-`nil`). This is overridden by NilClass#try, which always returns `nil`.

Examples:

"non-nil".try(&:length)       #=> 7
nil.try(&:length)             #=> nil

"non-nil".try(:slice, 0, 3)   #=> "non"
nil.try(:slice, 0, 3)         #=> nil


657
658
659
660
661
662
663
# File 'lib/ruby/jruby_hack.rb', line 657

def try(*args, &block)
  if args.empty?
    yield self
  else
    __send__(*args, &block)
  end
end