lib/object.fy in fancy-0.3.0 vs lib/object.fy in fancy-0.3.1
- old
+ new
@@ -2,25 +2,36 @@
"""
Root class of Fancy's class hierarchy.
All classes inherit from Object.
"""
+ def ++ other {
+ """
+ @other Other object to concatenate its @String value with.
+ @return @String concatenation of @String values of @self and @other.
+
+ Returns the @String concatenation of @self and @other.
+ Calls to_s on @self and @other and concatenates the results to a new @String.
+ """
+ to_s + (other to_s)
+ }
+
def loop: block {
"Infinitely calls the block (loops)."
{ true } while_true: {
block call
}
}
def println {
"Same as Console println: self. Prints the object on STDOUT, followed by a newline."
- Console println: $ self to_s
+ Console println: to_s
}
def print {
"Same as Console print: self. Prints the object on STDOUT."
- Console print: $ self to_s
+ Console print: to_s
}
def != other {
"Indicates, if two objects are unequal."
self == other not
@@ -52,32 +63,32 @@
}
def if_do: block {
"If the object is non-nil, it calls the given block with itself as argument."
- match self -> {
+ match self {
case nil -> nil
case false -> nil
case _ -> block call: [self]
}
}
def if_do: then_block else: else_block {
"""If the object is non-nil, it calls the given then_block with itself as argument.
Otherwise it calls the given else_block."""
- match self -> {
+ match self {
case nil -> else_block call: [self]
case false -> else_block call: [self]
case _ -> then_block call: [self]
}
}
def or_take: other {
"Returns self if it's non-nil, otherwise returns the given object."
- if: (self nil?) then: {
+ if: nil? then: {
other
} else: {
self
}
}
@@ -93,12 +104,16 @@
def to_i {
0
}
def || other {
- "Same as Object#or:"
- or: other
+ "Returns @ self if self is true-ish, otherwise returns @other"
+ self if_do: {
+ return self
+ } else: {
+ return other
+ }
}
def && other {
"Same as Object#and:"
and: other
@@ -164,7 +179,44 @@
}
def identity {
"The identity method simply returns self."
self
+ }
+
+ def returning: value do: block {
+ """
+ @value Value that gets returned at the end.
+ @block A @Block@ that gets called with @value before returning @value.
+ @return @value
+
+ Returns @value after calling @block with it.
+ Useful for returning some object after using it, e.g.:
+
+ # this will return [1,2]
+ returning: [] do: |arr| {
+ arr << 1
+ arr << 2
+ }
+ """
+
+ val = value
+ block call: [val]
+ val
+ }
+
+ def ? future {
+ future value
+ }
+
+ def yield {
+ Fiber yield
+ }
+
+ def yield: values {
+ Fiber yield: values
+ }
+
+ def wait: seconds {
+ Fiber yield: [seconds]
}
}