+ Ruby and other programming Languages Ronald L. Ramos.

Slides:



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

Python Objects and Classes
John Pinney Ruby in 20 minutes John Pinney
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
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.
The Ruby Programming Language
Python.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
A TOUR OF RUBY 2011 ACM Class, Dong Xie. What is Ruby?  Dynamic programming language  Complex but expressive grammar  A core class library with rich.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
The Java Programming Language
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CIT 590 Intro to Programming First lecture on Java.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSE 341 Programming Languages Introduction to Ruby Zach Tatlock Spring 2014.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
RUBY by Ryan Chase.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Python Let’s get started!.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
OOP Basics Classes & Methods (c) IDMS/SQL News
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Programming Languages Dan Grossman 2013 Classes and Objects.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Lecture IV Functions & Scope ● Defining Functions ● Returning ● Parameters ● Docstrings ● Functions as Objects ● Scope ● Dealing with Globals ● Nesting.
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)
Scope History of Ruby. Where can you use Ruby? General Features.
Object Oriented Programming in Python: Defining Classes
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP
Python Let’s get started!.
Variables, Environments and Closures
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Creating and Deleting Instances Access to Attributes and Methods
Chapter 3 Introduction to Classes, Objects Methods and Strings
CSE341: Programming Languages Ruby: Blocks & Procs; Inheritance & Overriding Alan Borning Spring 2018.
Variables, Environments and Closures
Substitution in string value
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Ruby Classes.
CISC101 Reminders All assignments are now posted.
Object Oriented Programming in java
Java Programming Language
CSE 3302 Programming Languages
Presentation transcript:

+ Ruby and other programming Languages Ronald L. Ramos

+ Ruby From Other Languages When you first look at some Ruby code, it will likely remind you of other programming languages you’ve used. This is on purpose. Much of the syntax is familiar to users of Perl, Python, and Java (among other languages), so if you’ve used those, learning Ruby will be a piece of cake.

+ Iteration some_list.each do |this_item| # We're inside the block. # deal with this_item. end

+ Everything has a value x = 10 y = 11 z = if x < y true else false end z # => true

+ Symbols are not lightweight Strings :george.object_id == :george.object_id => True "george".object_id == "george".object_id  False

+ Everything is an Object # This is the same as # class MyClass # attr_accessor :instance_var # end MyClass = Class.new do attr_accessor :instance_var end

+ Variable Constants Constants are not really constant. If you modify an already initialized constant, it will trigger a warning, but not halt your program. That isn’t to say you should redefine constants, though.

+ Naming conventions Ruby enforces some naming conventions. If an identifier starts with a capital letter, it is a constant. If it starts with a dollar sign ($), it is a global variable. If it starts it is an instance variable. If it starts with it is a class variable.

+ Keyword arguments def deliver(from: 'A', to: nil, via: 'mail') "Sending from #{from} to #{to} via #{via}." end deliver(to: 'B') # => "Sending from A to B via mail." deliver(via: 'Pony Express', from: 'B', to: 'A') # => "Sending from B to A via Pony Express."

+ The universal truth if 0: print "0 is true" else: print "0 is false" This will print “0 is false”. if 0 puts "0 is true" else puts "0 is false" end Prints “0 is true”. PythonRuby In Ruby, everything except nil and false is considered true.

+ Access modifiers apply until the end of scope class MyClass private def a_method; true; end def another_method; false; end end

+ Method access In Java, public means a method is accessible by anyone. protected means the class’s instances, instances of descendant classes, and instances of classes in the same package can access it, but not anyone else, and private means nobody besides the classes instances can access the method. Ruby differs slightly. public is, naturally, public. private means the method(s) are accessible only when they can be called without an explicit receiver. Only self is allowed to be the receiver of a private method call. Protected is the one to be on the lookout for. A protected method can be called from a class or descendant class instances, but also with another instance as its receiver.

+ Classes are open Ruby classes are open. You can open them up, add to them, and change them at any time. Even core classes, like Fixnum or even Object, the parent of all objects. Ruby on Rails defines a bunch of methods for dealing with time on Fixnum.

+ Funny method names In Ruby, methods are allowed to end with question marks or exclamation marks. By convention, methods that answer questions (i.e. Array#empty? returns true if the receiver is empty) end in question marks.

+ Singleton methods Singleton methods are per-object methods. They are only available on the Object you defined it on.

+ Missing methods Ruby doesn’t give up if it can’t find a method that responds to a particular message. It calls the method_missing method with the name of the method it couldn’t find and the arguments. By default, method_missing raises a NameError exception, but you can redefine it to better fit your application, and many libraries do.

+ Message passing, not function calls A method call is really a message to another object: # This # Is the same as this (2) # Which is the same as this: 1.send "+", 2

+ Blocks are Objects, they just don’t know it yet Blocks (closures, really) are heavily used by the standard library. To call a block, you can either use yield, or make it a Proc by appending a special argument to the argument list, like so: def block( &the_block ) # Inside here, the_block is the block passed to the method the_block # return the block end adder = block { |a, b| a + b } # adder is now a Proc object adder.class # => Proc

+ Operators are syntactic sugar Most operators in Ruby are just syntactic sugar (with some precedence rules) for method calls. You can, for example, override Fixnums + method: class Fixnum # You can, but please don't do this def +( other ) self - other end

+ End of Presentation