Ruby Code explained

前端 未结 3 1661
时光取名叫无心
时光取名叫无心 2020-12-10 18:42

Could somebody please explain this piece of Ruby code:

def add_spec_path_to(args) # :nodoc:
  args << {} unless Hash === args.last
  args.last[:spec_pa         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-10 19:09

    In a little shorter way:

    def add_spec_path_to(args) # :nodoc:
    
    ...
    
    # Append an empty hash to args UNLESS the last arg is a hash.. in which case do nothing
    args << {} unless Hash === args.last # so we need a hash. If it is not there, make an empty one and put it there.
    
    ...
    
    #if args.last[:spec_path] equals nil or false, set it to caller(0)[2]... 
    
    #so inside that hash from the first part, if :spec_path is not there, create it by using caller(0)[2].
    
    args.last[:spec_path] ||= caller(0)[2] 
    
    ...
    
    end
    

提交回复
热议问题