Time to get off the Rails and learn the core foundation of the Rails Framework, Ruby. Ruby is a clean, flexible and easy to read language. During this session you will learn
basic syntax, OOP and the tools/libs the Ruby Way.
http://www.flickr.com/photos/rrrodrigo/2393323487/ Yukihiro Matsumoto SmallTalk Perl LISP Python Created Ruby in 1995 in Japan. The first english speaking user group was not created until Ruby 1.3 (1999) Thursday, February 28, 13
String "Grumpy wizards make toxic brew" # => Grumpy wizards make toxic brew subject + " make toxic brew" # => Grumpy wizards make toxic brew "#{subject} make toxic brew" # => Grumpy wizards make toxic brew '#{subject} make toxic brew' # => #{subject} make toxic brew content = <One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little END Thursday, February 28, 13
String "Grumpy wizards make toxic brew".length # => 30 "Grumpy wizards make toxic brew".reverse # => "werb cixot ekam sdraziw ypmurG" "Grumpy wizards make toxic brew".upcase # => "GRUMPY WIZARDS MAKE TOXIC BREW" "".methods # => Shows all methods available to String, too many to list ;) Thursday, February 28, 13
Regex content = "Welcome to an intro of PHP. PHP the greatest language EVAR!" content.sub('PHP', 'Ruby') # => "Welcome to an intro of Ruby. PHP the greatest language EVAR!" content.gsub('PHP', 'Ruby') # => "Welcome to an intro of Ruby. Ruby the greatest language EVAR!" content.gsub(/php/i, 'Ruby') # => "Welcome to an intro of Ruby. Ruby the greatest language EVAR!" Thursday, February 28, 13
Conditionals if @post.published? and @post.approved? # Show the post else # DO NOT show post end unless @user.access? # Access Denied end if [email protected]? # Access Denied end if format == 'xml' # Base output on XML elsif format == 'json' # Do some JSON Hotness end @reservation.save if @location.available? redirect_to users_path and return unless @user.save Same Conditional Thursday, February 28, 13
Conditionals if "" puts "Empty String Works" end if 0 puts "Zero Works" end if nil puts "Nil Works" end if false puts "False Works" end Thursday, February 28, 13
Conditionals case ranking when 1..3 puts "You're at an elite level" when 4..10 puts "You made it to the second round!" else puts "Better luck next year!" end Thursday, February 28, 13
Iterations users = [ { name: 'John', username: 'jdoe' }, { name: 'Jane', username: 'jadoe' }, { name: 'Jake', username: 'jsmith' } ] users.each do |user| puts user[:name] end users.each_with_index do |user, index| puts "[#{index}] #{user[:name]}" end user = "Random User Variable" for user in users puts user[:name] end puts user # => {:name=>"Jake", :username=>"jsmith"} User is still defined? Thursday, February 28, 13
Looping 3.times { puts "Hello World" } # => Hello World # => Hello World # => Hello World bottles = 99 while bottles > 0 # take one down pass it... puts bottles.to_s + " bottles of beer on the wall" bottles -= 1 end until file.eof? # Work with line content end Thursday, February 28, 13
class Array def square!(&block) self.each_with_index do |value, index| self[index] = yield(value) end self end end my_numbers = [1,2,3,4] my_numbers.square! do |element| element ** 2 end puts my_numbers.join(', ') # => 1, 4, 9, 16 Thursday, February 28, 13
Creating a class class Asset def initialize(path) @path = path @dimensions = [32, 20] end end image = Asset.new('/images/hello.jpg') Thursday, February 28, 13
Public, Protected, Private class Asset attr_accessor :dimensions, :path def initialize(path) @path = path @dimensions = [32, 20] end def square? width == height end protected def get_type # get class type end end image.get_type # => protected method `get_type' called for #0x007f9ce2085508> (NoMethodError) Thursday, February 28, 13
Inheritance class Image < Asset def get_mime "image/jpeg" end end class Video < Asset def fps 30 end end image = Image.new('/images/new.jpg') puts image.dimensions.join('x') # => 32x20 puts image.get_mime # => “image/jpeg” Thursday, February 28, 13