İçindekiler. Glossary



Benzer belgeler
1 RUBY HAKINDA 1 Ruby nin Gelişim Hikayesi 1 Neden Ruby? 1 Neden Bu Kadar Popüler? 2

(Sözlük, dictionary, birleşik array, map)

Ruby. Prof.Dr.Timur Karaçay Başkent Üniversitesi

Döngüler. Tablo 15.1: Ruby de Başlıca Döngü Yapıları

Bölüm 6. Diziler (arrays) Temel kavramlar Tek boyutlu diziler Çok boyutlu diziler

232 DIZIN. attr, 65 attributes, 65

I Java Veri Yapıları 1

Mantıksal İşlemler. 7.1 true, false, nil

Binary Search. (Yarılama) Bölüm Dizide Bir Öğe Arama

Multiplication/division

Yarışma Sınavı A ) 60 B ) 80 C ) 90 D ) 110 E ) 120. A ) 4(x + 2) B ) 2(x + 4) C ) 2 + ( x + 4) D ) 2 x + 4 E ) x + 4

ArrayList Uygulamaları

Metotlar. d e f metot_adı [ ( [ arg [= d e f a u l t ] ]... [, arg [, &expr ] ] ) ] deyim ( l e r ) end

1 JAVASCRIPT NEDİR? 1

Class LinkedList java.lang.object java.util.abstractcollection java.util.abstractlist java.util.abstractsequentiallist java.util.

Algoritmalar. İkili Arama Ağaçları. Bahar 2016 Doç. Dr. Suat Özdemir 1

Alıştırma 1: Yineleme

Veri Yapıları ve Algoritmalar dönem

Karmaşık Sayılar Karmaşık Sayı Yaratma

Aşağıdaki programı inceleyip ekrana ne yazacağını söyleyebilir misiniz?

TEMPLATES. Binnur Kurt Bilgisayar Mühendisliği Bölümü İstanbul Teknik Üniversitesi. C++ ile Nesneye Dayalı Programlama 1

Matematik Mühendisliği - Mesleki İngilizce

SQL'e Giriş. SELECT Deyimi. SQL Komutları. Yardımcı Deyimler

Mantıksal (Logic) Operatörler

String String İşlemleri

Week 6: Bağlı Liste (Linked List) BAĞLI LİSTE KAVRAMI TEKİL (SINGLE) BAĞLI LİSTE ÇİFT (DOUBLE) BAĞLI LİSTE DAİRESEL (CIRCULAR) BAĞLI LİSTE

Yığıtın en üstündeki öğeyi değer olarak alır; ama onu yığıttan almaz, yerinde bırakır.

İST 264 VERİ YAPILARI Bitirme Sınavı A Grubu

2 PYTHON A GIRIŞ 13 PyCharm İle Python Projesi Oluşturma 15 Projenin Çalıştırılması 18 İlk Python Programımız 19 Açıklama Satırları 21

Maltepe Üniversitesi Bilgisayar Mühendisliği Bölümü BİL 203 Veri Yapıları ve Algoritmalar I

PostgreSQL ve PL/pgSQL

WEEK 11 CME323 NUMERIC ANALYSIS. Lect. Yasin ORTAKCI.

OPERATÖRLER Alıştırmalar

#include <stdio.h> int main(void) { float sayi; float * p; p = &sayi; printf("deger girin:"); scanf("%f", p); printf("girilen deger:%f\n", *p);

Görsel Programlama DERS 04. Görsel Programlama - Ders04/ 1

MICROSOFT WORD Şekil 1 TABLO HAZIRLAMA : Word 2002/II TAB AYARLARI :

Present continous tense

String Sınıfını Yönetmek

Programlama Dilleri 3

TEST RESULTS UFED, XRY and SIMCON

PostgreSQL ve PL/pgSQL

çindekiler 3 Python Programlarn Yürütme Kaynak Program... 24

Lambda, Map, Filter ve Dizi Tamamlama

Başlangıç 2. Generic İfadeler 3. Arayüzler (Interfaces) 5. LINQ ile Sık Kullanılan Arayüzler 7. Koleksiyon Sınıfları 14

Is the P/E cycle of MLC really only 3,000 times as its datasheet said?

NEY METODU SAYFA 082 NEY METHOD PAGE 082. well.

a, ı ı o, u u e, i i ö, ü ü

BBM Discrete Structures: Midterm 2 Date: , Time: 16:00-17:30. Question: Total Points: Score:

İZDÜŞÜM. İzdüşümün Tanımı ve Önemi İzdüşüm Metodları Temel İzdüşüm Düzlemleri Noktanın İzdüşümü Doğrunun İzdüşümü Düzlemlerin İz Düşümleri

SQL Komutları (2) Uzm. Murat YAZICI

Bilgisayar Programcılığı. Ögr. Gör. Cansu AYVAZ GÜVEN

Integer Sınıfı. 9.1 Integer Sınıfına Uygulanan Başlıca Metotlar. Ruby de tamsayılar için kullanılan Fixnum ve Bignum sınıflarını üreten sınıftır.

Al Ruby'i Vur Python'a

D-Link DSL 500G için ayarları

DOSYA ORGANİZASYONU. Sıralı erişimli dosya organizasyonu yöntemleri ÖZLEM AYDIN TRAKYA ÜNİVERSİTESİ BİLGİSAYAR MÜHENDİSLİĞİ BÖLÜMÜ

VERİTABANI Veritabanı Yönetimi

UTL_FILE PERFORMANSI

Veri Yapıları. Yrd. Doç. Dr. Şadi Evren ŞEKER

+,- #'. L = {a, b, c, d} a, b, c, d kümenin elemanları veya üyeleridir

Arasınav Soruları Bahar 2018

Python ile Programlamaya Giriş DERS 8: DEMETLER VE SÖZLÜKLER DR. HÜSEYİN BAHTİYAR

Bu işleçlerin dışında, aşağıda belirtilen karşılaştırma işleçlerinden de yararlanılır.

12. HAFTA BLM323 SAYISAL ANALİZ. Okt. Yasin ORTAKCI.

BBS 514 YAPISAL PROGRAMLAMA (STRUCTURED PROGRAMMING)

Week 5 Examples and Analysis of Algorithms

Data Programming SQL Language. Elbistan Meslek Yüksek Okulu Bahar Yarıyılı

Kafes Sistemler Turesses

Class PriorityQueue. Class PriorityQueue<E> java.lang.object java.util.abstractcollection<e> java.util.abstractqueue<e> java.util.

Neyzen olabilmek için en önemli özellik; sabretmeyi bilmektir. In order to be a neyzen the most important thing is to be patient.

ATILIM UNIVERSITY Department of Computer Engineering

Interface Comparator. Kılgılayan sınıf: Collator. Bildirimi: public interface Comparator

Ders 4: Diziler (Arrays( Arrays) barisgokce.com

BMÜ-111 Algoritma ve Programlama. Bölüm 5. Tek Boyutlu Diziler

Konular. Hafta 5 Veri Tipleri (Devam) BLG339 PROGRAMLAMA DİLLERİ KAVRAMI

MIPS assembler language de branching

VERİ YAPILARI DERS NOTLARI BÖLÜM 1 GİRİŞ. Yard. Doç. Dr. Deniz KILINÇ

BMT207 VERİ YAPILARI DATA STRUCTURE

Bu durumda ya cozum yoktur veya sonsuz cozum vardir. KIsaca cozum tek degildir. Veya cozumler birbirine lineer bagimlidir.

ArrayList(int initialcapacity) Sığası (capacity) parametrenin belirlediği sayıda olan bir dizi oluşturur.

mikroc Dili ile Mikrodenetleyici Programlama Ders Notları

ORACLE DA KÜRSÖRLER. Gerekli sistem değişkenleri

Java: printf() Metodu İle Çıktıyı Biçemleme

YZM 2116 Veri Yapıları

HSancak Nesne Tabanlı Programlama I Ders Notları

Sorgudan elde edilen değerin değişkenlere aktarılmasını sağlar. Sorgudan tek satır dönmesi gerekir. Çok satır dönerse hata verir.

Properties of Regular Languages. Mart 2006 Ankara Üniversitesi Bilgisayar Mühendisliği - TY 1

Standard Template Library

Learn how to get started with Dropbox: Take your stuff anywhere. Send large files. Keep your files safe. Work on files together. Welcome to Dropbox!

YZM 2116 Veri Yapıları

FINITE AUTOMATA. Mart 2006 Ankara Üniversitesi Bilgisayar Mühendisliği 1

KISIM I: TEMEL KONULAR 1

Operatörlere İşlev Yükleme

Do not open the exam until you are told that you may begin.

Veritabanı. SQL (Structured Query Language)

Arýza Giderme. Troubleshooting

Selection Sort Insertion Sort

(Dizim) Array. 1.1 Array Nedir?

Microsoft SQL Server Sorgulama

static void dizi_24() { Random rnd = new Random(); int [] tutulan=new int[10]; int dogru;

Sıralı Erişimli Dosyalar. Kütük Organizasyonu 1

Transkript:

İçindekiler Glossary i 1 Yönlendirme Uygulamaları 1 1.0.1 times Döngüsü............................. 3 1.0.2 for Döngüsü............................... 4 1.1 each_byte.................................... 5 1.1.1 retray.................................. 5 1.1.2 unless.................................. 5 1.1.3 Döngüyü Durdurma.......................... 6 2 Array Uygulamaları 9 2.0.4 Array Yaratma............................. 9 2.1 Array in Öğeleri ve Sayısı........................... 11 2.2 Öğelerin Değiştirilmesi............................. 11 2.3 Sıralama, Arama................................ 13 2.4 Mukayese.................................... 13 2.5 Yığıt (stack) ve Kuyruk............................ 14 2.6 Array ve Küme................................. 14 2.6.1 iterator................................. 15 2.6.2 iterator................................. 15 2.6.3 Associative Metotlar.......................... 15 2.6.4 Başka Metotlar............................. 16

ii 2.7 Array Uygulamaları.............................. 16 2.8 Arrayin Öğelerine Erişim........................... 18 2.8.1 Arraye Öğe Ekleme........................... 19 2.8.2 Arrayde Öğe Silme........................... 19 2.9 uniq ve uniq! Metotları............................. 20 2.10 Array Üzerinde Döngüler........................... 20 2.10.1 map................................... 20 2.10.2 select.................................. 21

2 Array Uygulamaları Uyarı 2.1. Bu derste yer alan uygulamaların çoğu, internette serbest öğretim sayfalarından (tutorial) derlenmiştir. Alıntılar birden çok sitede yer alan öğretici niteliği yüksek olan örnek programlardır. Asıl kaynağı belli olmayan bu örneklere atıf yapılamamıştır. Array yapısı hash yapısına çok benzer. Aralarındak önemli farklar şunlardır: 1. Array in öğeleri ardışık olarak numaralanır (index). İndisler o dan başlar, birer artarak devam eder. 2. Array in öğeleri yalındır. Hash yapısında olduğu gibi anahtar-değer çiftlerinden oluşmaz. Array yapısına hash yapısının metotlarının çoğu uygulanabilir. Array yapısına uygulanabilem metotları görmek için methods metodunu kullanabiliriz: Bu deyim Array yapısına uygulanabilen metotların uzun listesini verir. Array.methods 2.0.4 Array Yaratma Array ambarı yaratmak için şu yöntemleri kullanabiliriz:

10 BÖLÜM 2. ARRAY UYGULAMALARI 1. new Operatörünü Kullanma arr1= Array.new #=> [ ] deyimi boş bir array ambarı yaratır. Ambara nesnelerin nasıl konulacağını biraz sonra göreceğiz. 2. [ ] Operatörünü Kullanma arr2= [ ] deyimi boş bir array ambarı yaratır. 3. Bildirim Anında Ambara Nesneleri Koyma Array adını (pointer) izleyen= atama operatöründen sonra [ ] içine ambara konulacak nesneler yazılır. Ambardaki nesneler virgül ile birbirlerinden ayrılır. Başlangıçta söylediğimiz gibi, Ruby array ambarına farklı tipyen veriler (nesneler) konulabilir. arr3= ["Ankara", 3, 23.45, true, A ] Aşağıdaki örnekler, herkese açık ve öğretici olduğu için, Ruby dilinin yaratıcısı Yukihiro Matsumoto nun kitabının 9.5.2. bölümünden aynen alınmıştır (bkz. citematz12). Liste 2.1. 1 [ 1, 2, 3 ] # Basic array l i t e r a l [ ] # An empty array 3 [ ] # Arrays are mutable : t h i s empty array i s d i f f e r e n t %w[ a b c ] # => [ a, b, c ] : array of words 5 Array [ 1, 2, 3 ] # => [ 1, 2, 3 ] : j u s t l i k e an array l i t e r a l Creating arrays with the new ( ) method 7 empty = Array. new # [ ] : r e t u r n s a new empty array n i l s = Array. new (3) # [ n i l, n i l, n i l ] : three n i l elements 9 copy = Array. new( n i l s ) # Make a new copy of an e x i s t i n g array z e r o s = Array. new (4, 0) # [ 0, 0, 0, 0 ] : four 0 elements 11 count = Array. new (3) { i i +1} # [ 1, 2, 3 ] : three elements computed by block # Be c a r e f u l with repeated o b j e c t s 13 a=array. new (3, a ) # => [ a, a, a ] : three r e f e r e n c e s to the same s t r i n g a [ 0 ]. upcase! # C a p i t a l i z e the f i r s t element of the array 15 a # => [ A, A, A ] : they are a l l the same s t r i n g! a=array. new (3) { b } # => [ b, b, b ] : three d i s t i n c t s t r i n g o b j e c t s 17 a [ 0 ]. upcase! ; # C a p i t a l i z e the f i r s t one a # => [ B, b, b ] : the o t h e r s are s t i l l lowercase

2.1. ARRAY IN ÖĞELERI VE SAYISI 11 2.1 Array in Öğeleri ve Sayısı Liste 2.2. # Array length 2 [ 1, 2, 3 ]. length # => 3 [ ]. s i z e # => 0 : synonym f o r length 4 [ ]. empty? # => true [ n i l ]. empty? # => f a l s e 6 [ 1, 2, n i l ]. nitems # => 2 : number of non n i l elements [ 1, 2, 3 ]. nitems { x x>2} # => 1 : # of e l t s matching block (Ruby 1. 9 ) 8 # Indexing s i n g l e elements 10 a = %w[ a b c d ] # => [ a, b, c, d ] a [ 0 ] # => a : f i r s t element 12 a [ 1] # => d : l a s t element a [ a. s i z e 1] # => d : l a s t element 14 a[ a. s i z e 1] # => a : f i r s t element a [ 5 ] # => n i l : no such element 16 a [ 5] # => n i l : no such element a. at (2) # => c : j u s t l i k e [ ] f o r s i n g l e i n t e g e r argument 18 a. f e t c h (1) # => b : a l s o l i k e [ ] and at a. f e t c h ( 1) # => d : works with negative args 20 a. f e t c h (5) # => IndexError! : does not allow out of bounds a. f e t c h ( 5) # => IndexError! : does not allow out of bounds 22 a. f e t c h (5, 0) # => 0 : return 2nd arg when out of bounds a. f e t c h (5) { x x x} # => 25: compute value when out of bounds 24 a. f i r s t # => a : the f i r s t element a. l a s t # => d : the l a s t element 26 a. c h o i c e # Ruby 1. 9 : return one element at random 28 # Indexing subarrays a [ 0, 2 ] # => [ a, b ] : two elements, s t a r t i n g at 0 30 a [ 0.. 2 ] # => [ a, b, c ] : elements with index in range a [ 0... 2 ] # => [ a, b ] : three dots i n s t e a d of two 32 a [ 1, 1 ] # => [ b ] : s i n g l e element, as an array a [ 2,2] # => [ c, d ] : l a s t two elements 34 a [ 4, 2 ] # => [ ] : empty array r i g h t at the end a [ 5, 1 ] # => n i l : nothing beyond that 36 a. s l i c e ( 0.. 1 ) # => [ a, b ] : s l i c e i s synonym f o r [ ] a. f i r s t (3) # => [ a, b, c ] : f i r s t three elements 38 a. l a s t (1) # => [ d ] : l a s t element as an array 40 # Extracting a r b i t r a r y values a. values_at ( 0, 2 ) # => [ a, c ] 42 a. values_at (4, 3, 2, 1) # => [ n i l, d, c, b ] a. values_at (0, 2.. 3, 1) # => [ a, c, d, d ] 44 a. values_at ( 0.. 2, 1.. 3 ) # => [ a, b, c, b, c, d ] 2.2 Öğelerin Değiştirilmesi Liste 2.3.

12 BÖLÜM 2. ARRAY UYGULAMALARI a = [ 1, 2, 3 ] # Start with t h i s array 2 # Changing the value o f elements a [ 0 ] = 0 # Alter an e x i s t i n g element : a i s [ 0, 2, 3 ] 4 a [ 1] = 4 # Alter the l a s t element : a i s [ 0, 2, 4 ] a [ 1 ] = n i l # Set the 2nd element to n i l : a i s [ 0, n i l, 4 ] 6 # Appending to an array 8 a = [ 1, 2, 3 ] # Start over with t h i s array a [ 3 ] = 4 # Add a fourth element to i t : a i s [ 1, 2, 3, 4 ] 10 a [ 5 ] = 6 # We can skip elements : a i s [ 1, 2, 3, 4, n i l, 6 ] a << 7 # => [ 1, 2, 3, 4, n i l, 6, 7 ] 12 a << 8 << 9 # => [ 1, 2, 3, 4, n i l, 6, 7, 8, 9 ] operator i s chainable a = [ 1, 2, 3 ] # Start over with short array 14 a + a # => [ 1, 2, 3, 1, 2, 3 ] : + concatenates i n t o new array a. concat ( [ 4, 5 ] ) # => [ 1, 2, 3, 4, 5 ] : a l t e r a in place : note no! 16 # I n s e r t i n g elements with i n s e r t 18 a = [ a, b, c ] a. i n s e r t (1, 1, 2) # a now holds [ a, 1, 2, b, c ]. Like a [ 1, 0 ] = [ 1, 2 ] 20 # Removing ( and r e t u r n i n g ) i n d i v i d u a l elements by index 22 a = [ 1, 2, 3, 4, 5, 6 ] a. delete_at (4) # => 5 : a i s now [ 1, 2, 3, 4, 6 ] 24 a. delete_at ( 1) # => 6 : a i s now [ 1, 2, 3, 4 ] a. delete_at (4) # => n i l : a i s unchanged 26 # Removing elements by value 28 a. d e l e t e (4) # => 4 : a i s [ 1, 2, 3 ] a [ 1 ] = 1 # a i s now [ 1, 1, 3 ] 30 a. d e l e t e (1) # => 1 : a i s now [ 3 ] : both 1 s removed a = [ 1, 2, 3 ] 32 a. d e l e t e _ i f { x x%2==1} # Remove odd values : a i s now [ 2 ] 336 Chapter 9 : The Ruby Platform 34 a. r e j e c t! { x x%2==0} # Like d e l e t e _ i f : a i s now [ ] 36 # Removing elements and subarrays with s l i c e! a = [ 1, 2, 3, 4, 5, 6, 7, 8 ] 38 a. s l i c e! ( 0 ) # => 1 : remove element 0 : a i s [ 2, 3, 4, 5, 6, 7, 8 ] a. s l i c e!( 1,1) # => [ 8 ] : remove subarray at end : a i s [ 2, 3, 4, 5, 6, 7 ] 40 a. s l i c e! ( 2.. 3 ) # => [ 4, 5 ] : works with ranges : a i s [ 2, 3, 6, 7 ] a. s l i c e! ( 4, 2 ) # => [ ] : empty array j u s t past end : a unchanged 42 a. s l i c e! ( 5, 2 ) # => n i l : a now holds [ 2, 3, 6, 7, n i l ]! 44 # Replacing subarrays with []= # To delete, a s s i g n an empty array 46 # To i n s e r t, a s s i g n to a zero width s l i c e a = ( a.. e ). to_a # => [ a, b, c, d, e ] 48 a [ 0, 2 ] = [ A, B ] # a now holds [ A, B, c, d, e ] a [ 2... 5 ] = [ C, D, E ] # a now holds [ A, B, C, D, E ] 50 a [ 0, 0 ] = [ 1, 2, 3 ] # I n s e r t elements at the beginning of a a [ 0.. 2 ] = [ ] # Delete those elements 52 a [ 1,1] = [ Z ] # Replace l a s t element with another a [ 1,1] = Z # For s i n g l e elements, the array i s o p t i o n a l 54 a [ 1, 4 ] = n i l # Ruby 1. 9 : a now holds [ A, n i l ] # Ruby 1. 8 : a now holds [ A ] : n i l works l i k e [ ] 56 # Other methods

2.3. SIRALAMA, ARAMA 13 58 a = [ 4, 5 ] a. r e p l a c e ( [ 1, 2, 3 ] ) # a now holds [ 1, 2, 3 ] : a copy of i t s argument 60 a. f i l l (0) # a now holds [ 0, 0, 0 ] a. f i l l ( n i l, 1, 3 ) # a now holds [ 0, n i l, n i l, n i l ] 62 a. f i l l ( a, 2.. 4 ) # a now holds [ 0, n i l, a, a, a ] a [ 3 ]. upcase! # a now holds [ 0, n i l, A, A, A ] 64 a. f i l l ( 2.. 4 ) { b } # a now holds [ 0, n i l, b, b, b ] a [ 3 ]. upcase! # a now holds [ 0, n i l, b, B, b ] 66 a. compact # => [ 0, b, B, b ] : copy with n i l s removed a. compact! # Remove n i l s in place : a now holds [ 0, b, B, b ] 68 a. c l e a r # a now holds [ ] 2.3 Sıralama, Arama Liste 2.4. a = [ a, b, c ] 2 a. each { e l t p r i n t e l t } # The b a s i c each i t e r a t o r p r i n t s " abc " a. reverse_each { e p r i n t e} # Array s p e c i f i c : p r i n t s " cba " 4 a. c y c l e { e p r i n t e } # Ruby 1. 9 : p r i n t s " abcabcabc... " f o r e v e r a. each_index { i p r i n t i } # Array s p e c i f i c : p r i n t s "012" 6 a. each_with_index { e, i p r i n t e, i } # Enumerable : p r i n t s " a0b1c2 " a. map { x x. upcase } # Enumerable : r e t u r n s [ A, B, C ] 8 a. map! { x x. upcase } # Array s p e c i f i c : a l t e r s a in place a. c o l l e c t! { x x. downcase! } # c o l l e c t! i s synonym f o r map! 10 # Searching methods 12 a = %w[ h e l l o ] a. i n c l u d e?( e ) # => true 14 9.5 C o l l e c t i o n s 337 a. i n c l u d e?( w ) # => f a l s e 16 a. index ( l ) # => 2 : index of f i r s t match a. index ( L ) # => n i l : no match found 18 a. rindex ( l ) # => 3 : search backwards a. index { c c =~ / [ aeiou ]/} # => 1 : index of f i r s t vowel. Ruby 1. 9. 20 a. rindex { c c =~ / [ aeiou ]/} # => 4 : index of l a s t vowel. Ruby 1. 9. 22 # Sorting a. s o r t # => %w[ e h l l o ] : copy a and s o r t the copy 24 a. s o r t! # Sort in place : a now holds [ e, h, l, l, o ] a = [ 1, 2, 3, 4, 5 ] # A new array to s o r t i n t o evens and odds 26 a. s o r t! { a, b a%2 <=> b%2} # Compare elements modulo 2 # S h u f f l i n g arrays : the opposite of s o r t i n g ; Ruby 1.9 only 28 a = [ 1, 2, 3 ] # Start ordered puts a. s h u f f l e # S h u f f l e randomly. E. g. : [ 3, 1, 2 ]. Also s h u f f l e! 2.4 Mukayese Liste 2.5.

14 BÖLÜM 2. ARRAY UYGULAMALARI 1 [ 1, 2 ] <=> [ 4, 5 ] # => 1 because 1 < 4 [ 1, 2 ] <=> [ 0, 0, 0 ] # => +1 because 1 > 0 3 [ 1, 2 ] <=> [ 1, 2, 3 ] # => 1 because f i r s t array i s s h o r t e r [ 1, 2 ] <=> [ 1, 2 ] # => 0 : they are equal 5 [ 1, 2 ] <=> [ ] # => +1 [ ] always l e s s than a nonempty array 2.5 Yığıt (stack) ve Kuyruk pop ve push Liste 2.6. 1 a = [ ] a. push (1) # => [ 1 ] : a i s now [ 1 ] 3 a. push ( 2, 3 ) # => [ 1, 2, 3 ] : a i s now [ 1, 2, 3 ] a. pop # => 3 : a i s now [ 1, 2 ] 5 338 Chapter 9 : The Ruby Platform a. pop # => 2 : a i s now [ 1 ] 7 a. pop # => 1 : a i s now [ ] a. pop # => n i l : a i s s t i l l [ ] push ve shift Liste 2.7. a = [ ] 2 a. push (1) # => [ 1 ] : a i s [ 1 ] a. push (2) # => [ 1, 2 ] : a i s [ 1, 2 ] 4 a. s h i f t # => 1 : a i s [ 2 ] a. push (3) # => [ 2, 3 ] : a i s [ 2, 3 ] 6 a. s h i f t # => 2 : a i s [ 3 ] a. s h i f t # => 3 : a i s [ ] 8 a. s h i f t # => n i l : a i s [ ] 2.6 Array ve Küme Liste 2.8. [ 1, 3, 5 ] & [ 1, 2, 3 ] # => [ 1, 3 ] : s e t i n t e r s e c t i o n 2 [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] # => [ 1, 3 ] : d u p l i c a t e s removed [ 1, 3, 5 ] [ 2, 4, 6 ] # => [ 1, 3, 5, 2, 4, 6 ] : s e t union 4 [ 1, 3, 5, 5 ] [ 2, 4, 6, 6 ] # => [ 1, 3, 5, 2, 4, 6 ] : d u p l i c a t e s removed [ 1, 2, 3 ] [ 2, 3 ] # => [ 1 ] : s e t d i f f e r e n c e 6 [ 1, 1, 2, 2, 3, 3 ] [ 2, 3 ] # => [ 1, 1 ] : not a l l d u p l i c a t e s removed small = 0.. 1 0. to_a # A s e t of small numbers 8 even = 0.. 5 0. map { x x 2} # A s e t of even numbers

2.6. ARRAY VE KÜME 15 smalleven = small & even # Set i n t e r s e c t i o n 10 smalleven. i n c l u d e?(8) # => true : t e s t f o r s e t membership [ 1, 1, n i l, n i l ]. uniq # => [ 1, n i l ] : remove dups. Also uniq! 2.6.1 iterator Liste 2.9. 1 [ 1, 3, 5 ] & [ 1, 2, 3 ] # => [ 1, 3 ] : s e t i n t e r s e c t i o n [ 1, 1, 3, 5 ] & [ 1, 2, 3 ] # => [ 1, 3 ] : d u p l i c a t e s removed 3 [ 1, 3, 5 ] [ 2, 4, 6 ] # => [ 1, 3, 5, 2, 4, 6 ] : s e t union [ 1, 3, 5, 5 ] [ 2, 4, 6, 6 ] # => [ 1, 3, 5, 2, 4, 6 ] : d u p l i c a t e s removed 5 [ 1, 2, 3 ] [ 2, 3 ] # => [ 1 ] : s e t d i f f e r e n c e [ 1, 1, 2, 2, 3, 3 ] [ 2, 3 ] # => [ 1, 1 ] : not a l l d u p l i c a t e s removed 7 small = 0.. 1 0. to_a # A s e t of small numbers even = 0.. 5 0. map { x x 2} # A s e t of even numbers 9 smalleven = small & even # Set i n t e r s e c t i o n smalleven. i n c l u d e?(8) # => true : t e s t f o r s e t membership 11 [ 1, 1, n i l, n i l ]. uniq # => [ 1, n i l ] : remove dups. Also uniq! 2.6.2 iterator Liste 2.10. 1 a = [ 1, 2, 3 ] # I t e r a t e a l l p o s s i b l e 2 element subarrays ( order matters ) 3 a. permutation (2) { x p r i n t x } # Prints " [ 1, 2 ] [ 1, 3 ] [ 2, 1 ] [ 2, 3 ] [ 3, 1 ] [ 3, 2 ] " # I t e r a t e a l l p o s s i b l e 2 element s u b s e t s ( order does not matter ) 5 a. combination (2) { x p r i n t x } # Prints " [ 1, 2 ] [ 1, 3 ] [ 2, 3 ] " 7 9.5 C o l l e c t i o n s 339 # Return the Cartesian product of the two s e t s 9 a. product ( [ a, b ] ) # => [ [ 1, " a " ], [ 1, " b " ], [ 2, " a " ], [ 2, " b " ], [ 3, " a " ], [ 3, " b " ] ] [ 1, 2 ]. product ( [ 3, 4 ], [ 5, 6 ] ) # => [ [ 1, 3, 5 ], [ 1, 3, 6 ], [ 1, 4, 5 ], [ 1, 4, 6 ], etc... ] 2.6.3 Associative Metotlar Liste 2.11. [ [ key1, value1 ], [ key2, value2 ], [ key3, value3 ],... ] 2 h = { : a => 1, : b => 2} # Start with a hash 4 a = h. to_a # => [ [ : b, 2 ], [ : a, 1 ] ] : a s s o c i a t i v e array a. assoc ( : a ) # => [ : a, 1 ] : subarray f o r key : a 6 a. assoc ( : b). l a s t # => 2 : value f o r key : b

16 BÖLÜM 2. ARRAY UYGULAMALARI a. r a s s o c (1) # => [ : a, 1 ] : subarray f o r value 1 8 a. r a s s o c (2). f i r s t # => : b : key f o r value 2 a. assoc ( : c ) # => n i l 10 a. transpose # => [ [ : a, : b ], [ 1, 2 ] ] : swap rows and c o l s 2.6.4 Başka Metotlar Liste 2.12. [ [ key1, value1 ], [ key2, value2 ], [ key3, value3 ],... ] 2 # Conversion to s t r i n g s 4 [ 1, 2, 3 ]. j o i n # => " 1 2 3 " : convert elements to s t r i n g and j o i n [ 1, 2, 3 ]. j o i n ( ", " ) # => "1, 2, 3 " : o p t i o n a l d e l i m i t e r 6 [ 1, 2, 3 ]. to_s # => " [ 1, 2, 3 ] " in Ruby 1.9 [ 1, 2, 3 ]. to_s # => "123" in Ruby 1.8 8 [ 1, 2, 3 ]. i n s p e c t # => " [ 1, 2, 3 ] " : b e t t e r f o r debugging in 1.8 # Binary conversion with pack. See a l s o S t r i n g. unpack. 10 [ 1, 2, 3, 4 ]. pack ( "CCCC" ) # => "\001\002\003\004" [ 1, 2 ]. pack ( s2 ) # => "\001\000\002\000" 12 [ 1 2 3 4 ]. pack ( " i " ) # => "\322\004\000\000" # Other methods 14 [ 0, 1 ] 3 # => [ 0, 1, 0, 1, 0, 1 ] : operator r e p e a t s [ 1, [ 2, [ 3 ] ] ]. f l a t t e n # => [ 1, 2, 3 ] : r e c u r s i v e l y f l a t t e n ; a l s o f l a t t e n! 16 [ 1, [ 2, [ 3 ] ] ]. f l a t t e n (1) # => [ 1, 2, [ 3 ] ] : s p e c i f y # of l e v e l s ; Ruby 1.9 [ 1, 2, 3 ]. r e v e r s e # => [ 3, 2, 1 ] : a l s o r e v e r s e! 18 a = [ 1, 2, 3 ]. zip ( [ : a, : b, : c ] ) # => [ [ 1, : a ], [ 2, : b ], [ 3, : c ] ] : Enumerable method a. transpose # => [ [ 1, 2, 3 ], [ : a, : b, : c ] ] : swap rows/ c o l s 2.7 Array Uygulamaları [ ] Operatörü İle Array Yaratma Liste 2.13. 1 arr1 = [ ] arr2 = [ 1 0, 2 0, 3 0, 4 0, 5 0, 6 0 ] 3 arr3 = [ 5.. 0, 0.. 4 ] arr4 = [ [ 1, 2 ], [ 3, a ], [ 4 ] ] 5 x = 5 7 y = 3 arr5 = [ x+y, x y, x y ] 9 11 p r i n t ( arr1 ) p r i n t ( " \n " )

2.7. ARRAY UYGULAMALARI 17 13 p r i n t ( arr2 ) 15 p r i n t ( " \n " ) 17 p r i n t ( arr3 ) p r i n t ( " \n " ) 19 p r i n t ( arr4 ) 21 p r i n t ( " \n " ) 23 p r i n t ( arr5 ) p r i n t ( " \n " ) Liste 2.14. arr1 = [ " Ankara ", 3, 23.45, true, K ] 2 arr2 = Array [ 1, a, A ] arr3 = [ 1, a, A ] 4 p r i n t ( arr1 ) 6 p r i n t ( " \n " ) 8 p r i n t ( arr2 ) p r i n t ( " \n " ) 10 p r i n t ( arr3 ) 12 p r i n t ( " \n " ) new Operatör İle Array Yaratma Liste 2.15. boş_dizim = Array. new # => [ ] 2 n i l = Array. new (3) # => [ n i l, n i l, n i l ] f a l s e = Array. new (3, f a l s e ) # => [ f a l s e, f a l s e, f a l s e ] 4 s ı f ı r _ d i z i m = Array. new ( 3, 0 ) # => [ 0, 0, 0 ] Sabit Öğeli Array Yaratma Liste 2.16. ary = Array. new #=> [ ] 2 Array. new (3) #=> [ n i l, n i l, n i l ] Array. new (3, t r u e ) #=> [ true, true, true ] Liste 2.17. 1 Array. new (4) { Hash. new } #=> [ { }, {}, {}, {}] empty_table = Array. new (3) { Array. new (3) } 3 #=> [ [ n i l, n i l, n i l ], [ n i l, n i l, n i l ], [ n i l, n i l, n i l ] ] Array ( { : a => " a ", : b => " b " }) #=> [ [ : a, " a " ], [ : b, " b " ] ]

18 BÖLÜM 2. ARRAY UYGULAMALARI 2.8 Arrayin Öğelerine Erişim Liste 2.18. arr = [ 1, 2, 3, 4, 5, 6 ] 2 arr [ 2 ] #=> 3 arr [ 1 0 0 ] #=> n i l 4 arr [ 3] #=> 4 arr [ 2, 3 ] #=> [ 3, 4, 5 ] 6 arr [ 1.. 4 ] #=> [ 2, 3, 4, 5 ] arr [ 1.. 3 ] #=> [ 2, 3, 4 ] 8 arr. at (0) #=> 1 10 arr. f i r s t #=> 1 arr. l a s t #=> 6 12 14 arr. take (3) #=> [ 1, 2, 3 ] arr. drop (3) #=> [ 4, 5, 6 ] Liste 2.19. 1 a = [ " a ", " b ", " c ", " d ", " e " ] a [ 2 ] + a [ 0 ] + a [ 1 ] #=> " cab " 3 a [ 6 ] #=> n i l a [ 1, 2 ] #=> [ " b ", " c " ] 5 a [ 1.. 3 ] #=> [ " b ", " c ", " d " ] a [ 4.. 7 ] #=> [ " e " ] 7 a [ 6.. 1 0 ] #=> n i l a[ 3, 3 ] #=> [ " c ", " d ", " e " ] 9 # s p e c i a l c a s e s a [ 5 ] #=> n i l 11 a [ 6, 1 ] #=> n i l a [ 5, 1 ] #=> [ ] 13 a [ 5.. 1 0 ] #=> [ ] fetch indis aralığı dışında bir öğe istenirse, hata iletisi alınır. Bu durumda, istenirse, öndeğer hata iletisi yerine, kullanıcı başka bir metin koyabilir: Liste 2.20. 1 arr = [ a, b, c, d, e, f ] arr. f e t c h (100) #=> IndexError : index 100 o u t s i d e of array bounds : 6...6 3 arr. f e t c h (100, " dikkat et! " ) #=> " dkkat et! "

2.8. ARRAYIN ÖĞELERINE ERIŞIM 19 Bilgi Alma Liste 2.21. 1 browsers = [ Chrome, F i r e f o x, S a f a r i, Opera, IE ] 3 browsers. length #=> 5 browsers. count #=> 5 5 browsers. empty? #=> f a l s e browsers. i n c l u d e?( Konqueror ) #=> f a l s e 2.8.1 Arraye Öğe Ekleme Liste 2.22. arr = [ 1, 2, 3, 4 ] 2 arr. push (5) #=> [ 1, 2, 3, 4, 5 ] 4 arr << 6 #=> [ 1, 2, 3, 4, 5, 6 ] arr. u n s h i f t (0) #=> [ 0, 1, 2, 3, 4, 5, 6 ] 6 arr. i n s e r t (3, apple ) #=> [ 0, 1, 2, apple, 3, 4, 5, 6 ] arr. i n s e r t (3, orange, pear, g r a p e f r u i t ) 8 #=> [ 0, 1, 2, " orange ", " pear ", " g r a p e f r u i t ", " apple ", 3, 4, 5, 6 ] 2.8.2 Arrayde Öğe Silme Liste 2.23. arr = [ 1, 2, 3, 4, 5, 6 ] 2 arr. pop #=> 6 arr #=> [ 1, 2, 3, 4, 5 ] 4 arr. s h i f t #=> 1 6 arr #=> [ 2, 3, 4, 5 ] 8 arr. delete_at (2) #=> 4 arr #=> [ 2, 3, 5 ] 10 arr. d e l e t e (2) #=> 2 12 arr #=> [ 3, 5 ] Liste 2.24. Arrayden nil öğeleri yoketme: arr = [ foo, 0, n i l, bar, 7, baz, n i l ] 2 arr. compact #=> [ foo, 0, bar, 7, baz ] arr #=> [ foo, 0, n i l, bar, 7, baz, n i l ] 4 arr. compact! #=> [ foo, 0, bar, 7, baz ] arr #=> [ foo, 0, bar, 7, baz ]

20 BÖLÜM 2. ARRAY UYGULAMALARI 2.9 uniq ve uniq! Metotları uniq metodu array den tekrarlayan öğeleri siler, ama arayin aslını bozmaz. uniq! metodu array den tekrarlayan öğeleri siler, ama arayin aslını korumaz. Liste 2.25. 1 arr = [ 2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556] arr. uniq #=> [ 2, 5, 6, 556, 8, 9, 0, 123] 3 arr # => [ 2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556] arr. uniq! #=> [ 2, 5, 6, 556, 8, 9, 0, 123] 5 arr #=> [ 2, 5, 6, 556, 8, 9, 0, 123] 2.10 Array Üzerinde Döngüler Liste 2.26. 1 arr = [ 1, 2, 3, 4, 5 ] arr. each { a p r i n t a = 10, " " } / 2 [ 1, 2, 3, 4, 5 ] 9 8 7 6 5 [ 1, 2, 3, 4, 5 ] Liste 2.27. 1 words = %w[ f i r s t second t h i r d fourth f i f t h s i x t h ] s t r = " " 3 words. reverse_each { word s t r += "#{word} " } p s t r #=> " s i x t h f i f t h fourth t h i r d second f i r s t " 2.10.1 map Liste 2.28. arr. map { a 2 a } #=> [ 2, 4, 6, 8, 10] 2 arr #=> [ 1, 2, 3, 4, 5 ] arr. map! { a a 2 } #=> [ 1, 4, 9, 16, 25] 4 arr #=> [ 1, 4, 9, 16, 25]

2.10. ARRAY ÜZERINDE DÖNGÜLER 21 2.10.2 select Liste 2.29. arr = [ 1, 2, 3, 4, 5, 6 ] 2 arr. s e l e c t { a a > 3 } #=> [ 4, 5, 6 ] arr. r e j e c t { a a < 3 } #=> [ 3, 4, 5, 6 ] 4 arr. drop_while { a a < 4 } #=> [ 4, 5, 6 ] arr #=> [ 1, 2, 3, 4, 5, 6 ] keep_if Liste 2.30. 1 arr. d e l e t e _ i f { a a < 4 } #=> [ 4, 5, 6 ] arr #=> [ 4, 5, 6 ] 3 arr = [ 1, 2, 3, 4, 5, 6 ] 5 arr. keep_if { a a < 4 } #=> [ 1, 2, 3 ] arr #=> [ 1, 2, 3 ] pop Liste 2.31. a = [ " a ", " b ", " c ", " d " ] 2 a. pop #=> " d " a. pop (2) #=> [ " b ", " c " ] 4 a #=> [ " a " ] replace Liste 2.32. a = [ " a ", " b ", " c ", " d ", " e " ] 2 a. r e p l a c e ( [ " x ", " y ", " z " ] ) #=> [ " x ", " y ", " z " ] a #=> [ " x ", " y ", " z " ] reverse Liste 2.33. 1 [ " a ", " b ", " c " ]. r e v e r s e #=> [ " c ", " b ", " a " ] [ 1 ]. r e v e r s e #=> [ 1 ]