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

Ruby 101

Ruby 101

Basic ruby introduction for Railsgirls İstanbul 2018

Uğur Özyılmazel

November 24, 2018
Tweet

More Decks by Uğur Özyılmazel

Other Decks in Programming

Transcript

  1. şehirler = %w[İstanbul Ankara Viyana Paris] gittiğim_şehirler = %w[İstanbul Viyana]

    puts "Gitmem gereken şehirler", şehirler - gittiğim_şehirler Gitmem gereken şehirler Ankara Paris
  2. 5.class # => Integer 5.class.superclass # => Numeric 5.class.superclass.superclass #

    => Object 5.class.superclass.superclass.superclass # => BasicObject 5.class.superclass.superclass.superclass.superclass # => nil
  3. 5.methods # => [:- @, :**, :<=>, :upto, :<<, :<=,

    :>=, :==, :chr, :===, :>>, :[], : %, :&, :inspect, : +, :ord, :-, :/, :*, :size, :succ, :<, :>, :to_int, :coerce, :divmod, :to_ s, :to_i, :fdiv, :modulo, :remainder, :abs, :magnitude, :lcm, :integer?, : rationalize, :gcdlcm, :gcd, :denominator, :floor, :ceil, :round, :truncate , :to_r, :to_f, :^, :odd?, :even?, :allbits?, :anybits?, :nobits?, :downto , :times, :pred, :pow, :bit_length, :digits, :numerator, :next, :div, :|, :~, : +@, :conjugate, :eql?, :singleton_method_added, :i, :real?, :zero?, :nonze ro?, :finite?, :infinite?, :step, :positive?, :negative?, :angle, :real, : to_c, :conj, :phase, :imag, :abs2, :arg, :imaginary, :rectangular, :rect, :clone, :dup, :polar, :quo, :between?, :clamp, :instance_variable_set, :in stance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_ of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_vari ables, :method, :public_method, :define_singleton_method, :singleton_metho d, :public_send, :extend, :pp, :to_enum, :enum_for, :=~, :! ~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :hash, :clas s, :singleton_class, :yield_self, :itself, :tainted?, :taint, :untrust, :u ntaint, :trust, :untrusted?, :methods, :frozen?, :singleton_methods, :prot ected_methods, :private_methods, :!, :equal?, :instance_eval, :instance_ex ec, :!=, :__id__, :__send__]
  4. class Integer def kere(n) self * n end end 5.kere(5)

    # => 25 5.kere(5).kere(2) # => 50
  5. class Integer def gün self * 24 * 60 *

    60 end def önce Time.now - self end def sonra Time.now + self end end Time.now # => 2018-11-23 14:42:23 +0300 5.gün.önce # => 2018-11-18 14:42:23 +0300 1.gün.sonra # => 2018-11-24 14:42:23 +0300
  6. 5.times do |i| puts "Ruby’i seviyorum, i = #{i}" if

    i > 2 end # Ruby’i seviyorum, i = 3 # Ruby’i seviyorum, i = 4
  7. meals = %w[Pizza Döner Kebab] print "Let’s eat here!" unless

    meals.include? "Soup" menü'de çorba yoksa yemeğİ burada YİYELİM!
  8. varıable merhaba = "Dünya" # Değişken @merhaba # Instance Variable

    @@merhaba # Class Variable $merhaba # Global Variable MERHABA # Constant
  9. ARRAY [] # => [] [1, "Merhaba", 2] # =>

    [1, "Merhaba", 2] [[1, 2], ["Merhaba", "Dünya"]] # => [[1, 2], ["Merhaba", "Dünya"]]
  10. hash {} # => {} {:foo => "bar"} # =>

    {:foo=>"bar"} # Eski {foo: "bar"} # => {:foo=>"bar"} # Yeni
  11. ÜNLEM İŞARETİ isim = "vigo" isim.reverse # => "ogiv" isim

    # => "vigo" isim.reverse! # => "ogiv" isim # => "ogiv"
  12. İterasyon [1, "merhaba", 2, "dünya"].each do |eleman| puts eleman end

    [1, "merhaba", 2, "dünya"].each {|eleman| puts eleman} # 1 # merhaba # 2 # dünya
  13. İterasyon [1, 2, 3, 4, 5].select{|number| number.even?} # => [2,

    4] [1, 2, 3, 4, 5].inject{|sum, number| sum + number} # => 15 [1, 2, 3, 4, 5].map{|number| number * 2} # => [2, 4, 6, 8, 10]
  14. class class Person attr_accessor :age end vigo = Person.new vigo

    # => #<Person:0x00007fef1693fe60> vigo.age = 46 vigo # => #<Person:0x00007fef1693fe60 @age=46> vigo.age # => 46
  15. class class Person attr_accessor :age # Getter & Setter def

    initialize(name) @name = name end def greet "Hello #{@name}" end end vigo = Person.new "Uğur" vigo.age = 46 vigo # => #<Person:0x00007f889b056c40 @name="Uğur", @age=46> vigo.greet # => "Hello Uğur"
  16. class class Person def initialize(name) @name = name end def

    age=(value) @age = value end def age @age end def greet "Hello #{@name}" end end vigo = Person.new "Uğur" vigo.age = 43 Getter Setter attr_accessor :age }
  17. class class Person def is_human? true end end class Cyborg

    < Person def is_human? false end end vigo = Person.new # => #<Person:0x007faa7291d268> vigo.is_human? # => true t800 = Cyborg.new # => #<Cyborg:0x007faa7291cbd8> t800.is_human? # => false
  18. module + MIXIN module Greeter def say_hello "Hello #{@name}" end

    end class Person include Greeter def initialize(name) @name = name end end vigo = Person.new "Uğur" vigo.say_hello # => "Hello Uğur"