Learning Ruby - 7 Methods. def addem ( first, second ) first + second end # of addem. addem( 23, 6 ) def addemall ( first, *rest ) rest.each { |r| first.

Slides:



Advertisements
Similar presentations
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
Advertisements

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Learning Ruby Ruby Code Blocks. Code blocks in Ruby are not like “blocks of code” in other programming languages Blocks are typically associated with.
Learning Ruby Methods. We’ve seen several so far: puts, gets, chomp, to_s, and even +, -, * and / Usually use dot notation is really just a shortcut.
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Learning Ruby - 12 Grabbag II - The Grabbag Strikes Back!
CMSC 330: Organization of Programming Languages 1 Overview of Ruby.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Bit Manipulation. Get Bit from the particular Position Suppose you want to get a bit from the 4 th position. You can call int get_bit_val(int number_send,int.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Function and Function call Functions name programs Functions can be defined: def myFunction( ): function body (indented) Functions can be called: myFunction(
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Expressions Methods if else Statements Loops Potpourri.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Methods.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
Accumulator Recursion CS125 Spring 2007 Arthur Kantor.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Programming Languages Dan Grossman 2013 Classes and Objects.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Python Fundamentals: If Statements and Functions Eric Shook Department of Geography Kent State University.
Intro to CS Nov 29, 2016.
Topic: Recursion – Part 2
Chapter 5 Functions DDC 2133 Programming II.
Functions CIS 40 – Introduction to Programming in Python
Method.
Agenda Control Flow Statements Purpose test statement
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Important Concepts from Clojure
Important Concepts from Clojure
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Substitution in string value
Subroutines and Functions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Passing Parameters by value
Example 1.
Functions as everyday items
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
functions: argument, return value
CS 1111 Introduction to Programming Spring 2019
Important Concepts from Clojure
Program Flow.
The structure of programming
Fundamental Programming
The structure of programming
Unit-1 Introduction to Java
CSE 190p University of Washington Michael Ernst
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
CISC101 Reminders Assignment 3 due today.
Thinking procedurally
Functions I Creating a programming with small logical units of code.
Location Of The Online Examples
Defining Functions.
Presentation transcript:

Learning Ruby - 7 Methods

def addem ( first, second ) first + second end # of addem. addem( 23, 6 ) def addemall ( first, *rest ) rest.each { |r| first = first + r } first end # of addemall. addemall( 1, 2, 3, 4, 5, 6, 7 ) Ruby Methods are Easy!

def block_sent? ( what ) if block_given? yield( what ) else what end # of if. end # of block_sent? block_sent?( 22 ) block_sent?( 22 ) { |num| num*num } block_sent?( 22 ) { |num| num+num } Yield

def give_back ( a, *b ) return a end # of give_back. give_back( 10 ) give_back( 10, 11 ) def give_back2 ( a, *b ) return a, b.flatten end # of give_back2. give_back2( 10 ) give_back2( 10, 11 ) give_back2( 10, 11, 12, 13 ) first, rest = give_back2( 1, 2, 3, 4, 5 ) Fun with return

More... Ruby So Far Methods can take no, a fixed number or a variable number of parameters as arguments The value of the last statement executed is returned by the method (unless an explicit return is used) Methods are what you use to build classes in Ruby