SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: private?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,647
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    private?

    I'm trying to understand a ruby class,\
    Code Ruby:
    class Timer
     
      attr_accessor :seconds
     
      def initialize
        @seconds = 0
      end
     
      def time_string
        convert(@seconds)
        "#{padded(@hrs)}:#{padded(@min)}:#{padded(@sec)}"
      end
     
      private
     
      def padded(num)
        if num <= 9
          "0#{num}"
        else
          "#{num}"
        end
      end
     
      def convert(num)
        if num >= 3600
          @hrs = num / 3600
          @min = (num - (@hrs * 3600)) / 60
          @sec = num - (3600 + (@min * 60))
        elsif num < 3600 && num >= 60
          @hrs = 0
          @min = num / 60
          @sec = num - (@min * 60)
        else
          @hrs = 0
          @min = 0
          @sec = num
        end
      end
    end

    The only thing i dont understand is, what is the deal with private being in the middle?

    Thank you!
    Last edited by Pullo; Jul 28, 2013 at 05:46. Reason: Added code tags
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    When posting code samples, could you please use code tags.
    See here: http://www.sitepoint.com/forums/show...=1#post5407985

    To answer your question, when private is invoked without arguments, it sets access to private for all subsequent methods, meaning that they may only be accessed by methods of the same class.

    E.g.

    Code Ruby:
    t = Timer.new
    => #<Timer:0x436668 @seconds=0>
     
    t .seconds = 100
    => #<Timer:0x436668 @seconds=100>
     
    t.time_string
    => "00:01:40"
     
    t.padded(100)
    => timer.rb:46:in `<main>': private method `padded' called for #<Timer:0x436668 @seconds=100, @hrs=0, @min=1, @sec=40> (NoMethodError)

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,647
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks again, ya'know you Germans are a ok bunch (and smart)!
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,337
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Pullo View Post
    To answer your question, when private is invoked without arguments, it sets access to private for all subsequent methods, meaning that they may only be accessed by methods of the same class.
    And when arguments are provided they are used to designate those methods that are to be private within the class.

    Code ruby:
    class Timer
     
      private :padded, :convert
     
      attr_accessor :seconds
     
      def initialize
        @seconds = 0
      end
     
      def time_string
        convert(@seconds)
        "#{padded(@hrs)}:#{padded(@min)}:#{padded(@sec)}"
      end
     
      def padded(num)
        if num <= 9
          "0#{num}"
        else
          "#{num}"
        end
      end
     
      def convert(num)
        if num >= 3600
          @hrs = num / 3600
          @min = (num - (@hrs * 3600)) / 60
          @sec = num - (3600 + (@min * 60))
        elsif num < 3600 && num >= 60
          @hrs = 0
          @min = num / 60
          @sec = num - (@min * 60)
        else
          @hrs = 0
          @min = 0
          @sec = num
        end
      end
    end
    Last edited by ParkinT; Jul 28, 2013 at 11:09. Reason: For completeness
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,941
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    ya'know you Germans are a ok bunch (and smart)!
    Thanks
    I'm afraid I'm British though (ex-pat).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •