Giter Site home page Giter Site logo

splat-readme's Introduction

The Splat Operator

Objectives

Understand what the splat operator is and how it is used.

What is the Splat Operator?

The * (or splat) operator allows a method to take an arbitrary number of arguments and is perfect for situations when you would not know in advance how many arguments will be passed in to a method. Here's an example:

def name_greeting(*names)
  names.each do |name|
    puts "Hello, #{name}!"
  end
end

name_greeting("Happy", "Sleepy", "Dopey", "Bashful")
Hello, Happy!
Hello, Sleepy!
Hello, Dopey!
Hello, Bashful!
=> ["Happy", "Sleepy", "Dopey", "Bashful"]

How does it work?

The splat operater, placed before a parameter in a method signature (as above) tells the method to accept an unlimited number of arguments as an array. Let's take a look. We're going to play around with our name_greeting method in IRB using Pry. (Yes, you can use Pry inside IRB. Yes, it is awesome.)

  • Drop into IRB in your terminal and type the following command require 'pry'. You should see a return value of => true. That let's you know that you are able to use Pry.
  • Copy and pase the following method, with the binding.pry in it:
def name_greeting(*names)
  binding.pry
  names.each do |name|
    puts "Hello, #{name}!"
  end
end
  • Then, call the method with multiple arguments: name_greeting("Sally", "Bobby"). You'll be dropped inside the method via your binding.pry. Type names into your terminal and you should see the following:
=> ["Sally", "Bobby"]

The * has converted your multiple arguments into an array!

Now, let's try the same method call but with only one argument.

  • Exit out of your binding by typing exit in your terminal. Then, call the method again but this time with only argument: name_greeting("Sally"). Once again, you'll be dropped into the method via your binding.pry. Type names into your terminal and you should see the following:
=> ["Sally"]

The * still converts the argument into an array, but this time the array has only one element.

What would happen if we tried to call our method without any arguments at all? Let's find out!

  • Exit out of your binding and call the method again, this time without any arguments: name_greeting. You'll be dropped into your method via the binding. Type names into the terminal and you should see the following:
=> []

Instead of an ArgumentError, the lack of arguments has simply been converted into an empty array, thanks to the * operator.

A Note on Usage

You can define a method that takes in a required argument and, separately, an undefined number of additional arguments via the splat operator. Let's take a look:

def number_of_greetings(num, *names)
  num.times do 
    names.each do |name|
      puts "Hello, #{name}!"
    end
  end
end

We've defined a method that takes in a required argument of a number and an unlimited number of names. The method uses a times loop to puts out a greeting to each person, however many times the method has been told to via the num argument. Let's call the method:

number_of_greetings(5, "Sally", "Bobby")

#outputs:

Hello, Sally!
Hello, Bobby!
Hello, Sally!
Hello, Bobby!
Hello, Sally!
Hello, Bobby!
Hello, Sally!
Hello, Bobby!
Hello, Sally!
Hello, Bobby!

It is important to note that when defining a method to accept a required argument and splat arguments, you must define the method to first accept the required argument and then accept the splat arguments.

Resources

splat-readme's People

Contributors

sophiedebenedetto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.