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

- old
+ new

@@ -27,24 +27,24 @@ } def println { """ Same as: - Console println: self + *stdout* println: self - Prints @self on @STDOUT, followed by a newline. + Prints @self on @*stdout*, followed by a newline. """ *stdout* println: to_s } def print { """ Same as: - Console print: self + *stdout* print: self - Prints @self on STDOUT. + Prints @self on @*stdout*. """ *stdout* print: to_s } @@ -147,23 +147,23 @@ 0 } def to_enum { """ - @return @FancyEnumerator@ for @self using 'each: for iteration. + @return @Fancy::Enumerator@ for @self using 'each: for iteration. """ - FancyEnumerator new: self + Fancy Enumerator new: self } def to_enum: iterator { """ @iterator Message to use for iteration on @self. - @return @FancyEnumerator@ for @self using @iterator for iteration. + @return @Fancy::Enumerator@ for @self using @iterator for iteration. """ - FancyEnumerator new: self with: iterator + Fancy Enumerator new: self with: iterator } def and: other { """ @other Object or @Block@ (for short-circuit evaluation) to compare @self to. @@ -537,15 +537,32 @@ @__mutex__ = @__mutex__ || { Mutex new() } @__mutex__ synchronize(&block) } def copy_slots: slots from: object { + """ + @slots @Fancy::Enumerable@ of slot names to copy from @object. + @object Target @Object@ to copy slots from. + + Copies slots from @object to @self. + """ + slots each: |s| { set_slot: s value: (object get_slot: s) } } + def copy_slots_from: object { + """ + @object @Object@ to copy slots from. + + Copies all slots from @object to @self. + """ + + copy_slots: (object slots) from: object + } + def get_slots: slots { """ @slots @Array@ of slot names to retrieve from @self. @return @Array@ of slot values of slot names passed in via @slots. """ @@ -588,18 +605,28 @@ block call_with_receiver: self self } + def tap: block { + """ + @block @Block@ to be called with @self. + @return @self. + + Calls a given @Block@ with @self before returning @self. + """ + + block call: [self] + self + } + def slots { """ - @return @Set@ of slot names that @self has. + @return @Array@ of slot names that @self has. """ - Set new: $ instance_variables map: |s| { - s rest to_sym - } + instance_variables map: @{ rest to_sym } } def sleep: seconds { """ @seconds Amount of seconds to sleep. @@ -618,11 +645,11 @@ @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| { + File write: \"/tmp/output.txt\" with: |f| { let: '*stdout* be: f in: { \"hello, world!\" println # writes it to file not STDOUT } } """ @@ -636,11 +663,36 @@ oldval = Thread current[var_name] retval = nil try { Thread current[var_name]: value retval = block call + } catch Exception => e { + e raise! } finally { Thread current[var_name]: oldval return retval + } + } + + def with_output_to: filename do: block { + """ + @filename Filename of file to write to. + @block @Block@ to be executed with *stdout* being bound to the output file. + + Opens @filename and rebinds `*stdout*` to it within @block. + + Example: + with_output_to: \"/tmp/hello_world.txt\" do: { + \"hello\" println + \"world\" println + } + This writes + hello + world + to /tmp/hello_world.txt + """ + + File write: filename with: |f| { + let: '*stdout* be: f in: block } } } \ No newline at end of file