[ create a new paste ] login | about

Link: http://codepad.org/d4UB5mV2    [ raw code | output | fork ]

joshua_cheek - Ruby, pasted on Oct 27:
today = Time.now


# To format the time, you submit a format string to the strftime method
# the options you can use in the string can be seen at
# http://ruby-doc.org/core/classes/Time.html#M000298
#
# we can see that %b and %B correllate to the month so lets try them
puts "Using strftime"
puts today.strftime('%b') # => "Oct"
puts today.strftime('%B') # => "October"



# if this is the kind of thing we are doing very frequently,
# and we don't want to have to keep writing all of that stuff
# we can make our own method to abstract it. Lets call it month_name
class Time
  def month_name
    strftime '%B'   #equivalent to self.strftime("%B")
  end
end

puts "\nUsing our month_name"
puts today.month_name # => "October"


Output:
1
2
3
4
5
6
Using strftime
Oct
October

Using our month_name
October


Create a new paste based on this one


Comments: