string as optional parameter for method?

Discussion in 'Ruby' started by Varun Goel, May 14, 2008.

  1. Varun Goel

    Varun Goel Guest

    Hi All,
    I have one method like def hello(int,string)
    Now i want that if user pass int value or string value then it should
    search on the basis of one of those.

    means i want both as optional parameter.
    Please tell me how to do this in ROR.
    Thanks
    Varun
    --
    Posted via http://www.ruby-forum.com/.
    Varun Goel, May 14, 2008
    #1
    1. Advertising

  2. Varun Goel

    Axel Etzold Guest

    -------- Original-Nachricht --------
    > Datum: Thu, 15 May 2008 00:33:37 +0900
    > Von: Varun Goel <>
    > An:
    > Betreff: string as optional parameter for method?


    > Hi All,
    > I have one method like def hello(int,string)
    > Now i want that if user pass int value or string value then it should
    > search on the basis of one of those.
    >
    > means i want both as optional parameter.
    > Please tell me how to do this in ROR.
    > Thanks
    > Varun
    > --
    > Posted via http://www.ruby-forum.com/.


    Dear Varun,

    maybe something like this ?


    def hello(first=nil,second=nil)
    if first.class==Fixnum and second.class==String
    p 'you entered something for both int and string'
    elsif first.class==Fixnum
    p 'you entered something for int'
    elsif first.class==String and second==nil
    p 'you entered something for string'
    else
    p 'come on, read the manual!'
    end
    end


    p hello(5,'string')
    p hello(5)
    p hello('string')
    p hello(5.78,nil)
    p hello()


    Best regards,

    Axel
    --
    GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
    Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx
    Axel Etzold, May 14, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    6
    Views:
    69,828
    grizzleybear
    Apr 17, 2012
  2. Otto Wyss
    Replies:
    6
    Views:
    429
    Jeff Flinn
    Nov 9, 2004
  3. Replies:
    2
    Views:
    268
    Bruno Desthuilliers
    Oct 30, 2006
  4. C Barr Leigh
    Replies:
    12
    Views:
    501
    Bruno Desthuilliers
    Mar 8, 2007
  5. Sinex

    Usage of Use="Optional" on a web method parameter...

    Sinex, May 1, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    155
    Sinex
    May 1, 2007
Loading...

Share This Page