how to pass unkown length of parameters into a class method

Discussion in 'Ruby' started by Peter Loftus, Dec 18, 2007.

  1. Peter Loftus

    Peter Loftus Guest

    #!/usr/bin/ruby
    class Example
    def Examplemethod(var1)
    puts var1
    end
    end

    Check = Example.new
    Check.Examplemethod("Loftz")

    Hey guys

    So this is just calling a method that prints out a name. Im just
    wondering can i specify that there may be mulitple parameters going into
    this method

    for example
    Check.Example("Loftz","peter","john","paul","gary")

    and have that method inside the class Example not know how many
    parameters are going to be passed into it?

    def Examplemethod(?)

    Regards
    Loftz
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Loftus, Dec 18, 2007
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    def Example(*args)
    args.each do |arg|
    # Process individual argument
    end
    end

    Jason

    On Dec 18, 2007 7:16 AM, Peter Loftus <> wrote:

    > #!/usr/bin/ruby
    > class Example
    > def Examplemethod(var1)
    > puts var1
    > end
    > end
    >
    > Check = Example.new
    > Check.Examplemethod("Loftz")
    >
    > Hey guys
    >
    > So this is just calling a method that prints out a name. Im just
    > wondering can i specify that there may be mulitple parameters going into
    > this method
    >
    > for example
    > Check.Example("Loftz","peter","john","paul","gary")
    >
    > and have that method inside the class Example not know how many
    > parameters are going to be passed into it?
    >
    > def Examplemethod(?)
    >
    > Regards
    > Loftz
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Jason Roelofs, Dec 18, 2007
    #2
    1. Advertising

  3. Peter Loftus

    cruiserdan Guest

    Just to elaborate a little on Jason's point, for those who are
    interested in what this code is doing, it is based on Ruby's extremely
    cool array assignment feature. This is explained here:

    http://phrogz.net/ProgrammingRuby/tut_expressions.html#parallelassignment

    and applies to parameter assignment in method invocations as well, as
    Jason pointed out:

    http://phrogz.net/ProgrammingRuby/tut_methods.html#variablelengthargumentlists

    That is to say, that variable argument lists in Ruby is actually using
    nested assignment, which you can use anywhere, not just in method
    definitions.

    Best of luck,
    -Dan
    ---
    http://dev.zeraweb.com/



    On Dec 18, 4:33 am, Jason Roelofs <> wrote:
    > [Note: parts of this message were removed to make it a legal post.]
    >
    > def Example(*args)
    > args.each do |arg|
    > # Process individual argument
    > end
    > end
    >
    > Jason
    >
    > On Dec 18, 2007 7:16 AM, Peter Loftus <> wrote:
    >
    > > #!/usr/bin/ruby
    > > class Example
    > > def Examplemethod(var1)
    > > puts var1
    > > end
    > > end

    >
    > > Check = Example.new
    > > Check.Examplemethod("Loftz")

    >
    > > Hey guys

    >
    > > So this is just calling a method that prints out a name. Im just
    > > wondering can i specify that there may be mulitple parameters going into
    > > this method

    >
    > > for example
    > > Check.Example("Loftz","peter","john","paul","gary")

    >
    > > and have that method inside the class Example not know how many
    > > parameters are going to be passed into it?

    >
    > > def Examplemethod(?)

    >
    > > Regards
    > > Loftz
    > > --
    > > Posted viahttp://www.ruby-forum.com/.
     
    cruiserdan, Dec 18, 2007
    #3
    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. moondaddy
    Replies:
    1
    Views:
    6,828
    moondaddy
    Feb 28, 2004
  2. Klaas Vantournhout

    unkown number of nested for loops

    Klaas Vantournhout, Sep 21, 2006, in forum: C Programming
    Replies:
    3
    Views:
    350
    Lew Pitcher
    Sep 21, 2006
  3. keith
    Replies:
    9
    Views:
    2,155
    Graham Underwood
    Aug 8, 2008
  4. rkbnair
    Replies:
    5
    Views:
    30,979
    bruce barker
    Jan 30, 2008
  5. Chuck Remes
    Replies:
    3
    Views:
    210
    hemant
    Aug 12, 2010
Loading...

Share This Page