0

In Ruby 2.1.1 I defined the following method:

def post(url, params={}, send_json: false, success_only: true)

This is called within the same object in another method privateToken like so:

    self.post("#{@url}/api/v3/session", {
        :login    => @user,
        :password => @pass,
    })['private_token']

However, calling privateToken in initialize for this object results in:

<script>:207:in `privateToken': unknown keywords: login, password (ArgumentError)
    from <script>:199:in `initialize'
    from <script>:575:in `new'
    from <script>:575:in `<main>'

If I change the post method to accept params as a keyword argument then this error is avoided:

def post(url, params: {}, send_json: false, success_only: true)

# ..then in method 'privateToken':
self.post("#{@url}/api/v3/session", params: {
   :login    => @user,
   :password => @pass,
})['private_token']

Could someone please explain why this happens? I haven't seen any mention of hash expansion to keywords in the Keyword arguments documentation. And from what I've read the argument ordering should be: standard arguments, default arguments, keyword arguments.

KomodoDave
  • 7,239
  • 10
  • 60
  • 92

1 Answers1

0

Try

def post(url, send_json: false, success_only: true, **params)

see this answer https://stackoverflow.com/a/20633975/1285164

now you can call

self.post("#{@url}/api/v3/session", login: @user, password: @pass)

Community
  • 1
  • 1
bhaity
  • 92
  • 1
  • 8