This mrbgem extends the Kernel module in mruby with additional useful methods.
Raises a RuntimeError. This is an alias for raise.
Example:
fail "Something went wrong"
# Raises RuntimeError: Something went wrongReturns the current execution stack (backtrace).
- If
startis provided, it indicates the number of frames to skip. - If
lengthis provided, it limits the number of frames returned. - If a
rangeis 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
fooReturns 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_methodReturns 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_methodConverts arg to an Integer.
- Numeric types are converted directly (floating-point numbers are truncated).
- If
argis aString,base(0, or between 2 and 36) is used as the base for conversion.- If
baseis omitted or zero, radix indicators (0,0b,0x) in the string are honored.
- If
- Strings must strictly conform to numeric representation, unlike
String#to_i. - Passing
nilraises aTypeError.
Examples:
Integer(123.999) #=> 123
Integer("0x1a") #=> 26
Integer("0930", 10) #=> 930
Integer("111", 2) #=> 7
# Integer(nil) #=> TypeError
# Integer("invalid") #=> ArgumentErrorConverts arg to a Float.
- Numeric types are converted directly.
- Other types are converted using
arg.to_f. - Passing
nilraises aTypeError.
Examples:
Float(1) #=> 1.0
Float(123.456) #=> 123.456
Float("123.456") #=> 123.456
# Float(nil) #=> TypeError
# Float("invalid") #=> ArgumentErrorConverts arg to a String using its to_s method.
Examples:
String(self) #=> "main"
String(self.class) #=> "Object"
String(123456) #=> "123456"
String(:symbol) #=> "symbol"Converts arg to an Array.
- If
argresponds toto_a, it callsto_ato convert. - Otherwise, it returns a new array containing
argas 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_aConverts arg to a Hash.
- If
argis already aHash, it is returned. - If
argisnilor an emptyArray, an emptyHashis returned. - Otherwise, it raises a
TypeError.
Examples:
Hash({ key: :value }) #=> { key: :value }
Hash(nil) #=> {}
Hash([]) #=> {}
# Hash([1, 2, 3]) #=> TypeError