Ad
Code
Diff
  • # Method with three named arguments
    # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    def c one: "one", two: "tow", three: "three"
    p "one: %s two: %s three: %s" % [one,two,three]
    
    end
    
    c  # calling without arguments
    c  two: "TWO2" # calling with one argument
    c  two: "2", one: 1111111 # Calling with 2 arguments with no order
    c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    
    begin
      c  fore: "4" # calling with wrong argument
    rescue Exception => e 
      p e.message
      p e.class
    end
    begin
      #c (fore:"4") # calling with wrong argument
      # this lead to syntaxix error
    rescue Exception => e 
      puts("#{e.message}\n#{e.class}")
    end
    
    hash = {one: 'One', two: 'Two', three: 'Three'}
    c hash # calling with hash
    hash = {two: 'Two', three: 'Three', one: 'One', }
    c hash # calling with hash mess in order
    hash = { one: 'One', }
    c hash # calling with hash where not all argumetns listed
    
    # Super syntax
    hash = { two: '222', }
    c one: 1, **hash 
    # c one: 1, hash  # <== leads to error
    
    
    
    
    begin
      hash = { one: 'One', fore: "4" }
      c hash # calling with hash that contain pair unlisetd in named arguments
    rescue Exception => e 
      puts("#{e.message}\n#{e.class}")
    end
    
    • # Method with three named arguments
    • # https://robots.thoughtbot.com/ruby-2-keyword-arguments
    • def c one: "one", two: "tow", three: "three"
    • p "one: %s two: %s three: %s" % [one,two,three]
    • end
    • c # calling without arguments
    • c two: "TWO2" # calling with one argument
    • c two: "2", one: 1111111 # Calling with 2 arguments with no order
    • c(one: 1, two: 2, three: 3) # Passing 3 Named arguments
    • c(two: 22, three: 333, one: 1 ) # Passing 3 Named arguments (mess the order)
    • begin
    • c fore: "4" # calling with wrong argument
    • rescue Exception => e
    • p e.message
    • p e.class
    • end
    • begin
    • #c (fore:"4") # calling with wrong argument
    • # this lead to syntaxix error
    • rescue Exception => e
    • p e.message
    • p e.class
    • puts("#{e.message}\n#{e.class}")
    • end
    • hash = {one: 'One', two: 'Two', three: 'Three'}
    • c hash # calling with hash
    • hash = {two: 'Two', three: 'Three', one: 'One', }
    • c hash # calling with hash mess in order
    • hash = { one: 'One', }
    • c hash # calling with hash where not all argumetns listed
    • # Super syntax
    • hash = { two: '222', }
    • c one: 1, **hash
    • # c one: 1, hash # <== leads to error
    • begin
    • hash = { one: 'One', fore: "4" }
    • c hash # calling with hash that contain pair unlisetd in named arguments
    • rescue Exception => e
    • p e.message
    • p e.class
    • puts("#{e.message}\n#{e.class}")
    • end