The Ruby Programming Language

Slides:



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

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
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.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Your First Java Program: HelloWorld.java
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Access to Names Namespaces, Scopes, Access privileges.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Outline Java program structure Basic program elements
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Style Guidelines. Why do we need style?  Good programming style helps promote the readability, clarity and comprehensibility of your code.
CIT 590 Intro to Programming First lecture on Java.
Chapter 2: Java Fundamentals
Introduction to programming in the Java programming language.
10-Nov-15 Java Object Oriented Programming What is it?
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Copyright Curt Hill Variables What are they? Why do we need them?
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
C++ Lesson 1.
Dept of Computer Science University of Maryland College Park
Working with Java.
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)
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Statements, Comments & Simple Arithmetic
Java Programming with BlueJ
An overview of Java, Data types and variables
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CIT 383: Administrative Scripting
Chapter 1: Computer Systems
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Anatomy of a Java Program
Tonga Institute of Higher Education
Chap 2. Identifiers, Keywords, and Types
Question 1a) What is printed by the following Java program? int s;
Class code for pythonroom.com cchsp2cs
Presentation transcript:

The Ruby Programming Language Carol Wolf Computer Science

Object Orientation Ruby is fully object oriented; everything is an object. Inheritance is shown by ‘<‘ instead of ‘extends’. Java: class Student extends Person Ruby: class Student < Person Modules are used to group classes class Person < ActiveRecord:: Base Modules are like namespaces in html and xml. Access controls are similar to Java: public, protected and private. Each controls everything following it in a class. All variables are accessed by reference.

Variables and Symbols Ruby is weakly typed. Variables receive their types during assignment. There is no boolean type, but everything has a value. False and nil are false and all other objects are true. Instance variables (class variables) begin with the ‘@’ sign. @name, @age, @course Global variables begin with two ‘@’ signs. They are almost never used. Symbols seem to be peculiar to Ruby. They begin with a colon. :name, :age, :course Symbols have a name (string) and value (integer) but no location.

Blocks If a block consists of a single line, it is enclosed in curly braces. Usually blocks begin with a control statement and are terminated with the keyword, ‘end’. Indentation, usually two spaces, is used to indicate what is in the block. Common errors are to have either too few or too many ‘ends’. Variables within a block are local to the block unless they are instance variables starting with the ‘@’ sign. Methods begin with the keyword, ‘def’, and are terminated with an ‘end’. Parameters are enclosed with parentheses. If a method has no parameters, the parentheses are optional.

Example Program – Java public class People { public static void main (String [] args) { Person girl = new Person ("Alice", 5); girl.show_person (); } } // People class Person { String name; int age; Person (String name, int age) { this.name = name; this.age = age; protected void show_person () { System.out.println (name); System.out.println (age); } // Person

Example Program - Ruby class Person attr_accessor :name, :age # initialize is the same as a constructor def initialize (name, age) @name = name @age = age end # puts is the same as println # print is the same as print def show_person puts @name puts @age girl = Person.new("Alice", 5) girl.show_person

Instantiation and Initialization Ruby has girl = Person.new(“Alice”, 5). Java has Person girl = new Person(“Alice”,5); Java comments begin with ‘//’; Ruby’s with ‘#’. In Ruby we can write attr_accessor :name, :age instead of getters and setters. String getName () { } void setName (String name) { }

Data Structures Arrays Hash Tables Indexed with integers starting at 0. Contents do not have to all be the same type. Contents can be assigned in a list using square brackets. order = [“blue”, 6, 24.95] Arrays are objects so must be instantiated with ‘new’. Hash Tables Key – value pairs Keys are almost always symbols Contents can be assigned in a list of key-value pairs using curly braces. order = {:color => “blue”, :size => 6, :price => 24.95} To retrieve an element, use square brackets @size = order[:size]

Control Structures: Conditionals if order[:color] == “blue” … elsif order[:size] == 6 else end

Control Structures: Iteration for, while and until for item in order do puts item Iterator ‘each’ sum = 0 [1..10].each do |count| sum += count end puts sum count is a parameter to the block and has no value outside of it.

Exceptions begin … rescue ensure end rescue and ensure are the same as catch and finally Ruby also has throw and catch, similar to Java

Conventions Class names begin with upper case letters. Method and variable names use lower case. For names with more than one word: Class names use camel (or bumpy) case class ActiveRecord Method and variable names separate words with underscores. def show_person @little_girl In Rails, table names are the plurals of the record names Single record is course Table is called courses But the model class is called Course.

References Dave Thomas, Programming Ruby 1.9, the Pragmatic Progammers’ Guide, 3rd edition, The Pragmatic Programmers, 2009 Sam Ruby, Dave Thomas and David Heinemeier Hannson, Agile Web Development with Rails, 4th edition, 2010, Chapter 4