Your First Ruby Script

Similar documents
Ruby: Introduction, Basics

Ruby: Introduction, Basics

Ruby: Introduction, Basics

Overview of the Ruby Language. By Ron Haley

15.1 Origins and Uses of Ruby

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

CSCE 120: Learning To Code

CS 5142 Scripting Languages

Alleviate the Apprehension of Coding in Ruby

A Tour of Ruby.... for Java programmers

Programming with Java

CS 231 Data Structures and Algorithms, Fall 2016

Procedures, Parameters, Values and Variables. Steven R. Bagley

Lecture 1. Basic Ruby 1 / 61

Datatypes, Variables, and Operations

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Introduction CMSC 330: Organization of Programming Languages. Books on Ruby. Applications of Scripting Languages

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CSE 341, Autumn 2015, Ruby Introduction Summary

Full file at

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

PHP. Interactive Web Systems

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

WINTER. Web Development. Template. PHP Variables and Constants. Lecture

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

DaMPL. Language Reference Manual. Henrique Grando

CSE : Python Programming

What is Ruby? CSc 372. Comparative Programming Languages. 27 : Ruby Introduction. Department of Computer Science University of Arizona

Java Bytecode (binary file)

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 2: Introduction to C++

Welcome to Python 3. Some history

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Intro to Ruby Scripting

Introduction to the course and basic programming concepts

CS 360 Programming Languages Interpreters

9.2 Linux Essentials Exam Objectives

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

LECTURE 02 INTRODUCTION TO C++

Javascript Arrays, Object & Functions

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Variables, expressions and statements

Programming Paradigms

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Ruby Primer and Review for Developers

Chapter 2 Working with Data Types and Operators

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

CSC Web Programming. Introduction to JavaScript

Ruby on Rails TKK, Otto Hilska

Lecture 3. Miscellaneous Ruby and Testing

CGS 3066: Spring 2015 JavaScript Reference

ENGR 101 Engineering Design Workshop

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Algorithms and Programming I. Lecture#12 Spring 2015

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction to Ruby. SWEN-250 Personal Software Engineering

CIS192: Python Programming

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Using Scala in CS241

Server side basics CS380

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

The current topic: Python. Announcements. Python. Python

Iterators and blocks. Using iterators and blocks Iterate with each or use a for loop? Creating iterators

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

The C++ Language. Arizona State University 1

CSCI 123 Introduction to Programming Concepts in C++

S206E Lecture 19, 5/24/2016, Python an overview

CIT 590 Homework 5 HTML Resumes

Python Boot Camp. Day 3

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

PHP by Pearson Education, Inc. All Rights Reserved.

CSE 142/143 Unofficial Style Guide

At full speed with Python

Chapter 2 Basic Elements of C++

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Chapter 2: Functions and Control Structures

Sprite an animation manipulation language Language Reference Manual

RUBY VARIABLES, CONSTANTS AND LITERALS

Introduction to Python Part 2

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Computer Programming : C++

Hello, World and Variables

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

Python Programming Exercises 1

Basic Syntax - First Program 1

CMSC 201 Computer Science I for Majors

Python Unit

Getting Started Values, Expressions, and Statements CS GMU

Transcription:

Learn Ruby in 50 pages Your First Ruby Script Step-By-Step Martin Miliauskas @mmiliauskas 1

Your First Ruby Script, Step-By-Step By Martin Miliauskas Published in 2013 by Martin Miliauskas On the web: www.rubyebook.com Please send errors to martin.miliauskas@gmail.com Copyright 2013 by Martin Miliauskas. All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopy, recording or any information storage and retrieval system, without prior permission in writing from the publisher. 2

Table of Contents Introduction! 4 Warming Up! 5 Step 1. Basic Spider! 14 Step 2. Getting Smarter! 17 Step 3. Adding Guard! 19 Step 4. Scraping Abstract! 22 Step 5. More Scraping! 24 Step 6. Refactoring with OOP! 26 Step 7. Accessing Attributes! 30 Step 8. Verbose CLI! 33 Step 9. Fixing Bad Practice! 36 Step 10. Optimizing Keys! 39 Step 11. Adding Filters for Links! 41 Step 12. Custom Filters! 45 Step 13. Namespaces! 48 Step 14. Decoupling! 50 Finish! 54 3

Introduction This book s intention is to teach you Ruby programming language by writing a real world script, step-by-step. Every step will introduce a new Ruby concept written in a concise, all you need to know manner. By the end of this book you will be able to read and write intermediate level Ruby scripts. What we will build? During the course of this book, we will build a spider script capable of scraping Wikipedia.org pages. What Ruby concepts will we cover? Datatypes and Objects Methods Classes and Modules Code Blocks Dealing with External Libraries (aka gems) What you need to have? Knowledge of at least one programming language; Ruby installed. The following guide should suffice. 4

Warming Up A very useful tool which comes bundled with Ruby is IRB, an interactive command-line interpreter which lets you execute and see the results of your Ruby code immediately. In this chapter we will test Ruby concepts and play with its syntax using exactly this tool. So without further ado, let's start IRB by executing irb command in the terminal: 5

Everything is an object In Ruby everything is an object, even literals: >> "alice".capitalize => "Alice" >> 5.next => 6 >> false.class => FalseClass >> [5, 12, 4].sort => [4, 5, 12] Math Basics >> 3 + 2 => 5 >> 5 * 2 => 10 Power >> 2 ** 8 => 256 Division If all numbers in the expression are integers (objects of Fixnum), the result is an integer as well: >> 8 / 3 => 2 You can think of the above expression as 5 div 6 (if you are familiar with Turbo Pascal). In case you need more precise result, simply turn one of the numbers into a floating point number: >> 8 / 3.0 => 2.66666666666667 >> 8 / 3.to_f => 2.66666666666667 6

Data structures Two main types of collections that are used in Ruby are: arrays and hashes (associative arrays). Arrays and hashes are mutable. A common way to define an array is using [...] literal, and {...} to define a hash: >> [ 4, 5, [21, 'hello'] ] => [4, 5, [21, "hello"]] >> {'a'=>'alice', 2 => 'John' } => {"a"=>"alice", 2=>"John"} We will play more with these data structures in further chapters. Methods In order to have a re-usable code, we need to store it inside the method. Method definition starts with def and ends with end: >> def add a, b >> a + b => nil >> add 1, 5 => 6 As you can see, brackets around parameters in a method definition and method call are optional. return statement is also optional, and by default the result of the last expression is returned. Chaining methods Another cool thing that Ruby allows is method chaining. You can chain methods as long as the object returned by the previous method call has the subsequent method, i.e.: >> add(5,6).next.next => 13 add(5,6) returns an object of Fixnum, which has next method, which in turn also returns an object of type Fixnum. 7

Code blocks Code blocks are like anonymous functions, which can be attached to a method call. For example, you can attach a code block to Array#each method, which then passes every element from the array to it: >> [2, 15, 42].each { x puts x} 2 15 42 => [2, 15, 42] Code between {...} is used for defining a code block, the parameters of which are defined inside of the.... Code blocks which span over multiple lines can also be defined using def... end: >> [2, 15, 42].each do x?>! x = x * 2 >>! puts x 4 30 84 => [2, 15, 42] Iterating A common use of code blocks is to iterate over arrays and hashes: >> [2, 15, 42].each_with_index do x, index?>! puts "#{index}=>#{x}" 0=>2 1=>15 2=>42 => [2, 15, 42] >> u = {'a'=>'alice', 'j'=>'john'} => {"a"=>"alice", "j"=>"john"} >> u.each_pair do key, value?>! puts "#{key}=>{value}" a=>{value} j=>{value} => {"a"=>"alice", "j"=>"john"} 8

Simple counter loops can be achieved using methods provided by integers: >> 3.times { counter puts counter} 0 1 2 => 3 >> 1.upto(3) { counter puts counter} 1 2 3 => 1 >> 0.downto(-3) { counter puts counter} 0-1 -2-3 => 0 Flow control In order to break or skip iteration, use break and next respectively, i.e.: >> [2, 15, 42, 68, 111].each do x?>! if x == 15 >>!! next >>! end >>! if x > 50 >>!! break >>! end >>! puts x 2 42 => nil Variables Global variables are available anywhere in you script and are prefixed with a dollar ($) sign: >> $foo = "bar" => "bar 9

Local Local variables start with a lowercase letter or an underscore. Default notation is to use snake_case for your variable names: >> foo_bar = 10 => 10 Local variables are only available in their scope: if declared in a method, or code block (since Ruby 1.9) - they won t be available outside. >> 1.times do x?> y = x + 10 >>! puts y >>! puts x 10 0 => 1 >> puts y NameError: undefined local variable or method y' for main:object... >> puts x NameError: undefined local variable or method x' for main:object... However, variables defined outside the code block are available inside it: >> foo = "bar" => "bar" >> 1.times do?>! puts foo bar Constants Variables where the first letter is uppercase are treated as constants. By convention, constants have all letters in the uppercase and use underscore to separate words: >> FOO_BAR = 10 => 10 The interesting thing about constants is that they are not final; you can assign a new value to same constant. Ruby will give you a warning but will change the value anyway: 10

>> FOO_BAR = 20 (irb):31: warning: already initialized constant FOO_BAR => 20 >> FOO_BAR => 20 Ruby treats its developers as an adults, thus it does not impose restrictions that can be found in other languages (i.e. Java or Python). Constants case is a good example of this; you are allowed to change their value, but Ruby trusts you to do it only when it is really necessary. Strings Strings usually initialized by wrapping them with double ( ) or single ( ) quotes: >> greet = "Hello" => "Hello" >> to = 'World' => "World" Using double quotes allows it to perform string interpolation - placing Ruby code into a string: >> "1 + 2 = #{1+2}" => "1 + 2 = 3" It is common technique to use interpolation to concatenate strings: >> "#{greet}, #{to}!" => "Hello, World!" Outputting Two common ways to output something from the Ruby script, is by using puts or p. puts puts calls obj.to_s and outputs return value to output stream. to_s method returns string representation of an object value: >> puts ['hello', 'world'] hello world 11

>> f = File.new '/tmp/test_file', 'r' => #<File:/tmp/test_file> >> puts f #<File:0x007fb4d188a5d0> p p is same as puts obj.inspect. inspect returns human readable version of an object, thus it is useful for debugging: >> p ['hello', 'world'] ["hello", "world"] >> f = File.new '/tmp/test_file', 'r' => #<File:/tmp/test_file> >> p f #<File:/tmp/test_file> Classes The final exercise in our warm up is defining a Class: >> class Greeter >> def initialize >> @greet = '' >>?> def say_hello >> @greet = 'Hello' >> self >>?> def to name >> puts "#{@greet}, #{name}!" => nil >> g = Greeter.new => #<Greeter:0x007f892a101390 @greet=""> >> g.say_hello.to "Alice" Hello, Alice! => nil 1. Class definition starts with class keyword and ends with end; 2. Greeter the name of the class must always start with a capital letter; 3. initialize - a constructor method; 12

4. By prefixing a variable with @ you turn it into an instance variable, which is available anywhere in the class scope; 5. self - same as $this in PHP or this in Java - represents instance of the current class; 6. Greeter.new - creates a new instance of Greeter class; 7. g.say_hello.to - Greeter.say_hello can be chained with Greeter.to method, because Greeter.say_hello returns self. Conclusion In this short chapter we had a crash course on Ruby syntax. We played with: different data types; defined variables, methods, classes; and iterated with code blocks - good start! But it was just the beginning of the fun. In the next chapters we will build an actual spider script, step-by-step. With every change of our code we will learn new Ruby concepts. 13

Step 1. Basic Spider We are going to begin by writing a short script which would output source code of the V for Vendetta movie Wikipedia page: Full code require 'open-uri' url = 'http://en.wikipedia.org/wiki/v_for_vendetta' source = open(url).read puts source Place above code into spider.rb file and execute it with ruby command, as shown below: Things to note about the code that we wrote: 1. require 'open-uri' - loads standard Ruby library called OpenURI, which helps deal with http, https and ftp; 2. open(url).read - open(url) gets content from a remote file, which address is url, stores it inside the temporary file and returns File object representing it. read returns the contents of the File object as a string. 14

External libraries Libraries in the Ruby world are called gems and are managed by Rubygems package manager. Installing a gem $ gem install rspec Listing all installed gems $ gem list Using gem in your script In order to use gems in your script, you need to require them first, i.e.: require 'rspec' Where are my gems stored? The directory where Ruby gems are installed is in a list of Ruby s search paths, thus you don t need to specify an absolute path to your installed gems. If you are eager to know where your gems are stored, execute gem env command, and look for GEM PATHS: $ gem env... - RUBYGEMS PLATFORMS: - ruby - x86_64-darwin-11 - GEM PATHS: - /Users/mmiliauskass/.rvm/gems/ruby-1.9.2-p320 - /Users/mmiliauskass/.rvm/gems/ruby-1.9.2- p320@global 15

Did you like first two chapters? Download full version here. 16