RbRational

Swift
public struct RbRational: RbObjectConvertible

A simple interface to Ruby’s rational number support.

This is not a Swift rational number library. It could be used as an interface between one such and Ruby.

Ruby represents rational numbers internally as a positive or negative integer numerator and a positive integer denominator. Instances of this RbRational type converted from Ruby objects follow these rules; instances produced by Swift code using the RbRational.init(numerator:denominator:) method may not.

let myRat = RbRational(numerator: myFractionTop, denominator: myFractionBot)

let resultObj = myRubyService.call("addFinalSample", args: [myRat])

let myRatResult = RbRational(resultObj)

Topics

var numerator: Double

The rational number’s numerator.

Declaration
Swift
public let numerator: Double

var denominator: Double

The rational number’s denominator.

Declaration
Swift
public let denominator: Double

init(numerator: Double, denominator: Double)

Create a new rational number. The parameters are normalized to give a positive denominator.

Declaration
Swift
public init(numerator: Double, denominator: Double)

init?(RbObject)

Create a rational number from a Ruby object.

This calls #to_r before extracting the parts so can be passed various types of Ruby object.

Returns nil if the object cannot be converted or if its fractional parts parts cannot be converted to Swift Doubles. See RbError.history to see why a conversion failed.

Declaration
Swift
public init?(_ value: RbObject)

init?(any RbObjectConvertible)

Convert some Swift data type to a rational.

This is a convenience wrapper that lets you access Ruby’s rational library directly from Swift types, for example:

let rat = RbRational(0.3)
Declaration
Swift
public init?(_ value: any RbObjectConvertible)

var rubyObject: RbObject

Get a Ruby version of an RbRational.

This can theoretically produce RbObject.nilObject if the environment has been nobbled in some way.

Declaration
Swift
public var rubyObject: RbObject { get }