Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby

 Ruby

For non-rubyists

Tadas Sce

March 01, 2014
Tweet

More Decks by Tadas Sce

Other Decks in Technology

Transcript

  1. class Ruby def what? "A dynamic, open source programming language

    " + "with a focus on simplicity and productivity. " + "It has an elegant syntax that is " + "natural to read and easy to write." end end
  2. class Ruby def what_for? { web: ["Rails", "Sinatra", "Padrino"], desktop:

    ["MacRuby", "IronRuby"], mobile: ["RubyMotion", "MobiRuby", "Ruboto"] } end end
  3. # Basics ! puts "Hello VilniusRB!" # => nil #

    >> Hello VilniusRB! ! "Hi".class # => String 1.class # => Fixnum nil.class # => NilClass
  4. # Numbers ! -4 # => -4 1.5 # =>

    1.5 12.3e-3 # => 0.0123 0b101 # => 5 (bin) 0x11 # => 17 (hex) 020 # => 16 (oct) 12_000 # => 12000 10**20 # => 100000000000000000000
  5. # Numbers ! -4.class # => Fixnum 1.5.class # =>

    Float 12.3e-3.class # => Float 0b101.class # => Fixnum 0x11.class # => Fixnum 020.class # => Fixnum 12_000.class # => Fixnum (10**20).class # => Bignum
  6. # Numbers ! 4.even? # => true 3.zero? # =>

    false 3.next # => 4 ! 3.times do print "Hello " end ! # >> Hello Hello Hello
  7. # Strings and symbols ! "foo #{1 + 2}" #

    => "foo 3" 'foo #{1 + 2}' # => "foo \#{1 + 2}" ! :foo # => :foo ! "foo".object_id == "foo".object_id # => false :foo.object_id == :foo.object_id # => true
  8. # Strings and symbols ! "stressed".reverse # => "desserts" "vilnius".capitalize

    # => "Vilnius" "patience".length # => 8 " nu ".strip # => "nu" "Lietuva".start_with? "Liet" # => true :ruby.upcase # => :RUBY
  9. # Strings and symbols ! "I like coffee".sub("coffee", "beer") #

    => "I like beer" ! s = "I really really like ruby" s.split.count("really") # => 2
  10. # Collections ! [1, 2, 3] # Array ! 1..10

    # Range ! { :foo => 11, :bar => 12 } # Hash ! { foo: 11, bar: 12 } # Hash
  11. # Collections ! cities = %w[London Oslo Paris Amsterdam Berlin]

    visited = %w[Berlin Oslo] puts "I still need to visit: ", cities - visited ! # >> I still need to visit: # >> London # >> Paris # >> Amsterdam
  12. # Collections ! (1..10).select(&:even?) # => [2, 4, 6, 8,

    10] ! ["Tom", "Jerry"].join "&" # => "Tom&Jerry" ! [1, 2, 3].shuffle # => [3, 1, 2] ! [1, 1, 2, 1, 2].uniq # => [1, 2] ! [1, 2, 3].include?(2) # => true
  13. # Variables ! monster # is a local variable !

    Monster # is a constant ! @monster # is an instance variable ! @@monster # is a class variable ! $monster # is a global variable
  14. # Conditionals ! if monster.big? puts "AAaah! run!!" elsif monster.medium?

    puts "Be careful" else puts "Don't sweat it" end
  15. # Conditionals ! case monster.size when :big puts "AAaah! run!!"

    when :medium puts "Be careful" else puts "Don't sweat it" end
  16. # Conditionals ! case monster.size when :big then puts "AAaah!

    run!!" when :medium then puts "Be careful" else puts "Don't sweat it" end
  17. # Blocks ! names = %w[Emily Eva Mia] names.each do

    |name| puts "Hi #{name}!" end ! # >> Hi Emily! # >> Hi Eva! # >> Hi Mia!
  18. # Blocks ! names = %w[Emily Eva Mia] names.each {

    |name| puts "Hi #{name}!" } ! ! ! # >> Hi Emily! # >> Hi Eva! # >> Hi Mia!
  19. # Blocks ! def with_time puts "Started at #{Time.now}" yield

    puts "Finished at #{Time.now}" end ! with_time { sleep 2 } ! # >> Started at 2014-03-01 00:50:19 +0200 # >> Finished at 2014-03-01 00:50:21 +0200
  20. module Tail def wiggle puts "wiggling" end end ! class

    Monster include Tail end ! Monster.new("Swamp Donkey").wiggle
  21. # Monkey patching ! 2 + 2 # => 4

    ! class Fixnum def +(x) 42 end end ! 2 + 2 # => 42
  22. # Refinements ! 2 + 2 # => 4 !

    using Evil ! 2 + 2 # => 42
  23. # Meta-programming class Shape attr_reader :dimensions ! def initialize(dimensions =

    {}) @dimensions = dimensions end ! def width dimensions[:width] end ! def height dimensions[:height] end end
  24. # Meta-programming class Shape attr_reader :dimensions ! def initialize(dimensions =

    {}) @dimensions = dimensions end ! [:width, :height].each do |dim| define_method(dim) do dimensions[dim] end end end
  25. # Thank you ! ! { name: "Tadas Ščerbinskas", email:

    "[email protected]", twitter: "tadassce", github: "tadassce" }
  26. # Užduotis ! # Sukurkite Template klasę, turinčią body #

    metodą, kuri grąžins HTML'ą su paduotu # turiniu viduje.