| | |
Artificial System Clock Code review critique
Please support our Ruby advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
Hi - I'm very new here (1st post). I read the introductory docs, and looked at the categories, but I didn't see a specific place for posting code for review/critique. My apologies if this is not the appropriate place to do that...
Here is some code I am hoping to get reviewed/critiqued by people who know Ruby. I don't know squat about Ruby, but I am pretty familiar with modern OO lang's (C++, Java, C#, PHP, etc.), and I wrote this after reading a few tutorials.
It is an implementation of an Artificial System Clock (ASC). The idea is that you will use the getSystemDate method of this class everywhere in your app. that gets the date/time directly. Then you can change the date-time context for the whole app. for testing purposes. When in "channel" mode, it will go fetch the date-time value from TimeChannels.com, so that you can integration-test with multiple interfacing apps. all on the same shared ASC value.
Here's the TimeChannels class I wrote in Ruby:
Many thanks in advance to all who reply...
John
Here is some code I am hoping to get reviewed/critiqued by people who know Ruby. I don't know squat about Ruby, but I am pretty familiar with modern OO lang's (C++, Java, C#, PHP, etc.), and I wrote this after reading a few tutorials.
It is an implementation of an Artificial System Clock (ASC). The idea is that you will use the getSystemDate method of this class everywhere in your app. that gets the date/time directly. Then you can change the date-time context for the whole app. for testing purposes. When in "channel" mode, it will go fetch the date-time value from TimeChannels.com, so that you can integration-test with multiple interfacing apps. all on the same shared ASC value.
Here's the TimeChannels class I wrote in Ruby:
Ruby Syntax (Toggle Plain Text)
require 'date' require 'net/http' require 'open-uri' class TimeChannels @@tcURL = "http://www.timechannels.com/GetChannel" @@tcURLms = "http://www.timechannels.com/GetChannelMS" @@tcAscState = "TimeChannels.state.Production" @@tcChannel = "0" @@tcAscDateTime = nil @@tcServiceURL = "http://www.timechannels.com/GetChannel" @@tcMillisecondMode = "false" def TimeChannels.setToProductionState @@tcAscState = "TimeChannels.state.Production" end def TimeChannels.setToStaticTestState(aDateTimeValue) @@tcAscState = "TimeChannels.state.TestDate" @@tcAscDateTime = aDateTimeValue end def TimeChannels.setToTestState(aChannel) @@tcAscState = "TimeChannels.state.TestChannel" @@tcChannel = aChannel end def TimeChannels.setToMillisecondMode() @@tcMillisecondMode = "true" @@tcServiceURL = @@tcURLms end def TimeChannels.setToStandardMode @@tcMillisecondMode = "false" @@tcServiceURL = @@tcURL end def TimeChannels.getSystemDate if @@tcAscState == "TimeChannels.state.Production" return DateTime.now end if @@tcAscState == "TimeChannels.state.TestDate" return @@tcAscDateTime end if @@tcAscState == "TimeChannels.state.TestChannel" return getChannelFromTimeChannelsService(@@tcChannel) end DateTime.now end def TimeChannels.getHTTPContent(url) #res = Net::HTTP.get_response(URI.parse(url)) #data = res.body data = open(url).read data end def TimeChannels.getChannelFromTimeChannelsService(aChannel) url = @@tcServiceURL + "?channel=" + aChannel result = getHTTPContent(url) if (result.nil?) return Time.now end if (@@tcMillisecondMode == "true") retVal = result #retVal = DateTime.parse(result) else retVal = DateTime.parse(result) end retVal end end
Many thanks in advance to all who reply...
John
I have a suggestion, I think your code need a exception handler when your internet connection is lost.
Code Snippet Web log:
http://snippet.c0de.me
http://snippet.c0de.me
Though I'm no Ruby expert, here are a few suggestions:
can also be written as:
= Instead of using
= Your ASC state can be better represented using Ruby Symbols.
= In Ruby in absence of a can be replaced by: though as already mentioned, handling exceptions in case establishment of a connection fails should come first.
= The Ruby way of naming variables and methods is *not* to use camelCase but use under_scores; though it's a matter of taste. However it pays to follow and use the conventions used by the community in general to facilitate easier understanding of others' code and vice-versa.
In case you require a more in-depth review, feel free to post your code on the official ruby forums and you'll surely get more feedback.
HTH
-sos
Ruby Syntax (Toggle Plain Text)
if (result.nil?) return Time.now end
Ruby Syntax (Toggle Plain Text)
return Time.now if result.nil?
= Instead of using
@@tcMillisecondMode = "false" , why not just use @@tcMillisecondMode = false ?= Your ASC state can be better represented using Ruby Symbols.
= In Ruby in absence of a
return statement, the value of the last expression evaluated is returned hence the method: Ruby Syntax (Toggle Plain Text)
def TimeChannels.getHTTPContent(url) data = open(url).read data end
Ruby Syntax (Toggle Plain Text)
def TimeChannels.getHTTPContent(url) open(url).read end
= The Ruby way of naming variables and methods is *not* to use camelCase but use under_scores; though it's a matter of taste. However it pays to follow and use the conventions used by the community in general to facilitate easier understanding of others' code and vice-versa.
In case you require a more in-depth review, feel free to post your code on the official ruby forums and you'll surely get more feedback.
HTH
-sos
Last edited by ~s.o.s~; Aug 8th, 2009 at 12:22 pm.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- System Clock Disappears (Windows NT / 2000 / XP)
- Excercise in class design - code review (C++)
- Getting time from system clock in nanoseconds (C++)
- calling system clock? (C++)
- Review/ critique this site please (Website Reviews)
- system clock/date language (Visual Basic 4 / 5 / 6)
- System clock lose about 4mins per hour!!! (Windows NT / 2000 / XP)
- Mandrake guest in VMware - System clock frozen... (*nix Software)
Other Threads in the Ruby Forum
- Previous Thread: Domain Name Checker
- Next Thread: HAML/SASS Auto-Updater
| Thread Tools | Search this Thread |






