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.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
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
Scripting with Ruby What is a scripting language? What is Ruby?
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.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Why to Create a Procedure
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
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.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
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
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
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.
Scripting with Ruby What is a scripting language? What is Ruby?
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.
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.
JavaScript: Functions.
11/10/2018.
Chapter 14 Introduction to Ruby.
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
Introduction to Python
CIT 383: Administrative Scripting
Ruby Testing 2, 11/9/2004.
Review for Final Exam.
Perl Functions.
“If you can’t write it down in English, you can’t code it.”
Review for Final Exam.
G. Pullaiah College of Engineering and Technology
Module 2 - Part 1 Variables, Assignment, and Data Types
Ruby Containers, Iterators, and Blocks
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
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.
© Sangeeta M Chauhan, Gwalior
Presentation transcript:

Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i No variable declarations 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 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 CS 142 Lecture Notes: Ruby

Arrays and Hashes x = Array.new x << 10 x[0] = 99 y = ["Alice", 23, 7.3] x[1] = y[1] + y[-1] person = Hash.new person["last_name"] = "Rodriguez" person[:first_name] = "Alice" order = {"item" => "Corn Flakes", "weight" => 18} order = {:item => "Corn Flakes", :weight => 18} order = {item: "Corn Flakes", weight: 18} 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 <= 1 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) result= first for x in rest do if (x > result) then result= x return result 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 odd_numbers(3) do |i| print(i, "\n") end def odd_numbers(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

Iterators are Reusable def sum_odd(count) sum = 0 odd_numbers(count) do |i| sum += i end return sum def odd_numbers(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 142 Lecture Notes: Ruby