lib/object.fy in fancy-0.4.0 vs lib/object.fy in fancy-0.5.0

- old
+ new

@@ -27,27 +27,27 @@ } def println { """ Same as: - Console println: self + Console println: self Prints @self on @STDOUT, followed by a newline. """ - Console println: to_s + *stdout* println: to_s } def print { """ Same as: - Console print: self + Console print: self Prints @self on STDOUT. """ - Console print: to_s + *stdout* print: to_s } def != other { """ @other Other object to compare against. @@ -217,38 +217,38 @@ alias_method: ':|| for: 'or: def if: cond then: block { """ Same as: - cond if_true: block + cond if_true: block """ cond if_true: block } def if: cond then: then_block else: else_block { """ Same as: - cond if_true: then_block else: else_block + cond if_true: then_block else: else_block """ cond if_true: then_block else: else_block } def while: cond_block do: body_block { """ Same as: - cond_block while_do: body_block + cond_block while_do: body_block """ cond_block while_do: body_block } def until: cond_block do: body_block { """ Same as: - cond_block until_do: body_block + cond_block until_do: body_block """ cond_block until_do: body_block } @@ -273,20 +273,20 @@ } def unless: cond do: block { """ Same as: - cond if_true: { nil } else: block + cond if_true: { nil } else: block """ cond if_true: { nil } else: block } def unless: cond do: block else: else_block { """ Same as: - cond if_true: else_block else: block + cond if_true: else_block else: block """ cond if_true: else_block else: block } @@ -336,15 +336,15 @@ @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 - } + # this will return [1,2] + returning: [] do: |arr| { + arr << 1 + arr << 2 + } """ val = value block call: [val] val @@ -355,26 +355,25 @@ @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 + Example: + # 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` + `cat README` Gets translated to the following message send: - self backtick: \"cat README\" + 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@. """ @@ -568,27 +567,29 @@ } def do: block { """ @block @Block@ to be called in the context of @self. + @return @self. Helper method that calls @block with @self as the receiver. This allows message cascading like code, e.g.: - some_complex_object do: { - method_1: arg1 - method_2: arg2 - method_3: arg3 - } + some_complex_object do: { + method_1: arg1 + method_2: arg2 + method_3: arg3 + } - # this is the same as: - some_complex_object method_1: arg1 - some_complex_object method_2: arg2 - some_complex_object method_3: arg3 + # this is the same as: + some_complex_object method_1: arg1 + some_complex_object method_2: arg2 + some_complex_object method_3: arg3 """ block call_with_receiver: self + self } def slots { """ @return @Set@ of slot names that @self has. @@ -605,7 +606,41 @@ Sets the current Thread (in which self is running) for a given amount to sleep. """ Thread sleep: seconds + } + + def let: var_name be: value in: block (nil) { + """ + @var_name @Symbol@ that represents the name of the dynamic variable to be set. + @value Value for the variable. + @block @Block@ in which @var_name will be dynamically bound to @value. + @return Returns @value + + Dynamically rebinds @var_name as dynamic variable with @value as the value within @block. + + Example: + File open: \"/tmp/output.txt\" modes: ['write] with: |f| { + let: '*stdout* be: f in: { + \"hello, world!\" println # writes it to file not STDOUT + } + } + """ + + { return value } unless: var_name + unless: block do: { + Thread current[var_name]: value + return value + } + + oldval = Thread current[var_name] + retval = nil + try { + Thread current[var_name]: value + retval = block call + } finally { + Thread current[var_name]: oldval + return retval + } } } \ No newline at end of file