Rules / Types, classes & roles / basic

RatStr Reference

Dual value rational number and string

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method new #

method new(Rat $i, Str $s)

The constructor requires both the Rat and the Str value, when constructing one directly the values can be whatever is required:

method Capture #

method Capture(RatStr:D: --> Capture:D)

Returns Capture:D.

Equivalent to Mu.Capture.

method Numeric #

multi method Numeric(RatStr:D: --> Rat:D)
multi method Numeric(RatStr:U: --> Rat:D)

Returns Rat:D.

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value 0.0.

method Rat #

method Rat

Returns the Rat value of the RatStr.

method Real #

multi method Real(Real:D: --> Rat:D)
multi method Real(Real:U: --> Rat:D)

Returns Rat:D.

The :D variant returns the numeric portion of the invocant. The :U variant issues a warning about using an uninitialized value in numeric context and then returns value 0.0.

infix C«===» #

multi infix:<===>(RatStr:D $a, RatStr:D $b)

RatStr Value identity operator. Returns True if the Rat values of $a and $b are identical and their Str values are also identical. Returns False otherwise.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

1 doc-drift · 1 no-output · 1 ok

class RatStr is Allomorph is Rat {}

Not executed: the documentation states no expected output for this example.

my $rat-str = <42.1>;
say $rat-str.^name;       # OUTPUT: «RatStr␤»

my Rat $rat = $rat-str;   # OK!
my Str $str = $rat-str;   # OK!

# ∈ operator cares about object identity
say 42.1 ∈ <42.1  55  1>; # OUTPUT: «False␤»
Output
RatStr
False

Documentation, Rakudo and Raku++ all agree.

my $f = RatStr.new(42.1, "forty two and a bit");
say +$f; # OUTPUT: «42.1␤»
say ~$f; # OUTPUT: «"forty two and a bit"␤»
Documentation
42.1
"forty two and a bit"
Rakudo
42.1
forty two and a bit
Raku++
42.1
forty two and a bit

Both engines agree; the documentation states something else. Trust the engines.