How to define a method in ruby using splat and an optional hash at the same time? [duplicate]
Asked Answered
M

5

10

I am able to define a method like this:

def test(id, *ary, hash_params)
  # Do stuff here
end

But this makes the hash_params argument mandatory. These don't work either:

def t(id, *ary, hash_params=nil)  # SyntaxError: unexpected '=', expecting ')'
def t(id, *ary, hash_params={})   # SyntaxError: unexpected '=', expecting ')'

Is there a way to make it optional?

Merger answered 19/12, 2012 at 20:32 Comment(2)
Which version of Ruby are you using?Legato
The question is not clear. If you do test(id, hash1), are you expecting ary = [hash1]; hash = nil or ary = []; hash = hash1? What is the rule that determines whichever to choose?Foumart
S
11

You can't do that. You have to think about how Ruby would be able to determine what belongs to *ary and what belongs to the optional hash. Since Ruby can't read your mind, the above argument combination (splat + optional) is impossible for it to solve logically.

You either have to rearrange your arguments:

def test(id, h, *a)

In which case h will not be optional. Or then program it manually:

def test(id, *a)
  h = a.last.is_a?(Hash) ? a.pop : nil
  # ^^ Or whatever rule you see as appropriate to determine if h
  # should be assigned a value or not.
Sizemore answered 19/12, 2012 at 21:0 Comment(0)
E
13

There is support for this in ActiveSupport through the use of the array extension extract_options!.

def test(*args)
  opts = args.extract_options!
end

If the last element is a hash, then it will pop it from the array and return it, otherwise it will return an empty hash, which is technically the same as what you want (*args, opts={}) to do.

ActiveSupport Array#extract_options!

Embrue answered 10/3, 2013 at 8:10 Comment(0)
S
11

You can't do that. You have to think about how Ruby would be able to determine what belongs to *ary and what belongs to the optional hash. Since Ruby can't read your mind, the above argument combination (splat + optional) is impossible for it to solve logically.

You either have to rearrange your arguments:

def test(id, h, *a)

In which case h will not be optional. Or then program it manually:

def test(id, *a)
  h = a.last.is_a?(Hash) ? a.pop : nil
  # ^^ Or whatever rule you see as appropriate to determine if h
  # should be assigned a value or not.
Sizemore answered 19/12, 2012 at 21:0 Comment(0)
S
4

In addition to caspers answer:

You may use a splash parameter and check, if the last parameter is a hash. Then you take the hash as settings.

A code example:

def test(id, *ary )
  if ary.last.is_a?(Hash)
    hash_params =   ary.pop
  else
    hash_params =   {}
  end
  # Do stuff here

  puts "#{id}:\t#{ary.inspect}\t#{hash_params.inspect}"
end


test(1, :a, :b )
test(2, :a, :b, :p1 => 1, :p2 => 2 )
test(3, :a,  :p1 => 1, :p2 => 2 )

Result is:

1:  [:a, :b]    {}
2:  [:a, :b]    {:p1=>1, :p2=>2}
3:  [:a]    {:p1=>1, :p2=>2}

This will make problems, if your array-parameter should contain a hash at last position.

test(5, :a,  {:p1 => 1, :p2 => 2} )
test(6, :a,  {:p1 => 1, :p2 => 2}, {} )

Result:

5:  [:a]    {:p1=>1, :p2=>2}
6:  [:a, {:p1=>1, :p2=>2}]  {}
Staford answered 19/12, 2012 at 21:9 Comment(0)
S
2

You could (mis-)use the optional block at the end of the parameter list:

def test(id,*ary, &block)
  if block_given?
    opt_hash = block.call
    p opt_hash
  end
  p id
  p ary
end

test(1,2,3){{:a=>1,:b=>2}}
# output:
# {:a=>1, :b=>2} #Hurray!
# 1
# [2, 3]
Stockton answered 19/12, 2012 at 22:22 Comment(0)
P
1

@Casper is right. Only one of the parameters can have the splat operator. Arguments get assigned to the non-splatted parameters first left-to-right. Remaining arguments get assigned to the splat parameter.

You can do as he suggest. You can also do this:

def test(id,h={},*a)
  # do something with id
  # if not h.empty? then do something with h end
  # if not a.empty? then do something with a end
end

Here are some sample irb runs:

001 > def test (id, h={}, *a)
002?>   puts id.inspect
003?>   puts h.inspect
004?>   puts a.inspect
005?>   end
 => nil 
006 > test(1,2,3,4)
1
2
[3, 4]
 => nil 
007 > test(1,{"a"=>1,"b"=>2},3,4)
1
{"a"=>1, "b"=>2}
[3, 4]
 => nil 
008 > test(1,nil,3,4)
1
nil
[3, 4]
 => nil

Perhaps, I should add. You CAN have an optional parameter as the last parameter but it must be a block/proc.

For example:

def test(a,*b, &c)
  puts a.inspect
  puts b.inspect
  c.call if not c.nil?
end

Here are some sample calls:

006 > test(1,2,3)
1
[2, 3]
 => nil 
007 > test(1,2,3) {puts "hello"}
1
[2, 3]
hello
 => nil 
Poinciana answered 19/12, 2012 at 21:5 Comment(2)
Yeah true. Although h=nil or h={} only makes sense if you're going to call test with test(123), because in all other cases you have to provide the value to h manually when calling test. Ruby won't accept test(123,,somehing,else).Sizemore
Based on his question, test(123) might be a valid call.Poinciana

© 2022 - 2024 — McMap. All rights reserved.