lib/object.fy in fancy-0.3.2 vs lib/object.fy in fancy-0.3.3

- old
+ new

@@ -15,13 +15,11 @@ to_s + (other to_s) } def loop: block { "Infinitely calls the block (loops)." - { true } while_true: { - block call - } + block loop } def println { "Same as Console println: self. Prints the object on STDOUT, followed by a newline." Console println: to_s @@ -35,54 +33,53 @@ def != other { "Indicates, if two objects are unequal." self == other not } - def if_false: block { - "Calls the block." - nil + def if_true: block { + "Calls the @block if @true? returns @true" + block call: [self] } - def if_nil: block { - "Returns nil." - nil + def if_true: then_block else: else_block { + "Calls the @then_block if @true? returns @true - otherwise @else_block is called" + then_block call: [self] } - def nil? { - "Returns nil." + def if_false: block { + "Calls the @block if @false? returns @true@" nil } - def false? { - "Returns nil." - nil + def if_false: then_block else: else_block { + "Calls the @then_block if @false? returns @true - otherwise @else_block is called" + else_block call } - def true? { - "Returns nil." + def if_nil: block { + "Calls the @block if @nil? returns @true@" nil } - def if_do: block { - "If the object is non-nil, it calls the given block with itself as argument." + def if_nil: then_block else: else_block { + "Calls the @then_block if @nil? returns @true - otherwise @else_block is called" + else_block call + } - match self { - case nil -> nil - case false -> nil - case _ -> block call: [self] - } + def nil? { + "Returns @false." + false } - 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.""" + def false? { + "Returns @false." + false + } - match self { - case nil -> else_block call: [self] - case false -> else_block call: [self] - case _ -> then_block call: [self] - } + def true? { + "Returns @false." + false } def or_take: other { "Returns self if it's non-nil, otherwise returns the given object." @@ -103,38 +100,73 @@ def to_i { 0 } - def || other { - "Returns @ self if self is true-ish, otherwise returns @other" - self if_do: { + def to_enum { + FancyEnumerator new: self + } + + def to_enum: iterator { + FancyEnumerator new: self with: iterator + } + + def and: other { + """ + Boolean conjunction. + Returns @other if @self and @other are true-ish, otherwise @false. + """ + + if_true: { + { other = other call } if: (other is_a?: Block) + return other + } + return self + } + + def or: other { + """ + Boolean disjunction. + Returns true if either @self or other is true, otherwise nil. + """ + if_true: { return self } else: { + { other = other call } if: (other is_a?: Block) return other } } - def && other { - "Same as Object#and:" - and: other + def or: other { + """ + Boolean disjunction. + Returns true if either @self or other is true, otherwise nil. + """ + unless: self do: { + { other = other call } if: (other is_a?: Block) + return other + } + return self } + alias_method: ':&& for: 'and: + alias_method: ':|| for: 'or: + def if: cond then: block { """ Same as: - cond if_do: block + cond if_true: block """ - cond if_do: block + cond if_true: block } def if: cond then: then_block else: else_block { """ Same as: - cond if_do: then_block else: else_block + cond if_true: then_block else: else_block """ - cond if_do: then_block else: else_block + cond if_true: then_block else: else_block } def while: cond_block do: body_block { """ Same as: @@ -154,14 +186,14 @@ } def unless: cond do: block { """ Same as: - cond if_do: { nil } else: block + cond if_true: { nil } else: block """ - cond if_do: { nil } else: block + cond if_true: { nil } else: block } def method: method_name { "Returns the method with a given name for self, if defined." @@ -202,10 +234,41 @@ val = value block call: [val] val } + def if_responds? { + """ + @return RespondsToProxy for @self + + Returns a @RespondsToProxy@ for @self that forwards any messages + only if @self responds to them. + + Example usage: + + # only send 'some_message: if object responds to it: + object if_responds? some_message: some_parameter + """ + + RespondsToProxy new: self + } + + def backtick: str { + """ + This is the default implementation for backtick: which gets called when using the backtick syntax. + For example: + `cat README` + Gets translated to the following message send: + self backtick: \"cat README\" + Which allows for custom implementations of the backtick: method, if needed. + This default implementation works the same way as in Ruby, Perl or Bash. + It returns the output of running the given string on the command line as a @String@. + """ + + System pipe: str . read + } + def ? future { future value } def yield { @@ -216,7 +279,27 @@ Fiber yield: values } def wait: seconds { Fiber yield: [seconds] + } + + def next { + "Skip to the next iteration" + Fancy NextIteration new raise! + } + + def next: value { + "Return value for this iteration and skip to the next one" + (Fancy NextIteration new: value) raise! + } + + def break { + "Stop iterating" + Fancy BreakIteration new raise! + } + + def break: value { + "Return value from iteration" + (Fancy BreakIteration new: value) raise! } }