0

I have a class with the attribute .weekday saved as an integer value.

I'm trying to create a method in a module that converts that numeric value to the corresponding weekday.

This is how I like to work:

MyClass.weekday
=> 2
MyClass.weekday.my_module_method
=> "Tuesday"

Is it possible to do this conversion with a module method or am I thinking wrong here?

I can access the object from within the module mehtod, by self, but I don't seem to be able to do self.weekday.

8
  • You want 2.my_module_method to return "Tuesday"? Commented May 18, 2014 at 12:40
  • There is a wday to get the integer value of a day.Like this example DateTime.parse("Tuesdayy").wday #=> 3. And if you want the day,just use yourdateObj.strftime('%A').No need of these conversions. Commented May 18, 2014 at 12:45
  • 2
    Not clear. Post the def initions. For instance in a module self could be the module itself. Commented May 18, 2014 at 12:47
  • 1
    Just define on your model, def dayname Date::DAYNAMES[weekday] end and on call attribute with dayname Commented May 18, 2014 at 12:57
  • Do you mean the ISO 8601 weekday? Even so, there are two versions: Sunday = 0 (U.S. and some other strange countries) and Sunday = 7 (The whole rest of the world). It is not clear what you want. Commented May 18, 2014 at 12:58

1 Answer 1

1

What you are trying to do is certainly possible. You are correct when you point to ActiveRecord::Inflector as something that works similarly. This approach modifies the Fixnum class itself to add new methods and although I don't generally recommend ad-hoc patching of core classes, you can see it in action in active_support/core_ext/integer/inflections.rb :

require 'active_support/inflector'

class Integer
  # Ordinalize turns a number into an ordinal string used to denote the
  # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
  #
  #  1.ordinalize     # => "1st"
  #  2.ordinalize     # => "2nd"
  #  1002.ordinalize  # => "1002nd"
  #  1003.ordinalize  # => "1003rd"
  #  -11.ordinalize   # => "-11th"
  #  -1001.ordinalize # => "-1001st"
  #
  def ordinalize
    ActiveSupport::Inflector.ordinalize(self)
  end
end

In your case I might do something like:

module WeekdayInflector
  def weekday
    Date::DAYNAMES[self]
  end
end


class Fixnum
  include WeekdayInflector
end

which will at least help others track down the methods you added by looking at the module. Please note that this will affect ALL instances of Fixnum and could lead to conflicts if you include a Gem that tries to do the same thing. It is worth asking whether this tradeoff is worth it or if defining a simple view helper is the better way to go.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.