Greetings,
I have recently decided to dive into the Smalltalk 6.0 world of programming and have started off by playing around with the Date class for concept building. What i am trying to do is change the display of the Date in the workspace. for example: ------- Date today. ------ will return: Tuesday, August 29, 2006 Now, what I would like to do is change the display to: yyDDmm I believe I found the code, but I am not sure what astream is or how to put it in. Code I am trying to figure out: ----- Date new printOn: aStream format: 'yyMMdd'. ----- now if you run it, you can debug and I can see that yyMMdd displays: 060829 which is what i want to display. can someone explain aStream to me as well as how I put it into the code above? |
see my response in comp.lang.smalltalk...
"Rich" <[hidden email]> wrote in message news:[hidden email]... > Greetings, > > I have recently decided to dive into the Smalltalk 6.0 world of > programming and have started off by playing around with the Date class > for concept building. > > > What i am trying to do is change the display of the Date in the > workspace. > > > for example: > ------- > > > Date today. > > > ------ > will return: Tuesday, August 29, 2006 > > > Now, what I would like to do is change the display to: yyDDmm > > > I believe I found the code, but I am not sure what astream is or how to > > put it in. > > > Code I am trying to figure out: > ----- > > > Date new printOn: aStream format: 'yyMMdd'. > > > ----- > > > now if you run it, you can debug and I can see that yyMMdd displays: > 060829 > which is what i want to display. > > > can someone explain aStream to me as well as how I put it into the code > > above? > |
In reply to this post by Rich
Rich,
>I have recently decided to dive into the Smalltalk 6.0 world of >programming A wise choice :-) Welcome. >What i am trying to do is change the display of the Date in the >workspace. > >Date today. The first step is to understand what you are doing here. You are sending a message, #today, to the Date class. It uses the Windows api to find out what the current date is and then answers an instance of the Date class which contains that information. That's all it does. If you just evaluate the above statement then the instance of Date that is answered is just discarded. However, what you are probably doing is using "Display it" or "Inspect it" from a workspace to see the value of the Date object. This involves another message send, one performed automatically by the workspace, which asks the Date object to describe itself as a printable String. You can ask _any_ object to describe itself by sending it the #printString or #displayString message (which end up sending the #printOn: message). The default behaviour of a Date object is to answer a String formatted using the "Long Picture" - i.e. "Tuesday, August 29, 2006" You can change that behavior to tell the Date class to default to using the "Short Picture" In a workspace enter Date defaultLongPicture: false. Date today Highlight it all and then "Display it" . The default formatting for a String answered by Date (in my locale) now shows as 29/08/2006 Set the format back by using Date defaultLongPicture: true. OK, that's dealt with the default formatting of a Date object. The Date class obtains these default formats from the Locale information for your system - which is why my short date is formatted DD/MM/YYYY. What you are looking for is a date format that is not a default for your locale (an assumption on my part). To do that you have to tell the Date how you want it to be formatted. Aside - a Stream is just a Smalltalk class that allows you to store and retrieve objects (characters in this case) sequentially. You don't really need to understand it here - but you will in the future as it's a very imporatnt part of the class hierarchy. Enter the following into a Workspace - comments interspersed" "Create a new Stream that streams over a String - i.e. a sequence of characters" aStream := String writeStream. "get a new instance of the Date class containing todays date" aDate := Date today. "ask this Date object to describe itself, in a specific format, on the Stream object that we created. aDate printOn: aStream format: 'yyMMdd'. "now ask the stream for the sequence of character that it contains" aStream contents inspect If you select all of that and evaluate it you should get an Inspector open up on the String "060829" OK that's how you get a formatted String using a workspace. If you are going to want to do this a lot then one way of making it easier is to add a method to the Date class that does it for you. For example, you could implement an instance method Date>>printStringYYMMDD | stream | stream := String writeStream. self printOn: stream format: 'yyMMdd'. ^stream contents You could then just evaluate Date today printStringYYMMDD and it would answer a correctly formatted String. Note: If you want to change the behaviour for Date, so that it defaults to answering a String in the YYMMDD format, then it is possible but more complex. I've skipped a lot here but, hopefully, it should get you started. Please ask again if you want anything clarified. The best way to understand Smalltalk is 1) Use the ClassBrowser to look at the methods implemented by a class - in this case Date. You can learn a lot about how a class works by just reading the comments. 2) Experiment. Use the debugger to evaluate bits of code in a workspace and see where it takes you. -- Ian The From address is valid - for the moment. |
Ian & James,
Thank you so much for your input to my questions. Being that the only type of programing that I have done up to this part is autoit, I am still learning concepts and I am greatful for your detail explainations as well as examples. One of my good friends who is a smalltalk programmer has shown the class comments in the class bowser, though certain concepts I am still unclear of. I will post more questions as they come. In addition, I have plugged in the code and it works great. Initially, I use the workspace to evaluate the code then put it in an instance method of my main program. So far, I am truly impressed with the power of smalltalk with how little code writing you have to do inorder to make great programs. thanks agian, Rich |
Free forum by Nabble | Edit this page |