16
KON’NICHI WA, RUBY An introduction to Ruby with an appearance by Sinatra Monday, September 27, 2010

Kon nichi wa_ruby

Embed Size (px)

Citation preview

Page 1: Kon nichi wa_ruby

KON’NICHI WA, RUBYAn introduction to Ruby with an appearance by Sinatra

Monday, September 27, 2010

Page 2: Kon nichi wa_ruby

Speaker.new({ :name => "Scotty Motte", :twitter => "@spitfiresky", :email => "[email protected]"})

Monday, September 27, 2010

Page 3: Kon nichi wa_ruby

ABOUT RUBY

• Created by Matz - he’s from Japan!

•Written for humans not for computers

• Easy to read and write

• 5.times { puts “Arigato Matz! Have some chunky bacon!”}

• [‘riverside’, ‘japan’, ‘america’].each { |locale| puts locale.capitalize }

• Popularized by Ruby on Rails

Monday, September 27, 2010

Page 4: Kon nichi wa_ruby

INSTALLATION

•Mac Users, I say rejoice, for thou haveth Ruby already!

•Windows users - are you prejudiced towards Japan or something?! Let’s fix that by installing Ruby:

• http://rubyinstaller.org/ - follow the default installation steps and you are good to go. It will install to your programs directory.

Monday, September 27, 2010

Page 5: Kon nichi wa_ruby

IRB IS THE WORD

• irb - Interactive Ruby Console. The best way to tryout and learn Ruby.

• Comes packaged with your Ruby Install

• To Use:

• Mac: open up command line and type: irb

• Windows: open up Interactive Ruby program

Monday, September 27, 2010

Page 6: Kon nichi wa_ruby

IRB EXAMPLES

3.times { puts "da" }

["paris", "france", "napoleon"].each { |name| puts name.capitalize }

def say_hi_to(name) puts "Please to meet you, #{name}!"endsay_hi_to("Mr. President")

cost = 100tax = 8.25total = cost + taxputs total

Monday, September 27, 2010

Page 7: Kon nichi wa_ruby

EVERYTHING IS AN OBJECT

1.class # => Fixnum'a'.class # => String:z.class # => Symbol

class Foo # something hereendFoo.class # => ClassFoo.new.class # => Foo

Monday, September 27, 2010

Page 8: Kon nichi wa_ruby

DYNAMICALLY TYPED

def foo(bar) bar * 2end

foo(1) # => 2foo("a") # => "aa"

Basically, this means you don’t have to define things as integer or string before runtime.

Monday, September 27, 2010

Page 9: Kon nichi wa_ruby

DUCK TYPINGIf it looks like a duck, walks, like a duck, and quacks like a duck, then it’s probably a duck.

"a".respond_to? :size # => true1.respond_to? :+ # => true1.respond_to? :inject # => false

Monday, September 27, 2010

Page 10: Kon nichi wa_ruby

MORE EXAMPLES OF RUBYStringsname = 'Riverside'"Hello, #{name}!"

Methodsstr = "Hello, Riverside"str.size # => 16str.sub("Riverside", "World") # => "Hello, World!"

Numbers1 + 9 # => 10

Arrays[1, 2, 3] # => [1, 2, 3]

Monday, September 27, 2010

Page 11: Kon nichi wa_ruby

MORE EXAMPLES OF RUBY

Hashesoptions = {:name => "Riverside", :state => "CA"}

Iterators[1,2,3].each {|n| puts n} # => 1 2 3

If/Elsesif something == 2 # do somethingelsif something == 1 # do somethingelse # do somthing elseend

Monday, September 27, 2010

Page 12: Kon nichi wa_ruby

ENTER SINATRA

• Sinatra is a web framework built on top of Ruby. It’s kinda like Rails, but much lighter. You use it to build websites with Ruby.

• [sudo] gem install sinatra

• Gems are just a cool word for ‘libraries’ in the Ruby world. They contain packaged up code that do cool things. Rails is a gem, Sinatra is a gem, and nokogiri is a gem. You can find gems at http://rubygems.org

Monday, September 27, 2010

Page 13: Kon nichi wa_ruby

PUT THIS IN YOUR PIPE

Monday, September 27, 2010

Page 14: Kon nichi wa_ruby

AND SMOKE IT

Monday, September 27, 2010

Page 15: Kon nichi wa_ruby

AND BLOW SMOKE RINGS

Monday, September 27, 2010

Page 16: Kon nichi wa_ruby

RESOURCES

• Ruby-Lang Homepage - http://www.ruby-lang.org/en/

• Windows Installer - http://rubyinstaller.org

• Ruby Gems - http://rubygems.org

• Sinatra - http://sinatrarb.com

• E-Text Editor (windows users) - http://www.e-texteditor.com/

• Textmate (mac users) - http://macromates.com/

• Why’s Poignant Guide to Ruby - http://mislav.uniqpath.com/poignant-guide/

Monday, September 27, 2010