dom lizarraga

    dominiclizarraga@hotmail.com

    Decompose for method calls

    2 minutes to read

    In Ruby, it is possible to decompose the elements of arrays/hashes into distinct variables. Since values appear within arrays in a index order, they are unpacked into variables in the same order.

    # Decompose of an array
    >> fruits = ["apple", "banana", "cherry"]
    >> x, y, z = fruits
    >> x
    => "apple"
    

    In this case we are going to use decompose for a method call, (with arrays only one splat needed *).

    def send_welcome_email(name, email, signup_date)
      puts "Sending welcome email to: #{name}"
      puts "Email: #{email}"
      puts "Signed up on: #{signup_date}"
    end
    
    # Array of user information
    user_data = ['Jane Doe', 'jane.doe@example.com', '2023-04-01']
    
    # Using splat to pass an array of elements
    send_welcome_email(*user_data) 👈
    
    # This code will output:
    Sending welcome email to: Jane Doe
    Email: jane.doe@example.com
    Signed up on: 2023-04-01
    

    Now we are going to decompose for a hash, (two splats needed **).

    def configure_user(profile:, preferences:, settings:)
      puts "Configuring user profile:"
      puts "Profile: #{profile}"
      puts "Preferences: #{preferences}"
      puts "Settings: #{settings}"
    end
    
    # Hash of user configuration
    user_configuration = {
      profile: { username: 'johndoe', language: 'EN' },
      preferences: { theme: 'dark', notifications: true },
      settings: { privacy: 'high', location: 'off' }
    }
    
    # Using 2 splats to pass a hash
    configure_user(**user_configuration) 👈
    
    # This code will output:
    Configuring user profile:
    Profile: {:username=>"johndoe", :language=>"EN"}
    Preferences: {:theme=>"dark", :notifications=>true}
    Settings: {:privacy=>"high", :location=>"off"}