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.
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.
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 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
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.
Squirrel Programming Language By Nandini Bhatta CS537 Summer 2008.
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?
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
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.
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
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
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,
Trends in Scripting Languages History For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Functions Reusable Parts of Code SoftUni Team Technical Trainers Software University
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 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.
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.
Chapter 6 Methods Chapter 6 - Methods.
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.
CS314 – Section 5 Recitation 10
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
Functions.
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
Substitution in string value
Introduction to Python
Ruby Testing 2, 11/9/2004.
“If you can’t write it down in English, you can’t code it.”
G. Pullaiah College of Engineering and Technology
Ruby Containers, Iterators, and Blocks
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Corresponds with Chapter 5
 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.
Programming II Vocabulary Review Loops & functions
© Sangeeta M Chauhan, Gwalior
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 Substitution in string value No variable declarations

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

● 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 Hello > ● %Q (similar to double quotes) %Q|She said "#{greeting}"\n| ● “Here documents” <<END First line Second line END CS 142 Lecture Notes: RubySlide 3 Ruby String Syntax

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} CS 142 Lecture Notes: RubySlide 4 Arrays and Hashes

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

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

CS 142 Lecture Notes: RubySlide 7 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 end return result end

CS 142 Lecture Notes: RubySlide 8 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: RubySlide 9 Blocks, Iterators, Yield Invoke method’s block Block: code passed to method 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 end Iterator

CS 142 Lecture Notes: RubySlide 10 Iterators are Reusable def sum_odd(count) sum = 0 odd_numbers(count) do |i| sum += i end return sum end def odd_numbers(count) number = 1 while count > 0 do yield(number) number += 2 count -= 1 end

CS 142 Lecture Notes: RubySlide 11 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| print(value, "\n") end

CS 142 Lecture Notes: Ruby Slide 12 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}" p.x = 44

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