Computer Science Canada

[TYS] Properties

Author:  wtd [ Mon Jan 08, 2007 6:57 pm ]
Post subject:  [TYS] Properties

Create a class Foo, with instance variables "bar" and "baz" which are both readable and writable. The class should also have a method to_s which returns the two variables stringified, joined together separated by a space.

You may not use attr_*, "def" may occur at most twice, and the word "eval" may not occur in your code. You may not use "require" in your code either.

Author:  ericfourfour [ Tue Jan 09, 2007 11:17 pm ]
Post subject:  Re: [TYS] Properties

Does this pass?
Ruby:
Foo = Struct.new(:bar, :baz)
class Foo
  def to_s
    "#{bar} #{baz}"
  end
end

foo = Foo.new
foo.bar = "Hello"
foo.baz = "World"
puts foo

I added a bit at the end.


: