Tuesday, October 11, 2011

Creating an object in ruby

To create an object from an existing class, the new method is typically used:
myFile = File.new("textfile.txt","w")
myString = String.new("This is a string object")

This is not always explicitly required, however. In particular, you would not normally bother to call new for a String object as in the previous example:
yourString = "This is also a string object"
aNumber = 5 # new not needed here, either

No comments:

Post a Comment