SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    hyderabad
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    5.next=6, what about 5.prev ??

    Hi,

    There is a method called next for fixnum to get next number. Whats the method to get previous number.

    I need a straight method if any. I searched manual and not found one..

    I hope this will give a thought..

    number=5
    number.next=6

    to get 4 , which method i need to use..?. I need a straight method not alternatives..?

    Thanks,
    Sharma

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    There is no prev/previous method. You'd have to write it yourself. Fortunately it's really easy to do:
    Code:
    number = 5
    number.next #6
    
    class Fixnum
      def prev
        self - 1
      end
    end
    
    number.prev #4

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    hyderabad
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah..That can be possible. But why matz(ruby creator) missed this kind of method by default?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    succ() is used by ranges (similar to next()), I think that's why has been added. If you create a range: (3..10) and iterate over it, Ruby calls the succ() method of 3 until it's 10. That's why you can have ranges of strings or ranges of your own objects.

    If you're going to implement prev() you should probably create an alias named pred():

    succ <=> pred
    next <=> prev

  5. #5
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by phpsharma View Post
    Yeah..That can be possible. But why matz(ruby creator) missed this kind of method by default?
    Maybe because
    Code:
    variable - 1
    is more concise and less verbose than
    Code:
    variable.prev
    To be honest I'd ask the reverse question had Fenrir2 not answered it already: why next() at all?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    hyderabad
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Thanks for your time

    Thank you Fenrir2 & vgarcia,

    vgarcia: You are correct. When we can do with variable+1 why next method for fixnum?

    I hope some extra benifit is there. with next? Just a curious question.

    Sharma

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The pred and succ are polymorphic: they work on strings, numbers, or any type that defines the method. +1 and -1 only work on numbers.

    why next() at all?
    This is a very good question anyway: why does Ruby have so many method aliases? map/collect for example: collect from smalltalk and map from scheme (or maybe from another functional language?). I prefer to use map because it sounds less iterative than collect; more like an atomic operation. People who are used to imperative languages will probably prefer collect. Why does Ruby have these aliased methods at all? (it's not even consistent: there is inject from Smalltalk, but not reduce (from Scheme) or foldl (from Haskell)). This is one thing I don't like about Ruby. TIMTOWTDI is good, but not in this case?


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
  •