Search code examples
ruby-on-railsblacklight

Rails newbie: What does "<<" mean?


I am trying to add advanced search facilities to a Blacklight installation, but I know slim to nil about rails.

I am stuck in a (so far great) tutorial that states:

Turn this feature on by adding to your CatalogController definition:
self.search_params_logic << :add_advanced_parse_q_to_solr

I can find my CatalogController, but I have no clue where to put the "<< :add_advanced_parse_q_to_solr" thingie.

Is it a part of the class definition in the top? As it is now it says:

class CataligController < ApplicationController

Am I supposed to exchange the "< ApplicationController" with "<< :add_advanced_parse_q_to_solr", or should I just append it?

What does the ":" mean, and what does the "<<" mean?

If anyone have any good references to tutorials that can teach me these (I guess) basic syntaxes, please post them here - I would love to understand what I am doing instead of just copy/pasting me my way through!


Solution

  • The added line should appear within your CatalogController definition, so...

    class CatalogController < ApplicationController
      self.search_params_logic << :add_advanced_parse_q_to_solr
    

    The < operation shows class inheritance in the first line. The << operation means add the value on the right as a new element to the array on the left. An equivalent way would be to use the array push method...

    self.search_params_logic.push(:add_advanced_parse_q_to_solr)

    Which brings us to the question about what . means... it simply means you're calling a method that is part of an object or object's class.

    For example

    "Hasse".downcase
    => "hasse"
    

    Strings have a method downcase, and in the above line you're calling that method on the string and the result will be returned.

    self.search_params_logic means you're calling a method on self (in this case, self is the CatalogController so you could have also done CatalogController.search_params_logic but it's not very elegant).

    The search_params_logic returns an array and you can manipulate the array... add or remove elements, for example.