Substitution in string value

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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
And other languages….  Selection Statements  Iterative Statements  Unconditional Branching  Not covered Guarded Commands.
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.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Introduction to C++ Systems Programming.
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
Introduction to Ruby CS 480/680 – Comparative Languages.
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.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
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,
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.
JavaScript, Fourth Edition
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
CMSC 330: Organization of Programming Languages Functional Programming with OCaml.
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.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
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.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
CS314 – Section 5 Recitation 10
Royal University of Phnom Penh
Examples of Classes & Objects
Introduction to C++ Systems Programming.
Principles of programming languages 4: Parameter passing, Scope rules
Modern JavaScript Develop And Design
Functions.
JavaScript: Functions.
Lecture 14 Writing Classes part 2 Richard Gesick.
CIT 383: Administrative Scripting
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
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
More on Functions This lecture provides more ways of using functions
Ruby Testing 2, 11/9/2004.
Perl Functions.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Microsoft Excel – Part I
Control Structures Part 3
CISC101 Reminders All assignments are now posted.
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
CSC215 Lecture Control Flow.
CSE 3302 Programming Languages
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.
Week 6 - Monday CS221.
Presentation transcript:

Substitution in string value 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 Substitution in string value CS 142 Lecture Notes: Ruby

Variable Names and Scopes foo Local variable $foo Global variable @foo Instance variable in object @@foo Class variable MAX_USERS Constant (by convention) CS 142 Lecture Notes: Ruby

Ruby Statements if x < 10 then ... elsif x < 20 else end while x < 10 do array = [14, 22, 34, 46, 92] for value in array do CS 142 Lecture Notes: Ruby

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

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

Keyword Arguments def create_widget(size, properties) ... end create_widget(6, {:id => "table22", :class => "Cart"}) create_widget(6, :id => "table22", :class => "Cart") CS 142 Lecture Notes: Ruby

Blocks, Iterators, Yield oddNumbers(3) do |i| print(i, "\n") end def oddNumbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 Block: code passed to method Invoke method’s block CS 142 Lecture Notes: Ruby

Another Block/Iterator Example def sumOdd(count) sum = 0 oddNumbers(count) do |i| sum += i end return sum def oddNumbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 CS 142 Lecture Notes: Ruby

Equivalent Code array = [14, 22, 34, 46, 92] for value in array do print(value, "\n") end array = [14, 22, 34, 46, 92]; array.each do |value| CS 142 Lecture Notes: Ruby

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

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

CS 142 Lecture Notes: Ruby