Closures

Sort by

recency

|

20 Discussions

|

  • + 0 comments
    def block_message_printer
        message = "Welcome to Block Message Printer"
        if block_given?
            yield   # Calls the block passed to the method
        end
        puts "But in this function/method message is :: #{message}"
    end
    
    message = gets.chomp
    block_message_printer { puts "This message remembers message :: #{message}" }
    
    #####################################################################################
    
    def proc_message_printer(my_proc)
        message = "Welcome to Proc Message Printer"
        my_proc.call   # Call the Proc
        puts "But in this function/method message is :: #{message}"
    end
    
    my_proc = proc { puts "This message remembers message :: #{message}" }
    proc_message_printer(my_proc)
    
    #####################################################################################    
    
    def lambda_message_printer(my_lambda)
        message = "Welcome to Lambda Message Printer"
        my_lambda.call   # Call the Lambda
        puts "But in this function/method message is :: #{message}"
    end
    
    my_lambda = -> { puts "This message remembers message :: #{message}" }
    lambda_message_printer(my_lambda)
    
  • + 0 comments

    Excellent post! Very informative check Preisemenu.de.

  • + 0 comments

    def block_message_printer message = "Welcome to Block Message Printer" if block_given? yield end puts "But in this function/method message is :: #{message}" end 

def proc_message_printer(my_proc) message = "Welcome to Proc Message Printer" my_proc.() #Call my_proc puts "But in this function/method message is :: #{message}" end 
def lambda_message_printer(my_lambda) message = "Welcome to Lambda Message Printer" my_lambda.call #Call my_lambda puts "But in this function/method message is :: #{message}" end

  • + 0 comments

    Compiled Ruby solutions https://github.com/LinaOrmos/Ruby/tree/main

  • + 0 comments

    Here is Closures problem solution - https://www.gyangav.com/2022/10/hackerrank-ruby-closures-problem-solution.html