Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Ruby passing symbol(s) as keyword argument(s)?

I have a method, where I want to use keyword parameter(s) and case/when statements, where I prefered to use symbols:

  def navigate_to(page:)
    case page
    when :inbox
      # some code
    when :archive
      # some code
    else
      # some code
    end
  end

Is this the correct syntax when calling the method?

navigate_to page: :inbox

The : : seems a little bit strange.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The keyword argument syntax is:

key: value

If key is page and value is the symbol :inbox, you indeed have:

page: :inbox

You could also use a positional argument instead:

def navigate_to(page)
  # same as your code
end

navigate_to :inbox
over 4 years ago · Santiago Trujillo Relatório

0

Yes, the correct syntax is navigate_to page: :inbox.

While this is common Ruby, it is short and equivalent for several different things. First, the braces.

You are actually calling:

navigate_to(page: :inbox)

Second, the keyword argument pattern originates from hashes as arguments. Before there were keyword arguments, a common way would be to pass in a hash[1], like so:

def navigate_to(options)
  page = options[:page]
end
navigate_to({ page: :inbox })

But when last argument in a method call is a hash, one can leave out the {}.

And last, the actual keys in the hash. A while ago (1.8 -> 1.9 IIRC) a short version was introduced for the following:

{ :page => 'Some Value' }, namely { page: 'Some Value' }. When Some Value is a symbol, that becomes { page: :inbox }.

So, taking all that:

navigate_to page: :inbox

Orignates from:

navigate_to({ :page => :inbox })

It might make more sense reading it like this, or knowing it comes from that.

And I know Ruby, nor ruby-ist, like braces, (), as can be seen in the mindboggling DSL of for example rspec, but I can advise especially new developers, to add them. It often makes code better understandable.

navigate_to(page: :inbox) is probably easier to understand than navigate_to page: :inbox, especially when you start calling through other methods: navigate_to page page_from_session :user.


[1] But, to stress, that is not really what is happening here. Keyword arguments and hash arguments do differ, now that we have keyword arguments. this just shows why the syntax is this way.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda