CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*I i = i + 1 end puts "Sum of squares is #{sum}\n" Newline is.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Arrays.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Introduction to the C# Programming Language for the VB Programmer.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Learning Ruby - 7 Methods. def addem ( first, second ) first + second end # of addem. addem( 23, 6 ) def addemall ( first, *rest ) rest.each { |r| first.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
CSE 380 – Computer Game Programming Scripting and Lua Lua.
The Ruby Programming Language
Why to Create a Procedure
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
+ Ruby and other programming Languages Ronald L. Ramos.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Ruby. What is Ruby? Programming Language Object-oriented Interpreted Popular- Ruby on Rails a framework for Web Server Programming.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Methods.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }
CS 142 Lecture Notes: JavascriptSlide 1 Simple Javascript Example sum = 0; for (i = 1; i < 10; i++) { sum += i*i; }
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Royal University of Phnom Penh
Introduction to C++ Systems Programming.
CS 115 Lecture 8 Structured Programming; for loops
Functions.
Lecture 14 Writing Classes part 2 Richard Gesick.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
CHAPTER FOUR Functions.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Python Primer 2: Functions and Control Flow
Substitution in string value
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
More on Functions This lecture provides more ways of using functions
Simple Javascript Example
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
CS561 Computer Architecture Hye Yeon Kim
ITM 352 Functions.
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Presentation transcript:

CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*I i = i + 1 end puts "Sum of squares is #{sum}\n" Newline is statement separator do... end instead of {... } Optional parentheses in method invocation

CS 142 Lecture Notes: RubySlide 2 Variable Names foo Local variable $foo Global Instance variable in object Class variable MAX_USERS Constant (by convention)

CS 142 Lecture Notes: RubySlide 3 Iterators and Blocks [1, 2, 3, 4].each do |x| print x + "\n" end Method on Array object Block: code passed to method

CS 142 Lecture Notes: RubySlide 4 Yield def oddNumbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 end oddNumbers(5) do |i| print(i, "\n") end Invoke method’s block

CS 142 Lecture Notes: RubySlide 5 Factorial def fac(x) if x <= 0 then return 1 end return x*fac(x-1) end

CS 142 Lecture Notes: RubySlide 6 Arguments: Defaults, Variable # def inc(value, amount=1) value+amount end def max(first, *rest) max = first rest.each do |x| if (x > max) then max = x end return max end

CS 142 Lecture Notes: RubySlide 7 Keyword Arguments def create_widget(size, properties)... end create_widget(6, {:name => "Alice", :age => 31}) create_widget(6, :name => "Alice", :age => 31)

CS 142 Lecture Notes: RubySlide 8 Simple Class class Point def initialize(x, = = y end def end def = value end p = Point.new(3,4) puts "p.x is #{p.x}\n" p.x = 44;

CS 142 Lecture Notes: RubySlide 9 Module Example class MyClass include Enumerable... def each... end New methods available in MyClass: min, max, sort, map, select,...

CS 142 Lecture Notes: RubySlide 10