Topic: date printing...the "ruby way"

I came up with this.

def pretty_date(date)
  return date unless date.is_a?(Time)
  return date.strftime("%B %d, %Y") unless Time.now.year == date.year
  date.strftime("%B %d")
end

Basically, if the date is THIS year, I don't show the year.  Otherwise, I show the year.

Does this look pretty clean?  I'm not having any issues I just want to see if I can learn something.  :-)

Thanks.

Re: date printing...the "ruby way"

cbmeeks wrote:

I came up with this.

def pretty_date(date)
  return date unless date.is_a?(Time)
  return date.strftime("%B %d, %Y") unless Time.now.year == date.year
  date.strftime("%B %d")
end

Basically, if the date is THIS year, I don't show the year.  Otherwise, I show the year.

Does this look pretty clean?  I'm not having any issues I just want to see if I can learn something.  :-)

Thanks.

I don't understand what that first line is for
  return date unless date.is_a?(Time)

All the "return unless"  stuff makes it hard to understand the logic, for me anyway.  Better to use if elses, or a case statement, rather than lots of "return unless".

Also, why bring Time into this at all?  We don't really care whether it's a date or a time, we just want the day, month and year which we can get out of either..

Here's a shorter version

def pretty_date(date)
  date.strftime("%B %d#{", %Y" if date.year != Date.today.year}")
end

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: date printing...the "ruby way"

Max Williams wrote:

I don't understand what that first line is for
  return date unless date.is_a?(Time)

It's basically in case a non-Time object gets passed in.  This will probably never happen in my app so I could probably remove it.

Max Williams wrote:

All the "return unless"  stuff makes it hard to understand the logic, for me anyway.  Better to use if elses, or a case statement, rather than lots of "return unless".

That's the camp I'm from.  When I first started programming I had no guides or mentors.  So I would do stuff like tons of if/then/else.  But seems like a lot of the code I've seen in the Ruby community seem to use a lot of unless conditions. 
I am probably just using it in the wrong context.

Max Williams wrote:

Also, why bring Time into this at all?  We don't really care whether it's a date or a time, we just want the day, month and year which we can get out of either..


Max Williams wrote:

Here's a shorter version

def pretty_date(date)
  date.strftime("%B %d#{", %Y" if date.year != Date.today.year}")
end

I will give that a shot.

Thanks

Re: date printing...the "ruby way"

unless has its roots in Perl, especially when used as a statement modifier, and I rather like it. Sometimes, particularly if you are trying to handle an exceptional condition that only occurs occasionally, it seems more natural to code "do this unless (unlikely logic combination)" rather than "if (! unlikley logic combination) do this". It highlights the normal flow rather than the unexpected condition and makes it more obvious.

However, style is a matter of personal taste. That's just my $0.02 worth...

Re: date printing...the "ruby way"

specious wrote:

unless has its roots in Perl, especially when used as a statement modifier, and I rather like it. Sometimes, particularly if you are trying to handle an exceptional condition that only occurs occasionally, it seems more natural to code "do this unless (unlikely logic combination)" rather than "if (! unlikley logic combination) do this". It highlights the normal flow rather than the unexpected condition and makes it more obvious.

However, style is a matter of personal taste. That's just my $0.02 worth...

I use unless, there's nothing wrong with unless in itself.  unless is great as an alternative to 'if some_condition == false'.  I use unless like this

local_var = false unless defined?(local_var)

Nice and simple and readable.

However, unless can be confusing to the reader (thus making your code less well written in my opinion).  For example it's confusing if you use it in combination with else -

unless x < 15
  print "foo"
else
  print "bar"
end

...err...so we print "bar" when...exactly?  I'm confused...

This is also confusing as a program structure:

return "foo" unless x.first < 15
return "bar" unless x is_a?(Array)
return "chicken" unless x.size = 5

Trying to read through this, like 'so, if x is [1,2,3], this would return....errr...doh, confused again!  Maybe it's just me being dumb but i do believe that some code is easier to read than other code, and you should go with readability as a top priority.  Every time you or someone else have to stop and reverse engineer a chunk of code to work out what the hell it's doing, the code writer screwed up.

I would rewrite this as

if x.first >= 15
  return "foo"
elsif  x.is_a?(Array) == false
  return "bar"
elsif x.size != 5
  return "chicken"
else
  return nil
end

It's still confusing, because of the disparate and unrelated conditions, but it's a bit easier to get your head around.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: date printing...the "ruby way"

I'll jump in too. My personal preference is for very readable code, even if it's a few lines longer. Here's what I would probably end up with:


def pretty_date(date)
  result = date.strftime("%B %d")
  result << date.strftime(", %Y") if date.year > Time.now.year
end

I'm assuming you are working only with future dates.

Re: date printing...the "ruby way"

Hi,

Is anybody knows that how to display the current date in perl?
As I am new to perl, I find some difficulties.
please help me.

Last edited by Adan Daroski (2010-02-22 06:37:47)

Re: date printing...the "ruby way"

Adan Daroski wrote:

Hi,

Is anybody knows that how to display the current date in perl?
As I am new to perl, I find some difficulties.
please help me.

Why don't you try a perl forum?  This is a ruby forum.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: date printing...the "ruby way"

Don't know if we are allowed to recommend other forums, but www.tek-tips.com has a good Perl forum. Their Ruby forum is pretty rubbish, though wink