Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i

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
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
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.
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,
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS346 Javascript -3 Module 3 JavaScript Variables.
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.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
+ Ruby and other programming Languages Ronald L. Ramos.
JavaScript, Fourth Edition
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
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.
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Ruby Getting started & Ruby.New Presented by Mario Tayah
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
CS 371 Web Application Programming
Introduction to C++ Systems Programming.
JavaScript: Functions
Functions CIS 40 – Introduction to Programming in Python
Functions.
Shell scripts on Pegasus 2
Chapter 14 Introduction to Ruby.
CIT 383: Administrative Scripting
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
Introduction to Python
Ruby Testing 2, 11/9/2004.
PHP.
“If you can’t write it down in English, you can’t code it.”
Control Structures Part 3
G. Pullaiah College of Engineering and Technology
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
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.
© Sangeeta M Chauhan, Gwalior
Presentation transcript:

Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i 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

Ruby String Syntax Single quotes (only \' and \\) 'Bill\'s "personal" book' Double quotes (many escape sequences) "Found #{count} errors\nAborting job\n" %q (similar to single quotes) %q<Nesting works: <b>Hello</b>> %Q (similar to double quotes) %Q|She said "#{greeting}"\n| Here documents <<END First line Second line END November 21, 2011 @ExploreNY

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") 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}" 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 140 Lecture Notes: File Systems