Giter Club home page Giter Club logo

ruby_fundamentals2's Introduction

Collections and Methods

One of the main driving forces behind the evolution of computing power is to reliably and safely perform repetitive tasks. Our goal as developers is often to figure out a reliable approach once and apply it to a whole set of different values.

Collections are a fundamental data structure in most programming languages, allowing us to group objects together. Next, we often want to apply some operation or transformation to each item in our collection, which is where methods come in.

Agenda

After this lesson, you will know:

  • How to set up Atom for success
  • Different types of collections you'll come across
  • Operations you can perform on collections
  • The purpose of a method
  • How to define a method
  • The basics of variable scope
  • The importance of inputs and outputs
  • Code style

Atom Overview

  • Setting the language for the current file
  • Setting spaces vs tabs for indentation

Collections

  • A collection is a grouping of objects
  • In Ruby, you can make collections comprised of different types of objects
  • Usually we group things into collections because they are somehow related
  • Performing operations on collections of objects is one area computers provide a lot of benefit
    • In particular, performing the same operation on each item in a collection is where we'll get the most benefit
  • The two main types of collections in Ruby are Arrays and Hashes
  • They are both collections of things that can be counted
    • Something that can be counted is Enumerable
    • We'll revisit this idea with both Arrays and Hashes

Methods

  • A method is a named sequence of instructions
  • Some programming languages refer to it as a function
  • It encapsulates logic for re-use
    • Basically, it allows us to write code once, and reuse it in multiple places
    • A building block toward Don't Repeat Yourself (DRY)
    • Improves maintainability of our code when there's only one point of failure

Variable Scope

  • Local variables are only visible to the other statements at the same level

Inputs & Outputs

  • When creating a method the two things you want to think about at first are:

    • What are the inputs required to do the work inside the method?
    • What do I want to get out of the method?
  • Every method returns something

  • The last line of a method in Ruby will be the return value if you don't otherwise specify the return value explicitly

    • That's referred to as the implicit return value
  • You can use the return keyword to return a value explicitly

  • If you're not careful, your method will return nil

  • That's not a particularly useful result for anyone using your method

Code Style Guide

  • Code must be indented within blocks
  • Spaces around operators (=, +, =>, etc.)
  • No space between method name and parameter list
    • DO: def method(something)
    • DON'T: def method (something)
  • Spaces after commas in lists

ruby_fundamentals2's People

Contributors

danielvallecl avatar

Watchers

James Cloos avatar  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.