Rochester on Rails: Introduction to Ruby

33 %
67 %
Information about Rochester on Rails: Introduction to Ruby

Published on December 27, 2007

Author: jmorrison

Source: slideshare.net

Description

A brief and basic introduction to the syntax of Ruby 1.8, along with a few nice language features. This is a talk I gave at one of our first Rochester on Rails meetings.

Ruby Programmers’ Best Friend Jason Morrison January 19, 2006 Rochester on Rails

History

Ruby Yukihiro “Matz” Matsumoto February 24, 1993

Perl Java Python Ruby PHP 1987 1991 1993 1995

Examples!

5.times { print “Ruby! " }

Ruby! Ruby! Ruby! Ruby! Ruby! 5.times { print “Ruby! " }

[‘one’,‘two’,‘three’].each {|num| puts num.capitalize }

[‘one’,‘two’,‘three’].each {|num| puts num.capitalize } One Two Three

Everything is an object

-21474836480.abs

-21474836480.abs 21474836480

“ Rats live on no evil star”  .reverse.capitalize

“ Rats live on no evil star”  .reverse.capitalize “ Rats live on no evil star”

3.hours.from_now

3.hours.from_now Thu Jan 19 22:05:00 Eastern  Standard Time 2006

Conventions

Variables colored_index_cards

Class Names DromedaryDiner

Symbols :creme_de_menthe

Instance Variables @euros_per_liter

Constants Kilograms_Per_Pound

Syntax

Methods def say_hello(name) result = “Hello, #{name}!” return result end puts say_hello(“world”)

Methods def say_hello(name) “Hello, #{name}!” end puts say_hello(“world”)

Classes class MathWhiz def say_square(value) puts value * value end end sam = MathWhiz.new sam.say_square(5)

Open Classes class Integer def squared self * self end end 5.squared #=> 25

Inheritance class Whopper < Burger @maker = “Burger King” @calories = 0.67 * 10**3 end

Class Methods class FileUtil def self.mkdir(dir) # do it! end end FileUtil.mkdir(“oranges”)

Modules module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end

Modules require &quot;trig&quot; y = Trig.sin(Trig::PI/4) 0.707106780551956

Mixins module Debug def whoAmI? “ #{self.type.name} ” + “ (##{self.id}): ” + “ #{self.to_s}&quot; end end

Mixins class Phonograph include Debug # ... end ph = Phonograph.new(&quot;West End Blues&quot;) ph.whoAmI? &quot;Phonograph (#537766170): West End Blues&quot;

Attributes class PlainOldRubyObject attr_accessor :food, :drinks attr_reader :advice attr_writer :write_only end

Scope class Poet #public by default def poetry end protected def family_legacy end private def hopes_and_dreams end end

Arrays

foo = [] foo << 1 #=> [1] foo << 2 #=> [1, 2] foo << 3 #=> [1, 2, 3]

bar = [1, 2, 3] bar << 4 #=> [1, 2, 3, 4] bar << 5 #=> [1, 2, 3, 4, 5] bar << 6 #=> [1, 2, 3, 4, 5, 6]

folks = %w( Charles Ed Amanda ) #=> [“Charles”, “Ed”, “Amanda”] folks[1] Ed

Hashes

menu = { :douglas_sirk_steak => 17.50, :vanilla_coke => 2.75, :durwood_kirby_burger => 9.75, :five_dollar_shake => 5.00 } menu[:vanilla_coke] 2.75

Flow

if ( score >= 5000 ) puts “You win!” elsif ( score <= 0 ) puts “Game over.” else puts “Current score: #{score}” end

puts “Watch out!” if lion_distance < 5

Blocks

1.upto(5) { |x| puts x } 1 2 3 4 5

5.downto(1) do |time| print “#{time}... ” puts “!” if time <= 3 end 5... 4... 3... ! 2... ! 1... !

Fín!

Homepage www.ruby-lang.org Try Ruby in your browser tryruby.hobix.com Ruby with humor  www.poignantguide.net

#public presentations

Add a comment

Related presentations

Related pages

Rochester on Rails: Introduction to Rails - Technology

1.Jason Morrison January 19, 2006 Rochester on Rails Ruby on Rails Sustainable Productivity for Web Application Development. 2. History. 3. July 2004 David ...
Read more

Ruby on Rails Training: Onsite and Online | Webucator

Ruby on Rails Training: Onsite and Online. ... Introduction to Ruby on Rails. ... Rochester, NY; Syracuse, NY;
Read more

Introduction to Ruby on Rails - Documents - docslide.us

1. Introduction to Ruby on RailsIntroduction to Ruby on Rails Welcome to the puzzle….it’s a fun ride!Welcome to the puzzle….it’s a fun ride! By ...
Read more

Suzanne Duvall - Google Profile

Suzanne Duvall - Software Engineer - University of Rochester - Henrietta, NY - Ruby on Rails developer, ... Introduction. Ruby on Rails developer, ...
Read more

Introduction to Rails - Documents - docslide.us

Introduction to Rails Dominic Mitchell http://happygiraffe.net/blog ... • Why it works • Introduction to Ruby • Practical example . 4. What is ...
Read more

Introduction to Rails 3 - Anup Nivargi - Technology

Introduction to RAILS 3 ; About Me I'm Anup NIVARGI Mangospring, Pune [email_address] @anupnivargi ... Rochester on Rails: Introduction to Ruby
Read more

Rochester on Rails: Introduction to Ruby ...

A brief and basic introduction to the syntax of Ruby 1.8, along with a few nice language features. This is a talk I gave at one of our first Rochester on ...
Read more