Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [TYS] Properties
Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: