View
2.401
Download
2
Category
Tags:
Preview:
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.
Citation preview
RubyProgrammers’ Best Friend
Jason Morrison
January 19, 2006
Rochester on Rails
History
RubyYukihiro “Matz” Matsumoto
February 24, 1993
Perl Java Python Ruby PHP1987 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}
OneTwoThree
Everything isan object
-21474836480.abs
-21474836480.abs21474836480
“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 resultend
puts say_hello(“world”)
Methods
def say_hello(name) “Hello, #{name}!”end
puts say_hello(“world”)
Classesclass MathWhiz
def say_square(value) puts value * value
end
end
sam = MathWhiz.new
sam.say_square(5)
Open Classesclass Integer
def squared
self * self
end
end
5.squared #=> 25
Inheritance
class Whopper < Burger @maker = “Burger King” @calories = 0.67 * 10**3end
Class Methods
class FileUtil def self.mkdir(dir) # do it! endend
FileUtil.mkdir(“oranges”)
Modules
module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. endend
Modules
require "trig"y = Trig.sin(Trig::PI/4)0.707106780551956
Mixins
module Debug def whoAmI? “#{self.type.name} ” + “(\##{self.id}): ” + “#{self.to_s}" endend
Mixins
class Phonograph include Debug # ...endph = Phonograph.new("West End Blues")ph.whoAmI?"Phonograph (#537766170): West End Blues"
Attributes
class PlainOldRubyObject attr_accessor :food, :drinks attr_reader :advice attr_writer :write_onlyend
Scopeclass Poet #public by default def poetry end
protected def family_legacy end
private def hopes_and_dreams endend
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 }
12345
5.downto(1) do |time| print “#{time}... ” puts “!” if time <= 3end
5... 4... 3... !2... !1... !
Fín!
Homepagewww.ruby-lang.org
Try Ruby in your browsertryruby.hobix.com
Ruby with humor www.poignantguide.net
Recommended