mrbgems/mruby-kernel-ext/README.md
This mrbgem extends the Kernel module in mruby with additional useful methods.
fail(*args)Raises a RuntimeError. This is an alias for raise.
Example:
fail "Something went wrong"
# Raises RuntimeError: Something went wrong
caller(start=1, length=nil) -> array | nilcaller(range) -> array | nilReturns the current execution stack (backtrace).
start is provided, it indicates the number of frames to skip.length is provided, it limits the number of frames returned.range is provided, it specifies the portion of the stack to return.Returns nil if start is greater than or equal to the number of frames in the stack.
Example:
def foo
bar
end
def bar
puts caller(0) # Show all frames starting from the current one
puts caller(1) # Skip one frame
end
foo
__method__ -> symbol | nilReturns the name of the current method as a Symbol. If called outside of a method, it returns nil.
Example:
class MyClass
def my_method
puts __method__
end
end
MyClass.new.my_method
# Output: :my_method
__callee__ -> symbol | nilReturns the called name of the current method as a Symbol. If called outside of a method, it returns nil. This can be different from __method__ when using aliases.
Example:
class MyClass
def original_method
puts __callee__
end
alias aliased_method original_method
end
obj = MyClass.new
obj.original_method # Output: :original_method
obj.aliased_method # Output: :aliased_method
Integer(arg, base=0) -> integerConverts arg to an Integer.
arg is a String, base (0, or between 2 and 36) is used as the base for conversion.
base is omitted or zero, radix indicators (0, 0b, 0x) in the string are honored.String#to_i.nil raises a TypeError.Examples:
Integer(123.999) #=> 123
Integer("0x1a") #=> 26
Integer("0930", 10) #=> 930
Integer("111", 2) #=> 7
# Integer(nil) #=> TypeError
# Integer("invalid") #=> ArgumentError
Float(arg) -> floatConverts arg to a Float.
arg.to_f.nil raises a TypeError.Examples:
Float(1) #=> 1.0
Float(123.456) #=> 123.456
Float("123.456") #=> 123.456
# Float(nil) #=> TypeError
# Float("invalid") #=> ArgumentError
String(arg) -> stringConverts arg to a String using its to_s method.
Examples:
String(self) #=> "main"
String(self.class) #=> "Object"
String(123456) #=> "123456"
String(:symbol) #=> "symbol"
Array(arg) -> arrayConverts arg to an Array.
arg responds to to_a, it calls to_a to convert.arg as its single element.Examples:
Array(1..5) #=> [1, 2, 3, 4, 5]
Array([1, 2, 3]) #=> [1, 2, 3]
Array("hello") #=> ["hello"] # If String does not have to_a
Array({ a: 1, b: 2 }) #=> [[:a, 1], [:b, 2]] # If Hash has to_a
Hash(arg) -> hashConverts arg to a Hash.
arg is already a Hash, it is returned.arg is nil or an empty Array, an empty Hash is returned.TypeError.Examples:
Hash({ key: :value }) #=> { key: :value }
Hash(nil) #=> {}
Hash([]) #=> {}
# Hash([1, 2, 3]) #=> TypeError