John Pinney j.pinney@imperial.ac.uk Ruby in 20 minutes John Pinney j.pinney@imperial.ac.uk.

Slides:



Advertisements
Similar presentations
Introduction to LISP Programming of Pathway Tools Queries and Updates.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Introduction to Programming G51PRG University of Nottingham Revision 1
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Types in Ruby and other languages….  Classes and objects (vs prototypes)  Instance variables/encapsulation  Object creation  Object equality/comparison.
Ruby Reuben Mallaby.
Let's Learn Ruby AQAP As Quickly As Possible!.  Run the interactive ruby shell – irb  Suggest using Netbeans – download ALL Running Ruby Code 2.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction dev.mensfeld.pl github.com/mensfeld.
And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
Ruby The Gem of new programming languages. An interpreted scripting language.
Page 1 Ruby by Tim Hanson & Mamadou Seck. Page 2 Philosophy "I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming,
Scripting Languages CS351 – Programming Paradigms.
Scripting with Ruby What is a scripting language? What is Ruby?
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
The Ruby Programming Language
CMSC 330: Organization of Programming Languages 1 Overview of Ruby.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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.
CMSC 330: Organization of Programming Languages Ruby Hash Tables (and other topics)
General Purpose Language Ruby Kit Chan COMP3351 Programming Languages November 9, 2007.
SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Ruby! Ronald L. Ramos. What is Ruby? Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms,
A very basic overview of Server-Side Scripting Or what is PHP, Perl, Python, Ruby and what can they do for me?
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.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Learning Ruby - 10 Exploiting Classes. Ruby's Class Library Array - an ordered collection of objects Bignum - really big integer values Binding - remembering.
JavaScript Syntax, how to use it in a HTML document
Python Functions.
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.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Introduction to Ruby (adapted from CSE 341, Dan Grossman) 1.
CMSC330 Summer 2009 Kinga Dobolyi Introduction to Ruby.
RUBY by Ryan Chase.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Scripting with Ruby What is a scripting language? What is Ruby?
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
1 Getting Started with Ruby. 2 What’s Ruby? Ruby is an OO, dynamic, agile language –Everything’s an object For example, try puts -1.abs –Derives strengths.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
OOP Basics Classes & Methods (c) IDMS/SQL News
C is a high level language (HLL)
Ruby Tien Ho and Brandon Hostetter. Overview ▸ Object-oriented, functional, imperative, and reflective ▸ Interpreted language ▸ Influenced by Perl, Smalltalk,
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
1 Charles Oliver Nutter Thomas Enebo Tor Norbye Martin Krauskopf JRuby: Understanding the Fuss Except where otherwise noted, the content of this presentation.
6 - Ruby in the Rough 7 - Ruby on Rails 03/03/2006 Taylan Yemliha
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)
Scope History of Ruby. Where can you use Ruby? General Features.
Ruby and other languages….
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 14 Introduction to Ruby.
What to do today: Brief history of Ruby General qualities/info
Ruby Testing 2, 11/9/2004.
Ruby Containers, Iterators, and Blocks
C Programming Language
General Computer Science for Engineers CISC 106 Lecture 03
Code Blocks, Closures, and Continuations
Presentation transcript:

John Pinney j.pinney@imperial.ac.uk Ruby in 20 minutes John Pinney j.pinney@imperial.ac.uk

What is Ruby? A true object-oriented language with easily understandable syntax and convenient notations. => pure / elegant / powerful “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.” Yukihiro Matsumoto (“Matz”)

The Ruby interpreter > irb puts "Hello World" Hello World

(Almost) everything is an object There are no primitives in Ruby. Nearly everything can be considered as an object, i.e. is an instance of a class has an interface for method invocation

"hello". reverse => "olleh” 44. even. => true nil "hello".reverse => "olleh” 44.even? => true nil.class => NilClass nil.class.class => Class

Parts of speech Variables Numbers Strings Symbols Constants Ranges Regular Expressions Methods + arguments Blocks + arguments Arrays Hashes Operators Keywords

Variables local_variable = 3.0 @instance_variable = 4.6 @@class_variable = 8.9 (@ = “attribute”) $global_variable = 2.5

Numbers 1.class => Fixnum 1.0.class => Float

Symbols Symbols (starting with a colon ) are like lightweight strings. They always point to the same object, wherever used in the code. x = :my_symbol

Methods front_door.open front_door.open.close front_door.is_open? front_door.paint(3,:red) front_door.paint(3,:red).dry(30).close

Kernel methods are invoked by their names alone. This is just a bit of syntactic sugar. print "Hello\n" Hello Kernel.print("Hello\n") (print is really a class method of Kernel)

Operators Operators behave as you would expect: 1 + 2 3 But in Ruby they are actually methods! 1.+(2) Since all operators are methods, they can be defined as you like for your own classes, or even re-defined for existing classes.

Blocks Any code surrounded by curly braces is a closure, known as a block: 20.times{ print 'CAG' } CAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAGCAG Blocks are sets of instructions that can be passed around the program.

Blocks can also take arguments, delimited by pipe characters: "Hello".each_char{|ch| puts ch} H e l o

Braces can be replaced by the keywords do and end. "Hello".each_char do |ch| puts ch end H e l o

Effective use of blocks can allow highly transparent code: a = [ "a", "b", "c", "d" ] a.collect { |x| x + "!" } ["a!", "b!", "c!", "d!"]

Regular expressions Regex is very simple to use in Ruby (much nicer than Python!) text = "Cats are smarter than dogs”; if ( text =~ /(C|c)at(.*)/ ) then puts "I found a cat" end I found a cat

Example class Greeter def initialize(name = "World") @name = name end def say_hi puts "Hi #{@name}!" def say_bye puts "Bye #{@name}, come back soon."

g = Greeter. new("Pat") g. say_hi Hi Pat. g g = Greeter.new("Pat") g.say_hi Hi Pat! g.say_bye Bye Pat, come back soon. g.name NoMethodError: undefined method 'name' for #<Greeter:0x007f91b18708f8 @name="Pat">

class Greeter attr_accessor :name end g. name => "Pat" g class Greeter attr_accessor :name end g.name => "Pat" g.name = "John" g.say_hi Hi John!

Sources: https://www. ruby-lang