Metaprogramming - args

Hi

Is there any way to write sg like

vvv=(:a..:f)
def mymeth( vvv )
end

     instead of writing

def mymeth(a,b,c,d,e,f)
end

Thanks
Opti

Is there any way to write sg like

vvv=(:a..:f)
def mymeth( vvv )
end

     instead of writing

def mymeth(a,b,c,d,e,f)
end

A common pattern in ruby is to pass a single hash to a method instead of using a lot of parameters.

    def mymeth(args)
      a = args[:a] # etc
    end

    mymeth(a: 1, b: 2, c: 3)

Alternatively you can have a variable number of parameters using the splat operator.

    def mymeth(*args)
      a = args[0] #etc
    end

    mymeth(1, 2, 3)

Is this the sort of thing you mean?

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

Maybe like that:
vvv = ('a'..'f').map(&:to_sym)
mymeth *vvv

···

On Tue, Jan 17, 2017 at 4:10 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Is there any way to write sg like

vvv=(:a..:f)
def mymeth( vvv )
end

    instead of writing

def mymeth(a,b,c,d,e,f)
end

Thanks
Opti

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

​Is it because you're dynamically creating functions and won't know how
many parameters they'll take until runtime, or because you don't feel like
typing them out?

There are different outcomes, depending on how you answer that.

Cheers

···

On 17 January 2017 at 23:10, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Is there any way to write sg like

vvv=(:a..:f)
def mymeth( vvv )
end

    instead of writing

def mymeth(a,b,c,d,e,f)
end

Thanks
Opti

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Maybe like that:
vvv = ('a'..'f').map(&:to_sym)
mymeth *vvv

You do not nee the #map call:

irb(main):006:0> vvv = :a..:f
=> :a..:f
irb(main):007:0> p(*vvv.to_a)
:a
:b
:c
:d
:e
:f
=> [:a, :b, :c, :d, :e, :f]
irb(main):008:0> p(*vvv)
:a
:b
:c
:d
:e
:f
=> [:a, :b, :c, :d, :e, :f]

But no, OP wants to use it during method definition - not invocation:

Is there any way to write sg like

vvv=(:a..:f)
def mymeth( vvv )
end

    instead of writing

def mymeth(a,b,c,d,e,f)
end

Cheers

robert

···

On Tue, Jan 17, 2017 at 2:19 PM, Aleksey Ivanov <ialexxei@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Thanks --
> - But no, OP wants to use it during method definition - not invocation

So it seems not to be possible?

thx Opti

Not so fast: you can always use eval.

robert

···

On Tue, Jan 17, 2017 at 2:27 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Thanks --

- But no, OP wants to use it during method definition - not invocation

So it seems not to be possible?

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Hi
its just for not having to write the full arg-list,
but would also be interesting to use it generally with dynamic evaluation,
for example with attr_reader *dynvars.
Can the name of the method be a variable, too?

thanks
Opti

···

On 2017-01-17 20:32, Matthew Kerwin wrote:

On 17 January 2017 at 23:10, Die Optimisten <inform@die-optimisten.net > <mailto:inform@die-optimisten.net>> wrote:

    Hi

    Is there any way to write sg like

    vvv=(:a..:f)
    def mymeth( vvv )
    end

        instead of writing

    def mymeth(a,b,c,d,e,f)
    end

​Is it because you're dynamically creating functions and won't know how many parameters they'll take until runtime, or because you don't feel like typing them out?

There are different outcomes, depending on how you answer that.

When your argument list is so long that writing it out is painful,
that's a big code smell. I wouldn't usually pass more than about
two or three arguments. Consider passing arrays, hashes,
or specialized data objects instead.

Essentially the same is true for instance variables. Instead of
creating dozens/hundreds/... (?) of instance variables dynamically
it is probably better to use a rather small amount of instance variables
that hold references to more complex data structures. Chances are good
that even a single array or hash would suffice.

Metaprogramming is rarely necessary for "common" programming tasks.

Of course it's difficult to give more specific answers without
knowing your actual use case.

Regards,
Marcus

···

Am 17.01.2017 um 23:49 schrieb Die Optimisten:

its just for not having to write the full arg-list,
but would also be interesting to use it generally with dynamic evaluation,
for example with attr_reader *dynvars.
Can the name of the method be a variable, too?

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A