0

I need to know the correct way to type this: environment: :sandbox or environment::sandbox?

Edit: I saw your comments, maybe if I type in the code that I used, I will get a better answer?

require_relative 'lib/gocardless_pro'

    @client = GoCardlessPro::Client.new(
        access_token: ENV['GOCARDLESS_TOKEN'],
        # environment: :sandbox or environment::sandbox?
    )
tt2806
  • 21
  • 4

3 Answers3

1

Most likely it is environment: :sandbox, where you are defining a hash key environment to hold the value :sandbox, which is a symbolized string.

Your confusion probably comes from the fact that Module::Class is valid syntax, but your example in lowercase is not valid syntax, and I am guessing from the context of your question you are not attempting to use a namespace separator.

anothermh
  • 9,815
  • 3
  • 33
  • 52
1

Both are correct, but only the second one is a generally valid expression. The first one is only valid in three limited contexts where it means three to four different things.

The first one is an alternative way of writing the message send operator.

The second one can mean:

  • In a Hash literal: a key-value pair with key :environment and value :sandbox.
  • In a parameter list: an optional keyword parameter environment with default argument :sandbox.
  • In an argument list: a keyword argument environment with value :sandbox, or
    • at the very end of an argument list: a key-value pair with key :environment and value :sandbox that is part of a Hash value bound to the last positional argument.

However, since you actually have neither a Hash literal nor a parameter list nor an argument list in your code, it is simply a SyntaxError.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0
    @client = GoCardlessPro::Client.new(
        access_token: ENV['GOCARDLESS_TOKEN'],
        # environment: :sandbox or environment::sandbox?
    )

In this context you're passing in keyword arguments to a method. This is, basically, a Hash where the key is a Symbol and the value is whatever you like. So it would be environment: :sandbox.

    @client = GoCardlessPro::Client.new(
        access_token: ENV['GOCARDLESS_TOKEN'],
        environment: :sandbox
    )

That passes the argument :environment with the value :sandbox.

:access_token, :environment, and :sandbox are Symbols, sort of read-only strings that also use less memory.

environment: :sandbox is shorthand for the more conventional syntax :key => :value. You can replace :key => with simply key:.

Schwern
  • 153,029
  • 25
  • 195
  • 336