简体   繁体   中英

Rails Model Syntax Confusion

I came across this code in Rails app using mongodb:

  """
 Folder format:
{
  name: <folder name>,
  stocks: [
    {
      name: <stock name>,
      id: <stock id>,
      qty: <stock quantity>
    }
  ]
}
 """

  def format_with_folders(stocks)
   fmap = stock_folder_map
   res = stocks.group_by {|s| fmap[s["id"]] }.collect {|fname, ss|
    {
     "name" => fname,
     "stocks" => ss
    }
  }
  new(folders: res)
 end


   def stock_folder_map
     res = {}
     folders.each { |ff|
       ff.stocks.each { |s|
         res[s["id"]] = ff["name"]
       }
     }
    return res
   end
 end

The doubts are: 1) What does the code inside triple quote signify? Is is a commented code? 2)What would be the right format to use this code inside a ruby script?

First of all, the triple quoted string is often used as a comment, and that is the case here.

To get this to work outside of the class, you would need create a folders method that returns an array of folders in the correct structure. You could do something like this:

Folder = Struct.new(:name, :stocks)

def folders
  [
    Folder.new(
      "Folder 1",
      [
        { "name" => "stock name", "id" => "stock id", "qty" => 3 },
        { "name" => "stock name", "id" => "stock id", "qty" => 5 }
      ]
    ),
    Folder.new(
      "Folder 2",
      [
        { "name" => "stock name", "id" => "stock id", "qty" => 2 },
        { "name" => "stock name", "id" => "stock id", "qty" => 1 }
      ]
    )
  ]
end

def format_with_folders(stocks)
  # ...
end

def stock_folder_map
  # ...
end

The folders method returns an array of Folder objects, which both have a name and stocks attribute. Stocks are an array of hashes.

In Ruby, if you have multiple string literals next to each other, they get concatenated at parse time:

'foo' "bar"
# => 'foobar'

This is a feature inspired by C.

So, what you have there is three string literals next to each other. The first string literal is the empty string:

""

Then comes another string literal:

"
Folder format:
 {
   name: <folder name>,
    stocks: [
     {
      name: <stock name>,
      id: <stock id>,
      qty: <stock quantity>
     }
            ]
 }
"

And lastly, there is a third string literal which is again empty:

""

At parse time, this will be concatenated into a single string literal:

"
Folder format:
 {
   name: <folder name>,
    stocks: [
     {
      name: <stock name>,
      id: <stock id>,
      qty: <stock quantity>
     }
            ]
 }
"

And since this string object isn't referenced by anything, isn't assigned to any variable, isn't returned from any method or block, it will just get immediately garbage collected.

In other words: the entire thing is a no-op, it's dead code. A sufficiently smart Ruby compiler (such as JRuby or Rubinius) will probably completely eliminate it, compile it into nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM