├── .gitignore ├── .ipynb_checkpoints └── tutorial-checkpoint.ipynb ├── README.md ├── _config.yml ├── _site ├── README.md ├── images │ ├── bg_hr.png │ ├── blacktocat.png │ ├── icon_download.png │ └── sprite_download.png ├── index.md ├── javascripts │ └── main.js ├── notebook_demo.gif ├── params.json ├── stylesheets │ ├── github-light.css │ ├── normalize.css │ └── stylesheet.css ├── tutorial.ipynb └── tutorial.md ├── examples ├── rspec_testing │ ├── Document.rb │ └── document_spec.rb ├── stub_testing │ ├── Document.rb │ ├── document_spec.rb │ ├── printable_document.rb │ ├── printable_document_spec.rb │ └── printable_document_test.rb └── unit_testing │ ├── Document.rb │ ├── document_test.rb │ └── my_test.rb ├── index.md ├── notebook_demo.gif ├── stylesheets ├── github-light.css ├── normalize.css └── stylesheet.css ├── tutorial.ipynb └── tutorial.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # RubyTutorial 3 | 4 | We are using Jupyter Notebooks to make this tutorial. 5 | For those who don't know about Jupyter Notebooks, go [here](https://jupyter.org/). 6 | Below is a sample, about how notebooks work. 7 | 8 | ![alt text](https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/master/notebook_demo.gif "Without Smooth Scroll") 9 | 10 | ## Basics 11 | 12 | ### Data Types 13 | 14 | 15 | ```ruby 16 | my_num = 15 17 | my_string = "Tushar" 18 | my_bool = true 19 | my_own_string = "tuteja" 20 | 21 | puts my_num, my_string, my_bool 22 | puts my_own_string 23 | ``` 24 | 25 | 15 26 | Tushar 27 | true 28 | tuteja 29 | 30 | 31 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 32 | 33 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 34 | 35 | 36 | ```ruby 37 | print my_num, my_string, my_bool 38 | ``` 39 | 40 | 15Tushartrue 41 | 42 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 43 | 44 | Everything is a an Object in Ruby, 45 | 46 | Everything! 47 | 48 | To find out class of a ruby object we can call a 'class' method on any object. 49 | 50 | 51 | ```ruby 52 | puts my_num.class() , my_string.class() , my_bool.class() 53 | ``` 54 | 55 | Fixnum 56 | String 57 | TrueClass 58 | 59 | 60 | ## Arithmetic 61 | 62 | 63 | Basic Arithmetic operators are '+','-','*','/' and '%'. 64 | 65 | 66 | ```ruby 67 | five = 5 68 | two = 2 69 | seven = 5 + 2 70 | 71 | ``` 72 | 73 | 74 | 75 | 76 | 7 77 | 78 | 79 | 80 | 81 | ```ruby 82 | ten = five * two 83 | ``` 84 | 85 | 86 | 87 | 88 | 10 89 | 90 | 91 | 92 | 93 | ```ruby 94 | one = five % two 95 | ``` 96 | 97 | 98 | 99 | 100 | 1 101 | 102 | 103 | 104 | 105 | ```ruby 106 | two = five / two 107 | ``` 108 | 109 | 110 | 111 | 112 | 2 113 | 114 | 115 | 116 | 117 | ```ruby 118 | three = five - two 119 | 120 | ``` 121 | 122 | 123 | 124 | 125 | 3 126 | 127 | 128 | 129 | ## Strings 130 | 131 | 132 | ```ruby 133 | my_string = "Programming in Ruby Is Fun." 134 | ``` 135 | 136 | 137 | 138 | 139 | "Programming in Ruby Is Fun." 140 | 141 | 142 | 143 | 144 | ```ruby 145 | my_string.length() 146 | ``` 147 | 148 | 149 | 150 | 151 | 27 152 | 153 | 154 | 155 | 156 | ```ruby 157 | my_string.reverse() 158 | ``` 159 | 160 | 161 | 162 | 163 | ".nuF sI ybuR ni gnimmargorP" 164 | 165 | 166 | 167 | 168 | ```ruby 169 | puts my_string 170 | ``` 171 | 172 | Programming in Ruby Is Fun. 173 | 174 | 175 | 176 | ```ruby 177 | my_string.reverse!() 178 | ``` 179 | 180 | 181 | 182 | 183 | ".nuF sI ybuR ni gnimmargorP" 184 | 185 | 186 | 187 | 188 | ```ruby 189 | puts my_string 190 | ``` 191 | 192 | .nuF sI ybuR ni gnimmargorP 193 | 194 | 195 | 196 | ```ruby 197 | my_string = "Programming in Ruby Is Fun." 198 | my_string.split(" ") 199 | ``` 200 | 201 | 202 | 203 | 204 | ["Programming", "in", "Ruby", "Is", "Fun."] 205 | 206 | 207 | 208 | 209 | ```ruby 210 | words = my_string.split(" ") 211 | puts words #Gives an array, we'll come to it 212 | ``` 213 | 214 | ["Programming", "in", "Ruby", "Is", "Fun."] 215 | 216 | 217 | 218 | ```ruby 219 | word = "Programming" 220 | ``` 221 | 222 | 223 | 224 | 225 | "Programming" 226 | 227 | 228 | 229 | 230 | ```ruby 231 | word.downcase() #Get a word with lower case letters 232 | ``` 233 | 234 | 235 | 236 | 237 | "programming" 238 | 239 | 240 | 241 | 242 | ```ruby 243 | word.upcase() # get a word with upper case letters 244 | ``` 245 | 246 | 247 | 248 | 249 | "PROGRAMMING" 250 | 251 | 252 | 253 | 254 | ```ruby 255 | #This is a single line comment 256 | puts "comment" #this can come at the end of any expression 257 | =begin 258 | This is a multiline comment. 259 | =begin should start at the first character of the line to be it a comment, other wise it wont work 260 | 261 | =end 262 | ``` 263 | 264 | comment 265 | 266 | 267 | ## Naming Conventions 268 | 269 | For methods and variables we follow snake case like example_variable, example_function. 270 | For Classes we follow upper case letters, MyOwnClass. 271 | For functions which are risky to use, like reverse!, we add exclamation mark at the end. 272 | 273 | ## Function Chaining 274 | 275 | 276 | 277 | ```ruby 278 | my_string = "tushar tuteja" 279 | ``` 280 | 281 | 282 | 283 | 284 | "tushar tuteja" 285 | 286 | 287 | 288 | 289 | ```ruby 290 | my_string.reverse().upcase() # You can chain function calls one after the other 291 | ``` 292 | 293 | 294 | 295 | 296 | "AJETUT RAHSUT" 297 | 298 | 299 | 300 | ## String Formatter 301 | 302 | 303 | ```ruby 304 | my_name = "Tushar Tuteja" 305 | my_city = "Delhi" 306 | puts "Hi, I am #{my_name}, I belong to #{my_city}." #way to format complex strings. 307 | ``` 308 | 309 | Hi, I am Tushar Tuteja, I belong to Delhi. 310 | 311 | 312 | ## Control Flow 313 | 314 | 315 | ```ruby 316 | if true 317 | puts "true" 318 | else 319 | puts "false" 320 | end 321 | 322 | ``` 323 | 324 | true 325 | 326 | 327 | 328 | ```ruby 329 | false == "false" # this is false 330 | ``` 331 | 332 | 333 | 334 | 335 | false 336 | 337 | 338 | 339 | 340 | ```ruby 341 | false == nil # this is false 342 | ``` 343 | 344 | 345 | 346 | 347 | false 348 | 349 | 350 | 351 | 352 | ```ruby 353 | my_object = nil 354 | ``` 355 | 356 | 357 | ```ruby 358 | my_object == nil 359 | ``` 360 | 361 | 362 | 363 | 364 | true 365 | 366 | 367 | 368 | 369 | ```ruby 370 | my_object.nil? # this is the way to check nil 371 | ``` 372 | 373 | 374 | 375 | 376 | true 377 | 378 | 379 | 380 | Ruby has infinite true expressions, only two false expressions, one is false and the other is nil. So 0 would evaluate to true, this is different form C and C++ language. 381 | 382 | 383 | ```ruby 384 | if my_object 385 | puts "true" 386 | else 387 | puts "nil or false" 388 | end 389 | 390 | ``` 391 | 392 | nil or false 393 | 394 | 395 | 396 | ```ruby 397 | my_object = 0 398 | if my_object 399 | puts "this evaluates to true" 400 | else 401 | puts "should have been false if we were coding in C." 402 | end 403 | 404 | ``` 405 | 406 | this evaluates to true 407 | 408 | 409 | 410 | ```ruby 411 | first_condition = false 412 | second_condition = true 413 | 414 | if first_condition 415 | puts "first condition is true" 416 | elsif second_condition 417 | puts "second condition is true" 418 | elsif 0 > 3 419 | puts "won't happen" 420 | end 421 | 422 | ``` 423 | 424 | second condition is true 425 | 426 | 427 | 428 | ```ruby 429 | hungry = true 430 | ``` 431 | 432 | 433 | 434 | 435 | true 436 | 437 | 438 | 439 | 440 | ```ruby 441 | if not hungry 442 | puts "keep working" 443 | else 444 | puts "eat" 445 | end 446 | ``` 447 | 448 | eat 449 | 450 | 451 | Ruby gives us more than if else, it gives us unless else, which is more verbose than if not. Example 452 | 453 | 454 | ```ruby 455 | unless hungry 456 | puts "keep working" 457 | else 458 | puts "Eat" 459 | end 460 | 461 | ``` 462 | 463 | Eat 464 | 465 | 466 | ### Conditional Operators : == , >= ,<= ,!=, ! 467 | 468 | 469 | 470 | ```ruby 471 | 5 == '5' 472 | ``` 473 | 474 | 475 | 476 | 477 | false 478 | 479 | 480 | 481 | 482 | ```ruby 483 | 5*4 == 4*5 484 | ``` 485 | 486 | 487 | 488 | 489 | true 490 | 491 | 492 | 493 | 494 | ```ruby 495 | 4 < 7 496 | ``` 497 | 498 | 499 | 500 | 501 | true 502 | 503 | 504 | 505 | 506 | ```ruby 507 | 8 > 9 508 | 509 | ``` 510 | 511 | 512 | 513 | 514 | false 515 | 516 | 517 | 518 | 519 | ```ruby 520 | "tushar" > "tuteja" 521 | ``` 522 | 523 | 524 | 525 | 526 | false 527 | 528 | 529 | 530 | 531 | ```ruby 532 | 5 >= 5 533 | ``` 534 | 535 | 536 | 537 | 538 | true 539 | 540 | 541 | 542 | 543 | ```ruby 544 | 4 != 5 545 | ``` 546 | 547 | 548 | 549 | 550 | true 551 | 552 | 553 | 554 | 555 | ```ruby 556 | !true 557 | ``` 558 | 559 | 560 | 561 | 562 | false 563 | 564 | 565 | 566 | ### Composite Conditional Statements 567 | 568 | 569 | ```ruby 570 | work_done = true 571 | if work_done || hungry 572 | puts "eat" 573 | else 574 | puts "work" 575 | end 576 | ``` 577 | 578 | eat 579 | 580 | 581 | 582 | ```ruby 583 | puts false || true 584 | puts true || false 585 | puts true && true 586 | puts true && false 587 | puts false || false 588 | ``` 589 | 590 | true 591 | true 592 | true 593 | false 594 | false 595 | 596 | 597 | 598 | ```ruby 599 | if not false 600 | puts "it is true" 601 | else 602 | puts "it is false" 603 | end 604 | 605 | ``` 606 | 607 | it is true 608 | 609 | 610 | ## Loops 611 | 612 | 613 | ```ruby 614 | i = 0 615 | while i < 5 616 | puts "ruby is fun" 617 | i = i + 1 618 | end 619 | 620 | ``` 621 | 622 | ruby is fun 623 | ruby is fun 624 | ruby is fun 625 | ruby is fun 626 | ruby is fun 627 | 628 | 629 | 630 | ```ruby 631 | i = 5 632 | while i != 0 633 | puts "ruby is more verbose" 634 | i = i - 1 635 | end 636 | ``` 637 | 638 | ruby is more verbose 639 | ruby is more verbose 640 | ruby is more verbose 641 | ruby is more verbose 642 | ruby is more verbose 643 | 644 | 645 | 646 | ```ruby 647 | i = 5 648 | until i == 0 649 | puts "ruby is more verbose" 650 | i = i - 1 651 | end 652 | 653 | ``` 654 | 655 | ruby is more verbose 656 | ruby is more verbose 657 | ruby is more verbose 658 | ruby is more verbose 659 | ruby is more verbose 660 | 661 | 662 | 663 | ```ruby 664 | for i in 1..5 665 | puts "ruby has many ways to do the same thing." 666 | end 667 | ``` 668 | 669 | ruby has many ways to do the same thing. 670 | ruby has many ways to do the same thing. 671 | ruby has many ways to do the same thing. 672 | ruby has many ways to do the same thing. 673 | ruby has many ways to do the same thing. 674 | 675 | 676 | 677 | 678 | 679 | 1..5 680 | 681 | 682 | 683 | 684 | ```ruby 685 | for i in 1...5 686 | puts "..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5" 687 | end 688 | ``` 689 | 690 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 691 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 692 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 693 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 694 | 695 | 696 | 697 | 698 | 699 | 1...5 700 | 701 | 702 | 703 | 704 | ```ruby 705 | 5.times do 706 | puts "this needs to be done 5 times" 707 | end 708 | ``` 709 | 710 | this needs to be done 5 times 711 | this needs to be done 5 times 712 | this needs to be done 5 times 713 | this needs to be done 5 times 714 | this needs to be done 5 times 715 | 716 | 717 | 718 | 719 | 720 | 5 721 | 722 | 723 | 724 | 725 | ```ruby 726 | my_string = "Ruby is made for productivity." 727 | words = my_string.split(" ") 728 | words.each do |word| #would discuss more of this in arrays and hashes 729 | puts word.capitalize # This would capitalize every word 730 | end 731 | ``` 732 | 733 | Ruby 734 | Is 735 | Made 736 | For 737 | Productivity. 738 | 739 | 740 | 741 | 742 | 743 | ["Ruby", "is", "made", "for", "productivity."] 744 | 745 | 746 | 747 | ## Arrays 748 | 749 | 750 | ```ruby 751 | a = [] # this creates an empty array 752 | ``` 753 | 754 | 755 | 756 | 757 | [] 758 | 759 | 760 | 761 | Arrays are lists in ruby, means they doesn't need to be homegenous. 762 | 763 | 764 | ```ruby 765 | a = ["tushar", 5, [4,5,6]] 766 | ``` 767 | 768 | 769 | 770 | 771 | ["tushar", 5, [4, 5, 6]] 772 | 773 | 774 | 775 | Arrays are continuous data structures 776 | 777 | 778 | 779 | ```ruby 780 | a = [] 781 | a[45] = 5 782 | puts a 783 | ``` 784 | 785 | [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5] 786 | 787 | 788 | a[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero. 789 | 790 | 791 | ```ruby 792 | puts a[45] 793 | ``` 794 | 795 | 5 796 | 797 | 798 | 799 | ```ruby 800 | puts a[0] # Would print nothing 801 | ``` 802 | 803 | 804 | 805 | 806 | 807 | ```ruby 808 | puts a.length 809 | ``` 810 | 811 | 46 812 | 813 | 814 | ## Sets 815 | 816 | Sets are used to keep unique elements, no duplicates are allowed. 817 | 818 | 819 | 820 | ```ruby 821 | my_set = Set.new() # New Empty Set 822 | my_set.add(1) # Add one Element 823 | 824 | my_set.each do |v| 825 | puts v 826 | end 827 | 828 | my_set.add([1,2,3,4,5]) # adding [1,2,3,4,5] as a single element 829 | 830 | my_set.each do |v| 831 | puts v 832 | end 833 | 834 | 835 | ``` 836 | 837 | 1 838 | 1 839 | [1, 2, 3, 4, 5] 840 | 841 | 842 | 843 | 844 | 845 | # 846 | 847 | 848 | 849 | 850 | ```ruby 851 | a = [1,2,3,4,5,1,2,3,4,5] 852 | my_set = a.to_set # Easy way to create a set from array 853 | puts my_set.size 854 | ``` 855 | 856 | 5 857 | 858 | 859 | 860 | ```ruby 861 | my_set = Set.new([1,2,3,1,2,3]) # Easy way to create a set while initializing 862 | ``` 863 | 864 | 865 | 866 | 867 | # 868 | 869 | 870 | 871 | ## Hashes 872 | 873 | Hashes are smilar to javascript dictionaries, the only difference is that they are ordered. 874 | 875 | 876 | ```ruby 877 | my_hash = {} # creates a new Hash 878 | 879 | ``` 880 | 881 | 882 | 883 | 884 | {} 885 | 886 | 887 | 888 | 889 | ```ruby 890 | my_hash = Hash.new # creates a new Hash 891 | ``` 892 | 893 | 894 | 895 | 896 | {} 897 | 898 | 899 | 900 | 901 | ```ruby 902 | my_hash = {:name => "Tushar", :city => "Delhi"} # Creates a hash 903 | ``` 904 | 905 | 906 | 907 | 908 | {:name=>"Tushar", :city=>"Delhi"} 909 | 910 | 911 | 912 | 913 | ```ruby 914 | my_hash[:name] # to access :name from my_hash 915 | ``` 916 | 917 | 918 | 919 | 920 | "Tushar" 921 | 922 | 923 | 924 | :name and :city are actually symbols. they are used for faster accessing, we may use strings as well. 925 | 926 | 927 | 928 | ```ruby 929 | my_hash = {"name" => "tushar"} 930 | ``` 931 | 932 | 933 | 934 | 935 | {"name"=>"tushar"} 936 | 937 | 938 | 939 | 940 | ```ruby 941 | puts my_hash["name"] 942 | puts my_hash[:name] # This won't work, you need to remember that strings and symbols are different 943 | ``` 944 | 945 | tushar 946 | 947 | 948 | 949 | 950 | ```ruby 951 | my_hash = {:name => "Tushar", :city => "Delhi", :age => 25} 952 | ``` 953 | 954 | 955 | 956 | 957 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 958 | 959 | 960 | 961 | 962 | ```ruby 963 | my_hash.each do |key| # iterating over the keys 964 | puts key 965 | end 966 | 967 | ``` 968 | 969 | [:name, "Tushar"] 970 | [:city, "Delhi"] 971 | [:age, 25] 972 | 973 | 974 | 975 | 976 | 977 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 978 | 979 | 980 | 981 | If you look at the above code, the keys are ordered. 982 | 983 | 984 | ```ruby 985 | my_hash.each do |key,value| 986 | puts "#{key} : #{value}" 987 | end 988 | ``` 989 | 990 | name : Tushar 991 | city : Delhi 992 | age : 25 993 | 994 | 995 | 996 | 997 | 998 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 999 | 1000 | 1001 | 1002 | ## Methods 1003 | 1004 | 1005 | 1006 | ```ruby 1007 | def my_method 1008 | puts "hello method" 1009 | end 1010 | 1011 | ``` 1012 | 1013 | 1014 | 1015 | 1016 | :my_method 1017 | 1018 | 1019 | 1020 | 1021 | ```ruby 1022 | my_method() 1023 | ``` 1024 | 1025 | hello method 1026 | 1027 | 1028 | 1029 | ```ruby 1030 | my_method # parenthesis are optional in ruby, it makes the code more verbose 1031 | ``` 1032 | 1033 | hello method 1034 | 1035 | 1036 | 1037 | ```ruby 1038 | def is_odd number # parenthesis are optional 1039 | return number % 2 == 1 1040 | end 1041 | 1042 | is_odd 1 # parenthesis are optional 1043 | ``` 1044 | 1045 | 1046 | 1047 | 1048 | true 1049 | 1050 | 1051 | 1052 | 1053 | ```ruby 1054 | def is_even number 1055 | number % 2 == 0 #last expression in every function is a return statement by default. 1056 | end 1057 | 1058 | ``` 1059 | 1060 | 1061 | 1062 | 1063 | :is_even 1064 | 1065 | 1066 | 1067 | 1068 | ```ruby 1069 | is_even 2 1070 | ``` 1071 | 1072 | 1073 | 1074 | 1075 | true 1076 | 1077 | 1078 | 1079 | 1080 | ```ruby 1081 | def join_words words_array 1082 | result = "" 1083 | words_array.each do |word| 1084 | result = result + word + " " # String concatenation 1085 | end 1086 | result 1087 | end 1088 | 1089 | puts join_words ["Ruby","is","flexible"] 1090 | ``` 1091 | 1092 | Ruby is flexible 1093 | 1094 | 1095 | What is you want to pass a variable number of arguments and you don't want to pass an array. 1096 | 1097 | Voila, we have *args arguement. 1098 | 1099 | 1100 | ```ruby 1101 | def join_words *args 1102 | result = "" 1103 | args.each do |word| 1104 | result = result + word + " " # String concatenation 1105 | end 1106 | result 1107 | end 1108 | 1109 | puts join_words "Ruby","is","flexible" # This code is more verbose 1110 | puts join_words "Ruby","is","flexible",",", "This", "code" ,"is" ,"more" ,"verbose" 1111 | 1112 | ``` 1113 | 1114 | Ruby is flexible 1115 | Ruby is flexible , This code is more verbose 1116 | 1117 | 1118 | ## Classes 1119 | 1120 | 1121 | ```ruby 1122 | class MyClass # Defines a new Class 1123 | end 1124 | 1125 | ``` 1126 | 1127 | 1128 | ```ruby 1129 | my_class = MyClass.new # Creates a new instance of MyClass Object 1130 | ``` 1131 | 1132 | 1133 | 1134 | 1135 | # 1136 | 1137 | 1138 | 1139 | By default every class Inherits from Object class. When We Called MyClass.new , we called a constructor of Object class. 1140 | 1141 | 1142 | ```ruby 1143 | class MyClass 1144 | def initialize #Constructor method 1145 | puts "Constructor of MyClass is called" 1146 | end 1147 | end 1148 | 1149 | ``` 1150 | 1151 | 1152 | 1153 | 1154 | :initialize 1155 | 1156 | 1157 | 1158 | 1159 | ```ruby 1160 | my_class = MyClass.new 1161 | ``` 1162 | 1163 | Constructor of MyClass is called 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | # 1170 | 1171 | 1172 | 1173 | Classes contain methods and variables. 1174 | By Default Methods are of two types 1175 | * Instance Methods, that belongs to one instance of a class 1176 | * class methods, that belongs to one class. 1177 | Visibility of Methods is of three types 1178 | * private 1179 | * protected 1180 | * public, by default every method is public. 1181 | 1182 | Variables could be of three types 1183 | * Local Variable in a function 1184 | * Instance variables 1185 | * Class variables 1186 | 1187 | We'll discuss all of above in the following section 1188 | 1189 | 1190 | ```ruby 1191 | class Person 1192 | def initialize (name,age) 1193 | @name = name # this is how we declare instance variable, by having @ at the start 1194 | @age = age # same with age 1195 | end 1196 | 1197 | end 1198 | 1199 | ``` 1200 | 1201 | 1202 | 1203 | 1204 | :initialize 1205 | 1206 | 1207 | 1208 | 1209 | ```ruby 1210 | person = Person.new("Tushar",25) 1211 | ``` 1212 | 1213 | 1214 | 1215 | 1216 | # 1217 | 1218 | 1219 | 1220 | 1221 | ```ruby 1222 | person.public_methods 1223 | ``` 1224 | 1225 | 1226 | 1227 | 1228 | [:name, :name=, :details, :is_adult, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :display, :send, :object_id, :to_s, :method, :public_method, :singleton_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] 1229 | 1230 | 1231 | 1232 | We didn't define public_methods function, but it was called and it gave us some results. This is method that every object inherits from Object class. 1233 | 1234 | public_method functions gives us all the functions that could be called on that Object. In total Object class gives us 56 methods, which is a good thing and a bad thing. Bad thing as in now we have 56 names where our own name can collide. example, if we were to make SMS class, we would have send method, that would override the send method from Object class. 1235 | 1236 | 1237 | Lets add some more functionality to our Person class 1238 | 1239 | 1240 | ```ruby 1241 | class Person 1242 | def initialize (name,age) 1243 | @name = name # this is how we declare instance variable, by having @ at the start 1244 | @age = age # same with age 1245 | end 1246 | 1247 | def details # By default this method would be public 1248 | "Name: #{@name}" 1249 | end 1250 | 1251 | 1252 | 1253 | end 1254 | 1255 | ``` 1256 | 1257 | 1258 | 1259 | 1260 | :details 1261 | 1262 | 1263 | 1264 | 1265 | ```ruby 1266 | person = Person.new("Tushar",25) 1267 | ``` 1268 | 1269 | 1270 | 1271 | 1272 | # 1273 | 1274 | 1275 | 1276 | 1277 | ```ruby 1278 | person.details 1279 | ``` 1280 | 1281 | 1282 | 1283 | 1284 | "Name: Tushar" 1285 | 1286 | 1287 | 1288 | Every Instance variable in any object is a private variable. You need to write getter methods for them. I can not access name and age directly from the person Object. So lets add those methods. This is a good news, as we don't want to reveal the age of the person, but we should have a way to check if the person is adult or not. 1289 | 1290 | 1291 | ```ruby 1292 | class Person 1293 | def initialize (name,age) 1294 | @name = name # this is how we declare instance variable, by having @ at the start 1295 | @age = age # same with age 1296 | end 1297 | 1298 | def details # By default this method would be public 1299 | "Name: #{@name}" 1300 | end 1301 | 1302 | def name 1303 | @name 1304 | end 1305 | 1306 | def is_adult 1307 | @age > 18 1308 | end 1309 | 1310 | 1311 | 1312 | 1313 | end 1314 | 1315 | ``` 1316 | 1317 | 1318 | 1319 | 1320 | :is_adult 1321 | 1322 | 1323 | 1324 | 1325 | ```ruby 1326 | person = Person.new("Tushar",25) 1327 | puts person.name, person.is_adult 1328 | 1329 | ``` 1330 | 1331 | Tushar 1332 | true 1333 | 1334 | 1335 | We cannot change name or age once set, what if we want to change them, we need to add a setter method. Which is done in ruby as follows. 1336 | 1337 | 1338 | ```ruby 1339 | class Person 1340 | def initialize (name,age) 1341 | @name = name # this is how we declare instance variable, by having @ at the start 1342 | @age = age # same with age 1343 | end 1344 | 1345 | def details # By default this method would be public 1346 | "Name: #{@name}" 1347 | end 1348 | 1349 | def name 1350 | @name 1351 | end 1352 | 1353 | def name= name # we are defining a special method, with '=' in the signature 1354 | @name = name 1355 | end 1356 | 1357 | def is_adult 1358 | @age > 18 1359 | end 1360 | 1361 | 1362 | end 1363 | 1364 | ``` 1365 | 1366 | 1367 | 1368 | 1369 | :is_adult 1370 | 1371 | 1372 | 1373 | 1374 | ```ruby 1375 | person = Person.new("Tushar",25) 1376 | puts person.name 1377 | person.name = "Tuteja" 1378 | puts person.name 1379 | ``` 1380 | 1381 | Tushar 1382 | Tuteja 1383 | 1384 | 1385 | ### Private Methods in Ruby 1386 | 1387 | 1388 | ```ruby 1389 | class Person 1390 | def initialize (name,age) 1391 | @name = name # this is how we declare instance variable, by having @ at the start 1392 | @age = age # same with age 1393 | end 1394 | 1395 | def details # By default this method would be public 1396 | "Name: #{@name}" 1397 | end 1398 | 1399 | def name 1400 | @name 1401 | end 1402 | 1403 | def name= name # we are defining a special method, with '=' in the signature 1404 | @name = name 1405 | end 1406 | 1407 | def is_adult 1408 | @age > 18 1409 | end 1410 | 1411 | private # special keyword in ruby, after this everything we write would be private 1412 | def full_details # A private function 1413 | return @name, @age # Yes you can return more than one value, that would be returned as an array 1414 | end 1415 | 1416 | 1417 | end 1418 | ``` 1419 | 1420 | 1421 | 1422 | 1423 | :full_details 1424 | 1425 | 1426 | 1427 | 1428 | ```ruby 1429 | person = Person.new("Tushar",25) 1430 | person.full_details # Would throw an error 1431 | ``` 1432 | 1433 | 1434 | NoMethodError: private method `full_details' called for # 1435 | 1436 |
:1:in `
' 1437 | 1438 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1439 | 1440 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1441 | 1442 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1443 | 1444 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1445 | 1446 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1447 | 1448 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1449 | 1450 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1451 | 1452 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1453 | 1454 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1455 | 1456 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1457 | 1458 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1459 | 1460 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1461 | 1462 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1463 | 1464 | 1465 | ### Inheritance 1466 | 1467 | 1468 | ```ruby 1469 | class RichPerson < Person # RichPerson Inherits from Person 1470 | def print_details 1471 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1472 | end 1473 | 1474 | end 1475 | ``` 1476 | 1477 | 1478 | 1479 | 1480 | :print_details 1481 | 1482 | 1483 | 1484 | 1485 | ```ruby 1486 | rich_person = RichPerson.new("Tushar",25) 1487 | ``` 1488 | 1489 | 1490 | ArgumentError: wrong number of arguments (given 2, expected 4) 1491 | 1492 |
:5:in `initialize' 1493 | 1494 |
:in `new' 1495 | 1496 |
:in `
' 1497 | 1498 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1499 | 1500 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1501 | 1502 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1503 | 1504 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1505 | 1506 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1507 | 1508 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1509 | 1510 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1511 | 1512 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1513 | 1514 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1515 | 1516 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1517 | 1518 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1519 | 1520 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1521 | 1522 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1523 | 1524 | 1525 | 1526 | ```ruby 1527 | rich_person.print_details 1528 | ``` 1529 | 1530 | ["Tuteja", 25] 1531 | 1532 | 1533 | Child Class Objects can Call any private function of parent class. 1534 | 1535 | lets add some more functionality, lets suppose we want to add money in RichPerson class 1536 | 1537 | 1538 | ```ruby 1539 | class RichPerson < Person # RichPerson Inherits from Person 1540 | def initialize name, age, money 1541 | super name, age # special keyword in ruby to call constructor of parent class 1542 | @money = money 1543 | end 1544 | 1545 | def money 1546 | @money 1547 | end 1548 | 1549 | def print_details 1550 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1551 | end 1552 | 1553 | end 1554 | ``` 1555 | 1556 | 1557 | 1558 | 1559 | :print_details 1560 | 1561 | 1562 | 1563 | 1564 | ```ruby 1565 | rich_person = RichPerson.new("Bill Gates",25,1) 1566 | ``` 1567 | 1568 | 1569 | 1570 | 1571 | # 1572 | 1573 | 1574 | 1575 | 1576 | ```ruby 1577 | rich_person.money 1578 | ``` 1579 | 1580 | 1581 | 1582 | 1583 | 1 1584 | 1585 | 1586 | 1587 | It is a hassle to write getter method, like we wrote above for money and setter method like we wrote for name. 1588 | For this ruby gives us short cuts. 1589 | 1590 | 1591 | ```ruby 1592 | class RichPerson 1593 | attr_accessor :name, :money # Gives us getter and setter methods for name and money 1594 | attr_reader :age # gives us only getter 1595 | attr_writer :phone # gives us only setter 1596 | 1597 | def initialize(name, money,age,phone) 1598 | @name = name 1599 | @money = money 1600 | @age = age 1601 | @phone = phone 1602 | end 1603 | 1604 | end 1605 | 1606 | rich_person = RichPerson.new("Tushar",1,25,9999999999) 1607 | ``` 1608 | 1609 | 1610 | 1611 | 1612 | # 1613 | 1614 | 1615 | 1616 | 1617 | ```ruby 1618 | rich_person.age # attr_reader, created a method by the name age that returns @age 1619 | ``` 1620 | 1621 | 1622 | 1623 | 1624 | 25 1625 | 1626 | 1627 | 1628 | 1629 | ```ruby 1630 | puts rich_person.name # attr_accesor, created a method by the name that returns @name 1631 | rich_person.name = "Tuteja"# attr_accesor, created a method by the name= that sets @name for us 1632 | puts rich_person.name 1633 | ``` 1634 | 1635 | Tushar 1636 | Tuteja 1637 | 1638 | 1639 | Similarly we can only change phone number but cannot see it 1640 | 1641 | 1642 | 1643 | ```ruby 1644 | 1645 | ``` 1646 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | title: Rubytutorial 2 | description: 3 | google_analytics: 4 | show_downloads: true 5 | theme: jekyll-theme-slate 6 | 7 | gems: 8 | - jekyll-mentions 9 | -------------------------------------------------------------------------------- /_site/README.md: -------------------------------------------------------------------------------- 1 | 2 | # RubyTutorial 3 | 4 | We are using Jupyter Notebooks to make this tutorial. 5 | For those who don't know about Jupyter Notebooks, go [here](https://jupyter.org/). 6 | Below is a sample, about how notebooks work. 7 | 8 | ![alt text](https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/master/notebook_demo.gif "Without Smooth Scroll") 9 | 10 | ## Basics 11 | 12 | ### Data Types 13 | 14 | 15 | ```ruby 16 | my_num = 15 17 | my_string = "Tushar" 18 | my_bool = true 19 | 20 | puts my_num, my_string, my_bool 21 | ``` 22 | 23 | 15 24 | Tushar 25 | true 26 | 27 | 28 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 29 | 30 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 31 | 32 | 33 | ```ruby 34 | print my_num, my_string, my_bool 35 | ``` 36 | 37 | 15Tushartrue 38 | 39 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 40 | 41 | Everything is a an Object in Ruby, 42 | 43 | Everything! 44 | 45 | To find out class of a ruby object we can call a 'class' method on any object. 46 | 47 | 48 | ```ruby 49 | puts my_num.class() , my_string.class() , my_bool.class() 50 | ``` 51 | 52 | Fixnum 53 | String 54 | TrueClass 55 | 56 | 57 | ## Arithmetic 58 | 59 | 60 | Basic Arithmetic operators are '+','-','*','/' and '%'. 61 | 62 | 63 | ```ruby 64 | five = 5 65 | two = 2 66 | seven = 5 + 2 67 | 68 | ``` 69 | 70 | 71 | 72 | 73 | 7 74 | 75 | 76 | 77 | 78 | ```ruby 79 | ten = five * two 80 | ``` 81 | 82 | 83 | 84 | 85 | 10 86 | 87 | 88 | 89 | 90 | ```ruby 91 | one = five % two 92 | ``` 93 | 94 | 95 | 96 | 97 | 1 98 | 99 | 100 | 101 | 102 | ```ruby 103 | two = five / two 104 | ``` 105 | 106 | 107 | 108 | 109 | 2 110 | 111 | 112 | 113 | 114 | ```ruby 115 | three = five - two 116 | 117 | ``` 118 | 119 | 120 | 121 | 122 | 3 123 | 124 | 125 | 126 | ## Strings 127 | 128 | 129 | ```ruby 130 | my_string = "Programming in Ruby Is Fun." 131 | ``` 132 | 133 | 134 | 135 | 136 | "Programming in Ruby Is Fun." 137 | 138 | 139 | 140 | 141 | ```ruby 142 | my_string.length() 143 | ``` 144 | 145 | 146 | 147 | 148 | 27 149 | 150 | 151 | 152 | 153 | ```ruby 154 | my_string.reverse() 155 | ``` 156 | 157 | 158 | 159 | 160 | ".nuF sI ybuR ni gnimmargorP" 161 | 162 | 163 | 164 | 165 | ```ruby 166 | puts my_string 167 | ``` 168 | 169 | Programming in Ruby Is Fun. 170 | 171 | 172 | 173 | ```ruby 174 | my_string.reverse!() 175 | ``` 176 | 177 | 178 | 179 | 180 | ".nuF sI ybuR ni gnimmargorP" 181 | 182 | 183 | 184 | 185 | ```ruby 186 | puts my_string 187 | ``` 188 | 189 | .nuF sI ybuR ni gnimmargorP 190 | 191 | 192 | 193 | ```ruby 194 | my_string = "Programming in Ruby Is Fun." 195 | my_string.split(" ") 196 | ``` 197 | 198 | 199 | 200 | 201 | ["Programming", "in", "Ruby", "Is", "Fun."] 202 | 203 | 204 | 205 | 206 | ```ruby 207 | words = my_string.split(" ") 208 | puts words #Gives an array, we'll come to it 209 | ``` 210 | 211 | ["Programming", "in", "Ruby", "Is", "Fun."] 212 | 213 | 214 | 215 | ```ruby 216 | word = "Programming" 217 | ``` 218 | 219 | 220 | 221 | 222 | "Programming" 223 | 224 | 225 | 226 | 227 | ```ruby 228 | word.downcase() #Get a word with lower case letters 229 | ``` 230 | 231 | 232 | 233 | 234 | "programming" 235 | 236 | 237 | 238 | 239 | ```ruby 240 | word.upcase() # get a word with upper case letters 241 | ``` 242 | 243 | 244 | 245 | 246 | "PROGRAMMING" 247 | 248 | 249 | 250 | 251 | ```ruby 252 | #This is a single line comment 253 | puts "comment" #this can come at the end of any expression 254 | =begin 255 | This is a multiline comment. 256 | =begin should start at the first character of the line to be it a comment, other wise it wont work 257 | 258 | =end 259 | ``` 260 | 261 | comment 262 | 263 | 264 | ## Naming Conventions 265 | 266 | For methods and variables we follow snake case like example_variable, example_function. 267 | For Classes we follow upper case letters, MyOwnClass. 268 | For functions which are risky to use, like reverse!, we add exclamation mark at the end. 269 | 270 | ## Function Chaining 271 | 272 | 273 | 274 | ```ruby 275 | my_string = "tushar tuteja" 276 | ``` 277 | 278 | 279 | 280 | 281 | "tushar tuteja" 282 | 283 | 284 | 285 | 286 | ```ruby 287 | my_string.reverse().upcase() # You can chain function calls one after the other 288 | ``` 289 | 290 | 291 | 292 | 293 | "AJETUT RAHSUT" 294 | 295 | 296 | 297 | ## String Formatter 298 | 299 | 300 | ```ruby 301 | my_name = "Tushar Tuteja" 302 | my_city = "Delhi" 303 | puts "Hi, I am #{my_name}, I belong to #{my_city}." #way to format complex strings. 304 | ``` 305 | 306 | Hi, I am Tushar Tuteja, I belong to Delhi 307 | 308 | 309 | ## Control Flow 310 | 311 | 312 | ```ruby 313 | if true 314 | puts "true" 315 | else 316 | puts "false" 317 | end 318 | 319 | ``` 320 | 321 | true 322 | 323 | 324 | 325 | ```ruby 326 | false == "false" # this is false 327 | ``` 328 | 329 | 330 | 331 | 332 | false 333 | 334 | 335 | 336 | 337 | ```ruby 338 | false == nil # this is false 339 | ``` 340 | 341 | 342 | 343 | 344 | false 345 | 346 | 347 | 348 | 349 | ```ruby 350 | my_object = nil 351 | ``` 352 | 353 | 354 | ```ruby 355 | my_object == nil 356 | ``` 357 | 358 | 359 | 360 | 361 | true 362 | 363 | 364 | 365 | 366 | ```ruby 367 | my_object.nil? # this is the way to check nil 368 | ``` 369 | 370 | 371 | 372 | 373 | true 374 | 375 | 376 | 377 | Ruby has infinite true expressions, only two false expressions, one is false and the other is nil. So 0 would evaluate to true, this is different form C and C++ language. 378 | 379 | 380 | ```ruby 381 | if my_object 382 | puts "true" 383 | else 384 | puts "nil or false" 385 | end 386 | 387 | ``` 388 | 389 | nil or false 390 | 391 | 392 | 393 | ```ruby 394 | my_object = 0 395 | if my_object 396 | puts "this evaluates to true" 397 | else 398 | puts "should have been false if we were coding in C." 399 | end 400 | 401 | ``` 402 | 403 | this evaluates to true 404 | 405 | 406 | 407 | ```ruby 408 | first_condition = false 409 | second_condition = true 410 | 411 | if first_condition 412 | puts "first condition is true" 413 | elsif second_condition 414 | puts "second condition is true" 415 | elsif 0 > 3 416 | puts "won't happen" 417 | end 418 | 419 | ``` 420 | 421 | second condition is true 422 | 423 | 424 | 425 | ```ruby 426 | hungry = true 427 | ``` 428 | 429 | 430 | 431 | 432 | true 433 | 434 | 435 | 436 | 437 | ```ruby 438 | if not hungry 439 | puts "keep working" 440 | else 441 | puts "eat" 442 | end 443 | ``` 444 | 445 | eat 446 | 447 | 448 | Ruby gives us more than if else, it gives us unless else, which is more verbose than if not. Example 449 | 450 | 451 | ```ruby 452 | unless hungry 453 | puts "keep working" 454 | else 455 | puts "Eat" 456 | end 457 | 458 | ``` 459 | 460 | Eat 461 | 462 | 463 | ### Conditional Operators : == , >= ,<= ,!=, ! 464 | 465 | 466 | 467 | ```ruby 468 | 5 == '5' 469 | ``` 470 | 471 | 472 | 473 | 474 | false 475 | 476 | 477 | 478 | 479 | ```ruby 480 | 5*4 == 4*5 481 | ``` 482 | 483 | 484 | 485 | 486 | true 487 | 488 | 489 | 490 | 491 | ```ruby 492 | 4 < 7 493 | ``` 494 | 495 | 496 | 497 | 498 | true 499 | 500 | 501 | 502 | 503 | ```ruby 504 | 8 > 9 505 | 506 | ``` 507 | 508 | 509 | 510 | 511 | false 512 | 513 | 514 | 515 | 516 | ```ruby 517 | "tushar" > "tuteja" 518 | ``` 519 | 520 | 521 | 522 | 523 | false 524 | 525 | 526 | 527 | 528 | ```ruby 529 | 5 >= 5 530 | ``` 531 | 532 | 533 | 534 | 535 | true 536 | 537 | 538 | 539 | 540 | ```ruby 541 | 4 != 5 542 | ``` 543 | 544 | 545 | 546 | 547 | true 548 | 549 | 550 | 551 | 552 | ```ruby 553 | !true 554 | ``` 555 | 556 | 557 | 558 | 559 | false 560 | 561 | 562 | 563 | ### Composite Conditional Statements 564 | 565 | 566 | ```ruby 567 | work_done = true 568 | if work_done || hungry 569 | puts "eat" 570 | else 571 | puts "work" 572 | end 573 | ``` 574 | 575 | eat 576 | 577 | 578 | 579 | ```ruby 580 | puts false || true 581 | puts true || false 582 | puts true && true 583 | puts true && false 584 | puts false || false 585 | ``` 586 | 587 | true 588 | true 589 | true 590 | false 591 | false 592 | 593 | 594 | 595 | ```ruby 596 | if not false 597 | puts "it is true" 598 | else 599 | puts "it is false" 600 | end 601 | 602 | ``` 603 | 604 | it is true 605 | 606 | 607 | ## Loops 608 | 609 | 610 | ```ruby 611 | i = 0 612 | while i < 5 613 | puts "ruby is fun" 614 | i = i + 1 615 | end 616 | 617 | ``` 618 | 619 | ruby is fun 620 | ruby is fun 621 | ruby is fun 622 | ruby is fun 623 | ruby is fun 624 | 625 | 626 | 627 | ```ruby 628 | i = 5 629 | while i != 0 630 | puts "ruby is more verbose" 631 | i = i - 1 632 | end 633 | ``` 634 | 635 | ruby is more verbose 636 | ruby is more verbose 637 | ruby is more verbose 638 | ruby is more verbose 639 | ruby is more verbose 640 | 641 | 642 | 643 | ```ruby 644 | i = 5 645 | until i == 0 646 | puts "ruby is more verbose" 647 | i = i - 1 648 | end 649 | 650 | ``` 651 | 652 | ruby is more verbose 653 | ruby is more verbose 654 | ruby is more verbose 655 | ruby is more verbose 656 | ruby is more verbose 657 | 658 | 659 | 660 | ```ruby 661 | for i in 1..5 662 | puts "ruby has many ways to do the same thing." 663 | end 664 | ``` 665 | 666 | ruby has many ways to do the same thing. 667 | ruby has many ways to do the same thing. 668 | ruby has many ways to do the same thing. 669 | ruby has many ways to do the same thing. 670 | ruby has many ways to do the same thing. 671 | 672 | 673 | 674 | 675 | 676 | 1..5 677 | 678 | 679 | 680 | 681 | ```ruby 682 | for i in 1...5 683 | puts "..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5" 684 | end 685 | ``` 686 | 687 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 688 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 689 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 690 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 691 | 692 | 693 | 694 | 695 | 696 | 1...5 697 | 698 | 699 | 700 | 701 | ```ruby 702 | 5.times do 703 | puts "this needs to be done 5 times" 704 | end 705 | ``` 706 | 707 | this needs to be done 5 times 708 | this needs to be done 5 times 709 | this needs to be done 5 times 710 | this needs to be done 5 times 711 | this needs to be done 5 times 712 | 713 | 714 | 715 | 716 | 717 | 5 718 | 719 | 720 | 721 | 722 | ```ruby 723 | my_string = "Ruby is made for productivity." 724 | words = my_string.split(" ") 725 | words.each do |word| #would discuss more of this in arrays and hashes 726 | puts word.capitalize # This would capitalize every word 727 | end 728 | ``` 729 | 730 | Ruby 731 | Is 732 | Made 733 | For 734 | Productivity. 735 | 736 | 737 | 738 | 739 | 740 | ["Ruby", "is", "made", "for", "productivity."] 741 | 742 | 743 | 744 | ## Arrays 745 | 746 | 747 | ```ruby 748 | a = [] # this creates an empty array 749 | ``` 750 | 751 | 752 | 753 | 754 | [] 755 | 756 | 757 | 758 | Arrays are lists in ruby, means they doesn't need to be homegenous. 759 | 760 | 761 | ```ruby 762 | a = ["tushar", 5, [4,5,6]] 763 | ``` 764 | 765 | 766 | 767 | 768 | ["tushar", 5, [4, 5, 6]] 769 | 770 | 771 | 772 | Arrays are continuous data structures 773 | 774 | 775 | 776 | ```ruby 777 | a = [] 778 | a[45] = 5 779 | puts a 780 | ``` 781 | 782 | [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5] 783 | 784 | 785 | a[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero. 786 | 787 | 788 | ```ruby 789 | puts a[45] 790 | ``` 791 | 792 | 5 793 | 794 | 795 | 796 | ```ruby 797 | puts a[0] # Would print nothing 798 | ``` 799 | 800 | 801 | 802 | 803 | 804 | ```ruby 805 | puts a.length 806 | ``` 807 | 808 | 46 809 | 810 | 811 | ## Hashes 812 | 813 | Hashes are smilar to javascript dictionaries, the only difference is that they are ordered. 814 | 815 | 816 | ```ruby 817 | my_hash = {} # creates a new Hash 818 | 819 | ``` 820 | 821 | 822 | 823 | 824 | {} 825 | 826 | 827 | 828 | 829 | ```ruby 830 | my_hash = Hash.new # creates a new Hash 831 | ``` 832 | 833 | 834 | 835 | 836 | {} 837 | 838 | 839 | 840 | 841 | ```ruby 842 | my_hash = {:name => "Tushar", :city => "Delhi"} # Creates a hash 843 | ``` 844 | 845 | 846 | 847 | 848 | {:name=>"Tushar", :city=>"Delhi"} 849 | 850 | 851 | 852 | 853 | ```ruby 854 | my_hash[:name] # to access :name from my_hash 855 | ``` 856 | 857 | 858 | 859 | 860 | "Tushar" 861 | 862 | 863 | 864 | :name and :city are actually symbols. they are used for faster accessing, we may use strings as well. 865 | 866 | 867 | 868 | ```ruby 869 | my_hash = {"name" => "tushar"} 870 | ``` 871 | 872 | 873 | 874 | 875 | {"name"=>"tushar"} 876 | 877 | 878 | 879 | 880 | ```ruby 881 | puts my_hash["name"] 882 | puts my_hash[:name] # This won't work, you need to remember that strings and symbols are different 883 | ``` 884 | 885 | tushar 886 | 887 | 888 | 889 | 890 | ```ruby 891 | my_hash = {:name => "Tushar", :city => "Delhi", :age => 25} 892 | ``` 893 | 894 | 895 | 896 | 897 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 898 | 899 | 900 | 901 | 902 | ```ruby 903 | my_hash.each do |key| # iterating over the keys 904 | puts key 905 | end 906 | 907 | ``` 908 | 909 | [:name, "Tushar"] 910 | [:city, "Delhi"] 911 | [:age, 25] 912 | 913 | 914 | 915 | 916 | 917 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 918 | 919 | 920 | 921 | If you look at the above code, the keys are ordered. 922 | 923 | 924 | ```ruby 925 | my_hash.each do |key,value| 926 | puts "#{key} : #{value}" 927 | end 928 | ``` 929 | 930 | name : Tushar 931 | city : Delhi 932 | age : 25 933 | 934 | 935 | 936 | 937 | 938 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 939 | 940 | 941 | 942 | ## Methods 943 | 944 | 945 | 946 | ```ruby 947 | def my_method 948 | puts "hello method" 949 | end 950 | 951 | ``` 952 | 953 | 954 | 955 | 956 | :my_method 957 | 958 | 959 | 960 | 961 | ```ruby 962 | my_method() 963 | ``` 964 | 965 | hello method 966 | 967 | 968 | 969 | ```ruby 970 | my_method # parenthesis are optional in ruby, it makes the code more verbose 971 | ``` 972 | 973 | hello method 974 | 975 | 976 | 977 | ```ruby 978 | def is_odd number # parenthesis are optional 979 | return number % 2 == 1 980 | end 981 | 982 | is_odd 1 # parenthesis are optional 983 | ``` 984 | 985 | 986 | 987 | 988 | true 989 | 990 | 991 | 992 | 993 | ```ruby 994 | def is_even number 995 | number % 2 == 0 #last expression in every function is a return statement by default. 996 | end 997 | 998 | ``` 999 | 1000 | 1001 | 1002 | 1003 | :is_even 1004 | 1005 | 1006 | 1007 | 1008 | ```ruby 1009 | is_even 2 1010 | ``` 1011 | 1012 | 1013 | 1014 | 1015 | true 1016 | 1017 | 1018 | 1019 | 1020 | ```ruby 1021 | def join_words words_array 1022 | result = "" 1023 | words_array.each do |word| 1024 | result = result + word + " " # String concatenation 1025 | end 1026 | result 1027 | end 1028 | 1029 | puts join_words ["Ruby","is","flexible"] 1030 | ``` 1031 | 1032 | Ruby is flexible 1033 | 1034 | 1035 | What is you want to pass a variable number of arguments and you don't want to pass an array. 1036 | 1037 | Voila, we have *args arguement. 1038 | 1039 | 1040 | ```ruby 1041 | def join_words *args 1042 | result = "" 1043 | args.each do |word| 1044 | result = result + word + " " # String concatenation 1045 | end 1046 | result 1047 | end 1048 | 1049 | puts join_words "Ruby","is","flexible" # This code is more verbose 1050 | puts join_words "Ruby","is","flexible",",", "This", "code" ,"is" ,"more" ,"verbose" 1051 | 1052 | ``` 1053 | 1054 | Ruby is flexible 1055 | Ruby is flexible , This code is more verbose 1056 | 1057 | Some more content -------------------------------------------------------------------------------- /_site/images/bg_hr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/_site/images/bg_hr.png -------------------------------------------------------------------------------- /_site/images/blacktocat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/_site/images/blacktocat.png -------------------------------------------------------------------------------- /_site/images/icon_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/_site/images/icon_download.png -------------------------------------------------------------------------------- /_site/images/sprite_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/_site/images/sprite_download.png -------------------------------------------------------------------------------- /_site/index.md: -------------------------------------------------------------------------------- 1 | 2 | # RubyTutorial 3 | 4 | We are using Jupyter Notebooks to make this tutorial. 5 | For those who don't know about Jupyter Notebooks, go [here](https://jupyter.org/). 6 | Below is a sample, about how notebooks work. 7 | 8 | ![alt text](https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/master/notebook_demo.gif "Without Smooth Scroll") 9 | 10 | ## Basics 11 | 12 | ### Data Types 13 | 14 | 15 | ```ruby 16 | my_num = 15 17 | my_string = "Tushar" 18 | my_bool = true 19 | my_own_string = "tuteja" 20 | 21 | puts my_num, my_string, my_bool 22 | puts my_own_string 23 | ``` 24 | 25 | 15 26 | Tushar 27 | true 28 | tuteja 29 | 30 | 31 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 32 | 33 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 34 | 35 | 36 | ```ruby 37 | print my_num, my_string, my_bool 38 | ``` 39 | 40 | 15Tushartrue 41 | 42 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 43 | 44 | Everything is a an Object in Ruby, 45 | 46 | Everything! 47 | 48 | To find out class of a ruby object we can call a 'class' method on any object. 49 | 50 | 51 | ```ruby 52 | puts my_num.class() , my_string.class() , my_bool.class() 53 | ``` 54 | 55 | Fixnum 56 | String 57 | TrueClass 58 | 59 | 60 | ## Arithmetic 61 | 62 | 63 | Basic Arithmetic operators are '+','-','*','/' and '%'. 64 | 65 | 66 | ```ruby 67 | five = 5 68 | two = 2 69 | seven = 5 + 2 70 | 71 | ``` 72 | 73 | 74 | 75 | 76 | 7 77 | 78 | 79 | 80 | 81 | ```ruby 82 | ten = five * two 83 | ``` 84 | 85 | 86 | 87 | 88 | 10 89 | 90 | 91 | 92 | 93 | ```ruby 94 | one = five % two 95 | ``` 96 | 97 | 98 | 99 | 100 | 1 101 | 102 | 103 | 104 | 105 | ```ruby 106 | two = five / two 107 | ``` 108 | 109 | 110 | 111 | 112 | 2 113 | 114 | 115 | 116 | 117 | ```ruby 118 | three = five - two 119 | 120 | ``` 121 | 122 | 123 | 124 | 125 | 3 126 | 127 | 128 | 129 | ## Strings 130 | 131 | 132 | ```ruby 133 | my_string = "Programming in Ruby Is Fun." 134 | ``` 135 | 136 | 137 | 138 | 139 | "Programming in Ruby Is Fun." 140 | 141 | 142 | 143 | 144 | ```ruby 145 | my_string.length() 146 | ``` 147 | 148 | 149 | 150 | 151 | 27 152 | 153 | 154 | 155 | 156 | ```ruby 157 | my_string.reverse() 158 | ``` 159 | 160 | 161 | 162 | 163 | ".nuF sI ybuR ni gnimmargorP" 164 | 165 | 166 | 167 | 168 | ```ruby 169 | puts my_string 170 | ``` 171 | 172 | Programming in Ruby Is Fun. 173 | 174 | 175 | 176 | ```ruby 177 | my_string.reverse!() 178 | ``` 179 | 180 | 181 | 182 | 183 | ".nuF sI ybuR ni gnimmargorP" 184 | 185 | 186 | 187 | 188 | ```ruby 189 | puts my_string 190 | ``` 191 | 192 | .nuF sI ybuR ni gnimmargorP 193 | 194 | 195 | 196 | ```ruby 197 | my_string = "Programming in Ruby Is Fun." 198 | my_string.split(" ") 199 | ``` 200 | 201 | 202 | 203 | 204 | ["Programming", "in", "Ruby", "Is", "Fun."] 205 | 206 | 207 | 208 | 209 | ```ruby 210 | words = my_string.split(" ") 211 | puts words #Gives an array, we'll come to it 212 | ``` 213 | 214 | ["Programming", "in", "Ruby", "Is", "Fun."] 215 | 216 | 217 | 218 | ```ruby 219 | word = "Programming" 220 | ``` 221 | 222 | 223 | 224 | 225 | "Programming" 226 | 227 | 228 | 229 | 230 | ```ruby 231 | word.downcase() #Get a word with lower case letters 232 | ``` 233 | 234 | 235 | 236 | 237 | "programming" 238 | 239 | 240 | 241 | 242 | ```ruby 243 | word.upcase() # get a word with upper case letters 244 | ``` 245 | 246 | 247 | 248 | 249 | "PROGRAMMING" 250 | 251 | 252 | 253 | 254 | ```ruby 255 | #This is a single line comment 256 | puts "comment" #this can come at the end of any expression 257 | =begin 258 | This is a multiline comment. 259 | =begin should start at the first character of the line to be it a comment, other wise it wont work 260 | 261 | =end 262 | ``` 263 | 264 | comment 265 | 266 | 267 | ## Naming Conventions 268 | 269 | For methods and variables we follow snake case like example_variable, example_function. 270 | For Classes we follow upper case letters, MyOwnClass. 271 | For functions which are risky to use, like reverse!, we add exclamation mark at the end. 272 | 273 | ## Function Chaining 274 | 275 | 276 | 277 | ```ruby 278 | my_string = "tushar tuteja" 279 | ``` 280 | 281 | 282 | 283 | 284 | "tushar tuteja" 285 | 286 | 287 | 288 | 289 | ```ruby 290 | my_string.reverse().upcase() # You can chain function calls one after the other 291 | ``` 292 | 293 | 294 | 295 | 296 | "AJETUT RAHSUT" 297 | 298 | 299 | 300 | ## String Formatter 301 | 302 | 303 | ```ruby 304 | my_name = "Tushar Tuteja" 305 | my_city = "Delhi" 306 | puts "Hi, I am #{my_name}, I belong to #{my_city}." #way to format complex strings. 307 | ``` 308 | 309 | Hi, I am Tushar Tuteja, I belong to Delhi. 310 | 311 | 312 | ## Control Flow 313 | 314 | 315 | ```ruby 316 | if true 317 | puts "true" 318 | else 319 | puts "false" 320 | end 321 | 322 | ``` 323 | 324 | true 325 | 326 | 327 | 328 | ```ruby 329 | false == "false" # this is false 330 | ``` 331 | 332 | 333 | 334 | 335 | false 336 | 337 | 338 | 339 | 340 | ```ruby 341 | false == nil # this is false 342 | ``` 343 | 344 | 345 | 346 | 347 | false 348 | 349 | 350 | 351 | 352 | ```ruby 353 | my_object = nil 354 | ``` 355 | 356 | 357 | ```ruby 358 | my_object == nil 359 | ``` 360 | 361 | 362 | 363 | 364 | true 365 | 366 | 367 | 368 | 369 | ```ruby 370 | my_object.nil? # this is the way to check nil 371 | ``` 372 | 373 | 374 | 375 | 376 | true 377 | 378 | 379 | 380 | Ruby has infinite true expressions, only two false expressions, one is false and the other is nil. So 0 would evaluate to true, this is different form C and C++ language. 381 | 382 | 383 | ```ruby 384 | if my_object 385 | puts "true" 386 | else 387 | puts "nil or false" 388 | end 389 | 390 | ``` 391 | 392 | nil or false 393 | 394 | 395 | 396 | ```ruby 397 | my_object = 0 398 | if my_object 399 | puts "this evaluates to true" 400 | else 401 | puts "should have been false if we were coding in C." 402 | end 403 | 404 | ``` 405 | 406 | this evaluates to true 407 | 408 | 409 | 410 | ```ruby 411 | first_condition = false 412 | second_condition = true 413 | 414 | if first_condition 415 | puts "first condition is true" 416 | elsif second_condition 417 | puts "second condition is true" 418 | elsif 0 > 3 419 | puts "won't happen" 420 | end 421 | 422 | ``` 423 | 424 | second condition is true 425 | 426 | 427 | 428 | ```ruby 429 | hungry = true 430 | ``` 431 | 432 | 433 | 434 | 435 | true 436 | 437 | 438 | 439 | 440 | ```ruby 441 | if not hungry 442 | puts "keep working" 443 | else 444 | puts "eat" 445 | end 446 | ``` 447 | 448 | eat 449 | 450 | 451 | Ruby gives us more than if else, it gives us unless else, which is more verbose than if not. Example 452 | 453 | 454 | ```ruby 455 | unless hungry 456 | puts "keep working" 457 | else 458 | puts "Eat" 459 | end 460 | 461 | ``` 462 | 463 | Eat 464 | 465 | 466 | ### Conditional Operators : == , >= ,<= ,!=, ! 467 | 468 | 469 | 470 | ```ruby 471 | 5 == '5' 472 | ``` 473 | 474 | 475 | 476 | 477 | false 478 | 479 | 480 | 481 | 482 | ```ruby 483 | 5*4 == 4*5 484 | ``` 485 | 486 | 487 | 488 | 489 | true 490 | 491 | 492 | 493 | 494 | ```ruby 495 | 4 < 7 496 | ``` 497 | 498 | 499 | 500 | 501 | true 502 | 503 | 504 | 505 | 506 | ```ruby 507 | 8 > 9 508 | 509 | ``` 510 | 511 | 512 | 513 | 514 | false 515 | 516 | 517 | 518 | 519 | ```ruby 520 | "tushar" > "tuteja" 521 | ``` 522 | 523 | 524 | 525 | 526 | false 527 | 528 | 529 | 530 | 531 | ```ruby 532 | 5 >= 5 533 | ``` 534 | 535 | 536 | 537 | 538 | true 539 | 540 | 541 | 542 | 543 | ```ruby 544 | 4 != 5 545 | ``` 546 | 547 | 548 | 549 | 550 | true 551 | 552 | 553 | 554 | 555 | ```ruby 556 | !true 557 | ``` 558 | 559 | 560 | 561 | 562 | false 563 | 564 | 565 | 566 | ### Composite Conditional Statements 567 | 568 | 569 | ```ruby 570 | work_done = true 571 | if work_done || hungry 572 | puts "eat" 573 | else 574 | puts "work" 575 | end 576 | ``` 577 | 578 | eat 579 | 580 | 581 | 582 | ```ruby 583 | puts false || true 584 | puts true || false 585 | puts true && true 586 | puts true && false 587 | puts false || false 588 | ``` 589 | 590 | true 591 | true 592 | true 593 | false 594 | false 595 | 596 | 597 | 598 | ```ruby 599 | if not false 600 | puts "it is true" 601 | else 602 | puts "it is false" 603 | end 604 | 605 | ``` 606 | 607 | it is true 608 | 609 | 610 | ## Loops 611 | 612 | 613 | ```ruby 614 | i = 0 615 | while i < 5 616 | puts "ruby is fun" 617 | i = i + 1 618 | end 619 | 620 | ``` 621 | 622 | ruby is fun 623 | ruby is fun 624 | ruby is fun 625 | ruby is fun 626 | ruby is fun 627 | 628 | 629 | 630 | ```ruby 631 | i = 5 632 | while i != 0 633 | puts "ruby is more verbose" 634 | i = i - 1 635 | end 636 | ``` 637 | 638 | ruby is more verbose 639 | ruby is more verbose 640 | ruby is more verbose 641 | ruby is more verbose 642 | ruby is more verbose 643 | 644 | 645 | 646 | ```ruby 647 | i = 5 648 | until i == 0 649 | puts "ruby is more verbose" 650 | i = i - 1 651 | end 652 | 653 | ``` 654 | 655 | ruby is more verbose 656 | ruby is more verbose 657 | ruby is more verbose 658 | ruby is more verbose 659 | ruby is more verbose 660 | 661 | 662 | 663 | ```ruby 664 | for i in 1..5 665 | puts "ruby has many ways to do the same thing." 666 | end 667 | ``` 668 | 669 | ruby has many ways to do the same thing. 670 | ruby has many ways to do the same thing. 671 | ruby has many ways to do the same thing. 672 | ruby has many ways to do the same thing. 673 | ruby has many ways to do the same thing. 674 | 675 | 676 | 677 | 678 | 679 | 1..5 680 | 681 | 682 | 683 | 684 | ```ruby 685 | for i in 1...5 686 | puts "..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5" 687 | end 688 | ``` 689 | 690 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 691 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 692 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 693 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 694 | 695 | 696 | 697 | 698 | 699 | 1...5 700 | 701 | 702 | 703 | 704 | ```ruby 705 | 5.times do 706 | puts "this needs to be done 5 times" 707 | end 708 | ``` 709 | 710 | this needs to be done 5 times 711 | this needs to be done 5 times 712 | this needs to be done 5 times 713 | this needs to be done 5 times 714 | this needs to be done 5 times 715 | 716 | 717 | 718 | 719 | 720 | 5 721 | 722 | 723 | 724 | 725 | ```ruby 726 | my_string = "Ruby is made for productivity." 727 | words = my_string.split(" ") 728 | words.each do |word| #would discuss more of this in arrays and hashes 729 | puts word.capitalize # This would capitalize every word 730 | end 731 | ``` 732 | 733 | Ruby 734 | Is 735 | Made 736 | For 737 | Productivity. 738 | 739 | 740 | 741 | 742 | 743 | ["Ruby", "is", "made", "for", "productivity."] 744 | 745 | 746 | 747 | ## Arrays 748 | 749 | 750 | ```ruby 751 | a = [] # this creates an empty array 752 | ``` 753 | 754 | 755 | 756 | 757 | [] 758 | 759 | 760 | 761 | Arrays are lists in ruby, means they doesn't need to be homegenous. 762 | 763 | 764 | ```ruby 765 | a = ["tushar", 5, [4,5,6]] 766 | ``` 767 | 768 | 769 | 770 | 771 | ["tushar", 5, [4, 5, 6]] 772 | 773 | 774 | 775 | Arrays are continuous data structures 776 | 777 | 778 | 779 | ```ruby 780 | a = [] 781 | a[45] = 5 782 | puts a 783 | ``` 784 | 785 | [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5] 786 | 787 | 788 | a[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero. 789 | 790 | 791 | ```ruby 792 | puts a[45] 793 | ``` 794 | 795 | 5 796 | 797 | 798 | 799 | ```ruby 800 | puts a[0] # Would print nothing 801 | ``` 802 | 803 | 804 | 805 | 806 | 807 | ```ruby 808 | puts a.length 809 | ``` 810 | 811 | 46 812 | 813 | 814 | ## Sets 815 | 816 | Sets are used to keep unique elements, no duplicates are allowed. 817 | 818 | 819 | 820 | ```ruby 821 | my_set = Set.new() # New Empty Set 822 | my_set.add(1) # Add one Element 823 | 824 | my_set.each do |v| 825 | puts v 826 | end 827 | 828 | my_set.add([1,2,3,4,5]) # adding [1,2,3,4,5] as a single element 829 | 830 | my_set.each do |v| 831 | puts v 832 | end 833 | 834 | 835 | ``` 836 | 837 | 1 838 | 1 839 | [1, 2, 3, 4, 5] 840 | 841 | 842 | 843 | 844 | 845 | # 846 | 847 | 848 | 849 | 850 | ```ruby 851 | a = [1,2,3,4,5,1,2,3,4,5] 852 | my_set = a.to_set # Easy way to create a set from array 853 | puts my_set.size 854 | ``` 855 | 856 | 5 857 | 858 | 859 | 860 | ```ruby 861 | my_set = Set.new([1,2,3,1,2,3]) # Easy way to create a set while initializing 862 | ``` 863 | 864 | 865 | 866 | 867 | # 868 | 869 | 870 | 871 | ## Hashes 872 | 873 | Hashes are smilar to javascript dictionaries, the only difference is that they are ordered. 874 | 875 | 876 | ```ruby 877 | my_hash = {} # creates a new Hash 878 | 879 | ``` 880 | 881 | 882 | 883 | 884 | {} 885 | 886 | 887 | 888 | 889 | ```ruby 890 | my_hash = Hash.new # creates a new Hash 891 | ``` 892 | 893 | 894 | 895 | 896 | {} 897 | 898 | 899 | 900 | 901 | ```ruby 902 | my_hash = {:name => "Tushar", :city => "Delhi"} # Creates a hash 903 | ``` 904 | 905 | 906 | 907 | 908 | {:name=>"Tushar", :city=>"Delhi"} 909 | 910 | 911 | 912 | 913 | ```ruby 914 | my_hash[:name] # to access :name from my_hash 915 | ``` 916 | 917 | 918 | 919 | 920 | "Tushar" 921 | 922 | 923 | 924 | :name and :city are actually symbols. they are used for faster accessing, we may use strings as well. 925 | 926 | 927 | 928 | ```ruby 929 | my_hash = {"name" => "tushar"} 930 | ``` 931 | 932 | 933 | 934 | 935 | {"name"=>"tushar"} 936 | 937 | 938 | 939 | 940 | ```ruby 941 | puts my_hash["name"] 942 | puts my_hash[:name] # This won't work, you need to remember that strings and symbols are different 943 | ``` 944 | 945 | tushar 946 | 947 | 948 | 949 | 950 | ```ruby 951 | my_hash = {:name => "Tushar", :city => "Delhi", :age => 25} 952 | ``` 953 | 954 | 955 | 956 | 957 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 958 | 959 | 960 | 961 | 962 | ```ruby 963 | my_hash.each do |key| # iterating over the keys 964 | puts key 965 | end 966 | 967 | ``` 968 | 969 | [:name, "Tushar"] 970 | [:city, "Delhi"] 971 | [:age, 25] 972 | 973 | 974 | 975 | 976 | 977 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 978 | 979 | 980 | 981 | If you look at the above code, the keys are ordered. 982 | 983 | 984 | ```ruby 985 | my_hash.each do |key,value| 986 | puts "#{key} : #{value}" 987 | end 988 | ``` 989 | 990 | name : Tushar 991 | city : Delhi 992 | age : 25 993 | 994 | 995 | 996 | 997 | 998 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 999 | 1000 | 1001 | 1002 | ## Methods 1003 | 1004 | 1005 | 1006 | ```ruby 1007 | def my_method 1008 | puts "hello method" 1009 | end 1010 | 1011 | ``` 1012 | 1013 | 1014 | 1015 | 1016 | :my_method 1017 | 1018 | 1019 | 1020 | 1021 | ```ruby 1022 | my_method() 1023 | ``` 1024 | 1025 | hello method 1026 | 1027 | 1028 | 1029 | ```ruby 1030 | my_method # parenthesis are optional in ruby, it makes the code more verbose 1031 | ``` 1032 | 1033 | hello method 1034 | 1035 | 1036 | 1037 | ```ruby 1038 | def is_odd number # parenthesis are optional 1039 | return number % 2 == 1 1040 | end 1041 | 1042 | is_odd 1 # parenthesis are optional 1043 | ``` 1044 | 1045 | 1046 | 1047 | 1048 | true 1049 | 1050 | 1051 | 1052 | 1053 | ```ruby 1054 | def is_even number 1055 | number % 2 == 0 #last expression in every function is a return statement by default. 1056 | end 1057 | 1058 | ``` 1059 | 1060 | 1061 | 1062 | 1063 | :is_even 1064 | 1065 | 1066 | 1067 | 1068 | ```ruby 1069 | is_even 2 1070 | ``` 1071 | 1072 | 1073 | 1074 | 1075 | true 1076 | 1077 | 1078 | 1079 | 1080 | ```ruby 1081 | def join_words words_array 1082 | result = "" 1083 | words_array.each do |word| 1084 | result = result + word + " " # String concatenation 1085 | end 1086 | result 1087 | end 1088 | 1089 | puts join_words ["Ruby","is","flexible"] 1090 | ``` 1091 | 1092 | Ruby is flexible 1093 | 1094 | 1095 | What is you want to pass a variable number of arguments and you don't want to pass an array. 1096 | 1097 | Voila, we have *args arguement. 1098 | 1099 | 1100 | ```ruby 1101 | def join_words *args 1102 | result = "" 1103 | args.each do |word| 1104 | result = result + word + " " # String concatenation 1105 | end 1106 | result 1107 | end 1108 | 1109 | puts join_words "Ruby","is","flexible" # This code is more verbose 1110 | puts join_words "Ruby","is","flexible",",", "This", "code" ,"is" ,"more" ,"verbose" 1111 | 1112 | ``` 1113 | 1114 | Ruby is flexible 1115 | Ruby is flexible , This code is more verbose 1116 | 1117 | 1118 | ## Classes 1119 | 1120 | 1121 | ```ruby 1122 | class MyClass # Defines a new Class 1123 | end 1124 | 1125 | ``` 1126 | 1127 | 1128 | ```ruby 1129 | my_class = MyClass.new # Creates a new instance of MyClass Object 1130 | ``` 1131 | 1132 | 1133 | 1134 | 1135 | # 1136 | 1137 | 1138 | 1139 | By default every class Inherits from Object class. When We Called MyClass.new , we called a constructor of Object class. 1140 | 1141 | 1142 | ```ruby 1143 | class MyClass 1144 | def initialize #Constructor method 1145 | puts "Constructor of MyClass is called" 1146 | end 1147 | end 1148 | 1149 | ``` 1150 | 1151 | 1152 | 1153 | 1154 | :initialize 1155 | 1156 | 1157 | 1158 | 1159 | ```ruby 1160 | my_class = MyClass.new 1161 | ``` 1162 | 1163 | Constructor of MyClass is called 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | # 1170 | 1171 | 1172 | 1173 | Classes contain methods and variables. 1174 | By Default Methods are of two types 1175 | * Instance Methods, that belongs to one instance of a class 1176 | * class methods, that belongs to one class. 1177 | Visibility of Methods is of three types 1178 | * private 1179 | * protected 1180 | * public, by default every method is public. 1181 | 1182 | Variables could be of three types 1183 | * Local Variable in a function 1184 | * Instance variables 1185 | * Class variables 1186 | 1187 | We'll discuss all of above in the following section 1188 | 1189 | 1190 | ```ruby 1191 | class Person 1192 | def initialize (name,age) 1193 | @name = name # this is how we declare instance variable, by having @ at the start 1194 | @age = age # same with age 1195 | end 1196 | 1197 | end 1198 | 1199 | ``` 1200 | 1201 | 1202 | 1203 | 1204 | :initialize 1205 | 1206 | 1207 | 1208 | 1209 | ```ruby 1210 | person = Person.new("Tushar",25) 1211 | ``` 1212 | 1213 | 1214 | 1215 | 1216 | # 1217 | 1218 | 1219 | 1220 | 1221 | ```ruby 1222 | person.public_methods 1223 | ``` 1224 | 1225 | 1226 | 1227 | 1228 | [:name, :name=, :details, :is_adult, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :display, :send, :object_id, :to_s, :method, :public_method, :singleton_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] 1229 | 1230 | 1231 | 1232 | We didn't define public_methods function, but it was called and it gave us some results. This is method that every object inherits from Object class. 1233 | 1234 | public_method functions gives us all the functions that could be called on that Object. In total Object class gives us 56 methods, which is a good thing and a bad thing. Bad thing as in now we have 56 names where our own name can collide. example, if we were to make SMS class, we would have send method, that would override the send method from Object class. 1235 | 1236 | 1237 | Lets add some more functionality to our Person class 1238 | 1239 | 1240 | ```ruby 1241 | class Person 1242 | def initialize (name,age) 1243 | @name = name # this is how we declare instance variable, by having @ at the start 1244 | @age = age # same with age 1245 | end 1246 | 1247 | def details # By default this method would be public 1248 | "Name: #{@name}" 1249 | end 1250 | 1251 | 1252 | 1253 | end 1254 | 1255 | ``` 1256 | 1257 | 1258 | 1259 | 1260 | :details 1261 | 1262 | 1263 | 1264 | 1265 | ```ruby 1266 | person = Person.new("Tushar",25) 1267 | ``` 1268 | 1269 | 1270 | 1271 | 1272 | # 1273 | 1274 | 1275 | 1276 | 1277 | ```ruby 1278 | person.details 1279 | ``` 1280 | 1281 | 1282 | 1283 | 1284 | "Name: Tushar" 1285 | 1286 | 1287 | 1288 | Every Instance variable in any object is a private variable. You need to write getter methods for them. I can not access name and age directly from the person Object. So lets add those methods. This is a good news, as we don't want to reveal the age of the person, but we should have a way to check if the person is adult or not. 1289 | 1290 | 1291 | ```ruby 1292 | class Person 1293 | def initialize (name,age) 1294 | @name = name # this is how we declare instance variable, by having @ at the start 1295 | @age = age # same with age 1296 | end 1297 | 1298 | def details # By default this method would be public 1299 | "Name: #{@name}" 1300 | end 1301 | 1302 | def name 1303 | @name 1304 | end 1305 | 1306 | def is_adult 1307 | @age > 18 1308 | end 1309 | 1310 | 1311 | 1312 | 1313 | end 1314 | 1315 | ``` 1316 | 1317 | 1318 | 1319 | 1320 | :is_adult 1321 | 1322 | 1323 | 1324 | 1325 | ```ruby 1326 | person = Person.new("Tushar",25) 1327 | puts person.name, person.is_adult 1328 | 1329 | ``` 1330 | 1331 | Tushar 1332 | true 1333 | 1334 | 1335 | We cannot change name or age once set, what if we want to change them, we need to add a setter method. Which is done in ruby as follows. 1336 | 1337 | 1338 | ```ruby 1339 | class Person 1340 | def initialize (name,age) 1341 | @name = name # this is how we declare instance variable, by having @ at the start 1342 | @age = age # same with age 1343 | end 1344 | 1345 | def details # By default this method would be public 1346 | "Name: #{@name}" 1347 | end 1348 | 1349 | def name 1350 | @name 1351 | end 1352 | 1353 | def name= name # we are defining a special method, with '=' in the signature 1354 | @name = name 1355 | end 1356 | 1357 | def is_adult 1358 | @age > 18 1359 | end 1360 | 1361 | 1362 | end 1363 | 1364 | ``` 1365 | 1366 | 1367 | 1368 | 1369 | :is_adult 1370 | 1371 | 1372 | 1373 | 1374 | ```ruby 1375 | person = Person.new("Tushar",25) 1376 | puts person.name 1377 | person.name = "Tuteja" 1378 | puts person.name 1379 | ``` 1380 | 1381 | Tushar 1382 | Tuteja 1383 | 1384 | 1385 | ### Private Methods in Ruby 1386 | 1387 | 1388 | ```ruby 1389 | class Person 1390 | def initialize (name,age) 1391 | @name = name # this is how we declare instance variable, by having @ at the start 1392 | @age = age # same with age 1393 | end 1394 | 1395 | def details # By default this method would be public 1396 | "Name: #{@name}" 1397 | end 1398 | 1399 | def name 1400 | @name 1401 | end 1402 | 1403 | def name= name # we are defining a special method, with '=' in the signature 1404 | @name = name 1405 | end 1406 | 1407 | def is_adult 1408 | @age > 18 1409 | end 1410 | 1411 | private # special keyword in ruby, after this everything we write would be private 1412 | def full_details # A private function 1413 | return @name, @age # Yes you can return more than one value, that would be returned as an array 1414 | end 1415 | 1416 | 1417 | end 1418 | ``` 1419 | 1420 | 1421 | 1422 | 1423 | :full_details 1424 | 1425 | 1426 | 1427 | 1428 | ```ruby 1429 | person = Person.new("Tushar",25) 1430 | person.full_details # Would throw an error 1431 | ``` 1432 | 1433 | 1434 | NoMethodError: private method `full_details' called for # 1435 | 1436 |
:1:in `
' 1437 | 1438 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1439 | 1440 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1441 | 1442 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1443 | 1444 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1445 | 1446 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1447 | 1448 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1449 | 1450 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1451 | 1452 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1453 | 1454 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1455 | 1456 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1457 | 1458 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1459 | 1460 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1461 | 1462 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1463 | 1464 | 1465 | ### Inheritance 1466 | 1467 | 1468 | ```ruby 1469 | class RichPerson < Person # RichPerson Inherits from Person 1470 | def print_details 1471 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1472 | end 1473 | 1474 | end 1475 | ``` 1476 | 1477 | 1478 | 1479 | 1480 | :print_details 1481 | 1482 | 1483 | 1484 | 1485 | ```ruby 1486 | rich_person = RichPerson.new("Tushar",25) 1487 | ``` 1488 | 1489 | 1490 | ArgumentError: wrong number of arguments (given 2, expected 4) 1491 | 1492 |
:5:in `initialize' 1493 | 1494 |
:in `new' 1495 | 1496 |
:in `
' 1497 | 1498 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1499 | 1500 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1501 | 1502 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1503 | 1504 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1505 | 1506 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1507 | 1508 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1509 | 1510 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1511 | 1512 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1513 | 1514 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1515 | 1516 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1517 | 1518 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1519 | 1520 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1521 | 1522 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1523 | 1524 | 1525 | 1526 | ```ruby 1527 | rich_person.print_details 1528 | ``` 1529 | 1530 | ["Tuteja", 25] 1531 | 1532 | 1533 | Child Class Objects can Call any private function of parent class. 1534 | 1535 | lets add some more functionality, lets suppose we want to add money in RichPerson class 1536 | 1537 | 1538 | ```ruby 1539 | class RichPerson < Person # RichPerson Inherits from Person 1540 | def initialize name, age, money 1541 | super name, age # special keyword in ruby to call constructor of parent class 1542 | @money = money 1543 | end 1544 | 1545 | def money 1546 | @money 1547 | end 1548 | 1549 | def print_details 1550 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1551 | end 1552 | 1553 | end 1554 | ``` 1555 | 1556 | 1557 | 1558 | 1559 | :print_details 1560 | 1561 | 1562 | 1563 | 1564 | ```ruby 1565 | rich_person = RichPerson.new("Bill Gates",25,1) 1566 | ``` 1567 | 1568 | 1569 | 1570 | 1571 | # 1572 | 1573 | 1574 | 1575 | 1576 | ```ruby 1577 | rich_person.money 1578 | ``` 1579 | 1580 | 1581 | 1582 | 1583 | 1 1584 | 1585 | 1586 | 1587 | It is a hassle to write getter method, like we wrote above for money and setter method like we wrote for name. 1588 | For this ruby gives us short cuts. 1589 | 1590 | 1591 | ```ruby 1592 | class RichPerson 1593 | attr_accessor :name, :money # Gives us getter and setter methods for name and money 1594 | attr_reader :age # gives us only getter 1595 | attr_writer :phone # gives us only setter 1596 | 1597 | def initialize(name, money,age,phone) 1598 | @name = name 1599 | @money = money 1600 | @age = age 1601 | @phone = phone 1602 | end 1603 | 1604 | end 1605 | 1606 | rich_person = RichPerson.new("Tushar",1,25,9999999999) 1607 | ``` 1608 | 1609 | 1610 | 1611 | 1612 | # 1613 | 1614 | 1615 | 1616 | 1617 | ```ruby 1618 | rich_person.age # attr_reader, created a method by the name age that returns @age 1619 | ``` 1620 | 1621 | 1622 | 1623 | 1624 | 25 1625 | 1626 | 1627 | 1628 | 1629 | ```ruby 1630 | puts rich_person.name # attr_accesor, created a method by the name that returns @name 1631 | rich_person.name = "Tuteja"# attr_accesor, created a method by the name= that sets @name for us 1632 | puts rich_person.name 1633 | ``` 1634 | 1635 | Tushar 1636 | Tuteja 1637 | 1638 | 1639 | Similarly we can only change phone number but cannot see it 1640 | 1641 | 1642 | # Blocks 1643 | 1644 | 1645 | ```ruby 1646 | def special_function 1647 | yield 1648 | end 1649 | 1650 | 1651 | ``` 1652 | 1653 | 1654 | 1655 | 1656 | :special_function 1657 | 1658 | 1659 | 1660 | special_function is a type of funciton which requires a block of code to run. If we call it without supplying any block it would give an error as follows. How does the function knows that it requires a block ? The answer is the yield statement, as soon we put a yield statement, the function knows that this is where I'll give control to a block supplied to me. 1661 | 1662 | 1663 | ```ruby 1664 | special_function 1665 | ``` 1666 | 1667 | 1668 | LocalJumpError: no block given (yield) 1669 | 1670 |
:1:in `special_function' 1671 | 1672 |
:in `
' 1673 | 1674 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1675 | 1676 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1677 | 1678 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1679 | 1680 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1681 | 1682 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1683 | 1684 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1685 | 1686 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1687 | 1688 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1689 | 1690 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1691 | 1692 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1693 | 1694 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1695 | 1696 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1697 | 1698 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1699 | 1700 | 1701 | There are two ways to give a block. One is using a do end block and another is using {} braces. 1702 | 1703 | 1704 | ```ruby 1705 | special_function do 1706 | puts " a block is given to this function " 1707 | end 1708 | 1709 | # In This special function we'll receive a block of code that would be executed. 1710 | 1711 | ``` 1712 | 1713 | a block is given to this function 1714 | 1715 | 1716 | Where did the execution of that block occur ? Let's see another example to figure that out. 1717 | 1718 | 1719 | 1720 | ```ruby 1721 | def special_function 1722 | puts " before yield statement " 1723 | yield 1724 | puts " after yield statement " 1725 | end 1726 | 1727 | 1728 | ``` 1729 | 1730 | 1731 | 1732 | 1733 | :special_function 1734 | 1735 | 1736 | 1737 | ww'll give a block of code using curly braces this time. 1738 | 1739 | 1740 | ```ruby 1741 | special_function {puts "A block of code is given"} 1742 | ``` 1743 | 1744 | before yield statement 1745 | A block of code is given 1746 | after yield statement 1747 | 1748 | 1749 | 1750 | ```ruby 1751 | def special_function 1752 | yield 1753 | yield 1754 | end 1755 | 1756 | ``` 1757 | 1758 | 1759 | 1760 | 1761 | :special_function 1762 | 1763 | 1764 | 1765 | 1766 | ```ruby 1767 | special_function {puts "A block of code is given"} 1768 | ``` 1769 | 1770 | A block of code is given 1771 | A block of code is given 1772 | 1773 | 1774 | You can have more than one yield statement, but you can only pass one block. 1775 | 1776 | What if our block of code requires a parameter to be executed ? 1777 | 1778 | 1779 | 1780 | ```ruby 1781 | def special_function 1782 | yield 5, [1,2,3] # these arguments would be passed to the block of code we'll supply 1783 | end 1784 | 1785 | ``` 1786 | 1787 | 1788 | 1789 | 1790 | :special_function 1791 | 1792 | 1793 | 1794 | 1795 | ```ruby 1796 | special_function { |first_number, second_array | puts first_number,second_array } 1797 | ``` 1798 | 1799 | 5 1800 | [1, 2, 3] 1801 | 1802 | 1803 | 1804 | ```ruby 1805 | def iterator n 1806 | i = 0 1807 | while i < n 1808 | yield i 1809 | i = i + 1 1810 | end 1811 | end 1812 | ``` 1813 | 1814 | 1815 | 1816 | 1817 | :iterator 1818 | 1819 | 1820 | 1821 | 1822 | ```ruby 1823 | iterator(10) do |x| 1824 | puts "this is the iteration number #{x}" 1825 | end 1826 | ``` 1827 | 1828 | this is the iteration number 0 1829 | this is the iteration number 1 1830 | this is the iteration number 2 1831 | this is the iteration number 3 1832 | this is the iteration number 4 1833 | this is the iteration number 5 1834 | this is the iteration number 6 1835 | this is the iteration number 7 1836 | this is the iteration number 8 1837 | this is the iteration number 9 1838 | 1839 | 1840 | 1841 | ```ruby 1842 | 1843 | ``` 1844 | -------------------------------------------------------------------------------- /_site/javascripts/main.js: -------------------------------------------------------------------------------- 1 | console.log('This would be the main JS file.'); 2 | -------------------------------------------------------------------------------- /_site/notebook_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/_site/notebook_demo.gif -------------------------------------------------------------------------------- /_site/params.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Rubytutorial", 3 | "tagline": "", 4 | "body": "\r\n# RubyTutorial\r\n\r\nWe are using Jupyter Notebooks to make this tutorial.\r\nFor those who don't know about Jupyter Notebooks, go [here](https://jupyter.org/). \r\nBelow is a sample, about how notebooks work.\r\n\r\n![alt text](https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/master/notebook_demo.gif \"Without Smooth Scroll\")\r\n\r\n## Basics\r\n\r\n### Data Types\r\n\r\n\r\n```ruby\r\nmy_num = 15\r\nmy_string = \"Tushar\"\r\nmy_bool = true\r\n\r\nputs my_num, my_string, my_bool\r\n```\r\n\r\n 15\r\n Tushar\r\n true\r\n\r\n\r\nRuby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables\r\n\r\n'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. \r\n\r\n\r\n```ruby\r\nprint my_num, my_string, my_bool \r\n```\r\n\r\n 15Tushartrue\r\n\r\nmy_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. \r\n\r\nEverything is a an Object in Ruby,\r\n\r\nEverything!\r\n\r\nTo find out class of a ruby object we can call a 'class' method on any object.\r\n\r\n\r\n```ruby\r\nputs my_num.class() , my_string.class() , my_bool.class()\r\n```\r\n\r\n Fixnum\r\n String\r\n TrueClass\r\n\r\n\r\n## Arithmetic\r\n\r\n\r\nBasic Arithmetic operators are '+','-','*','/' and '%'.\r\n\r\n\r\n```ruby\r\nfive = 5\r\ntwo = 2\r\nseven = 5 + 2\r\n\r\n```\r\n\r\n\r\n\r\n\r\n 7\r\n\r\n\r\n\r\n\r\n```ruby\r\nten = five * two\r\n```\r\n\r\n\r\n\r\n\r\n 10\r\n\r\n\r\n\r\n\r\n```ruby\r\none = five % two\r\n```\r\n\r\n\r\n\r\n\r\n 1\r\n\r\n\r\n\r\n\r\n```ruby\r\ntwo = five / two\r\n```\r\n\r\n\r\n\r\n\r\n 2\r\n\r\n\r\n\r\n\r\n```ruby\r\nthree = five - two\r\n\r\n```\r\n\r\n\r\n\r\n\r\n 3\r\n\r\n\r\n\r\n## Strings\r\n\r\n\r\n```ruby\r\nmy_string = \"Programming in Ruby Is Fun.\"\r\n```\r\n\r\n\r\n\r\n\r\n \"Programming in Ruby Is Fun.\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_string.length()\r\n```\r\n\r\n\r\n\r\n\r\n 27\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_string.reverse()\r\n```\r\n\r\n\r\n\r\n\r\n \".nuF sI ybuR ni gnimmargorP\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nputs my_string\r\n```\r\n\r\n Programming in Ruby Is Fun.\r\n\r\n\r\n\r\n```ruby\r\nmy_string.reverse!()\r\n```\r\n\r\n\r\n\r\n\r\n \".nuF sI ybuR ni gnimmargorP\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nputs my_string\r\n```\r\n\r\n .nuF sI ybuR ni gnimmargorP\r\n\r\n\r\n\r\n```ruby\r\nmy_string = \"Programming in Ruby Is Fun.\"\r\nmy_string.split(\" \")\r\n```\r\n\r\n\r\n\r\n\r\n [\"Programming\", \"in\", \"Ruby\", \"Is\", \"Fun.\"]\r\n\r\n\r\n\r\n\r\n```ruby\r\nwords = my_string.split(\" \")\r\nputs words #Gives an array, we'll come to it\r\n```\r\n\r\n [\"Programming\", \"in\", \"Ruby\", \"Is\", \"Fun.\"]\r\n\r\n\r\n\r\n```ruby\r\nword = \"Programming\"\r\n```\r\n\r\n\r\n\r\n\r\n \"Programming\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nword.downcase() #Get a word with lower case letters\r\n```\r\n\r\n\r\n\r\n\r\n \"programming\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nword.upcase() # get a word with upper case letters\r\n```\r\n\r\n\r\n\r\n\r\n \"PROGRAMMING\"\r\n\r\n\r\n\r\n\r\n```ruby\r\n#This is a single line comment\r\nputs \"comment\" #this can come at the end of any expression\r\n=begin\r\nThis is a multiline comment.\r\n=begin should start at the first character of the line to be it a comment, other wise it wont work\r\n\r\n=end\r\n```\r\n\r\n comment\r\n\r\n\r\n## Naming Conventions\r\n\r\nFor methods and variables we follow snake case like example_variable, example_function.\r\nFor Classes we follow upper case letters, MyOwnClass.\r\nFor functions which are risky to use, like reverse!, we add exclamation mark at the end.\r\n\r\n## Function Chaining\r\n\r\n\r\n\r\n```ruby\r\nmy_string = \"tushar tuteja\"\r\n```\r\n\r\n\r\n\r\n\r\n \"tushar tuteja\"\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_string.reverse().upcase() # You can chain function calls one after the other\r\n```\r\n\r\n\r\n\r\n\r\n \"AJETUT RAHSUT\"\r\n\r\n\r\n\r\n## String Formatter\r\n\r\n\r\n```ruby\r\nmy_name = \"Tushar Tuteja\"\r\nmy_city = \"Delhi\"\r\nputs \"Hi, I am #{my_name}, I belong to #{my_city}.\" #way to format complex strings.\r\n```\r\n\r\n Hi, I am Tushar Tuteja, I belong to Delhi\r\n\r\n\r\n## Control Flow\r\n\r\n\r\n```ruby\r\nif true\r\n puts \"true\"\r\nelse\r\n puts \"false\"\r\nend\r\n\r\n```\r\n\r\n true\r\n\r\n\r\n\r\n```ruby\r\nfalse == \"false\" # this is false\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n\r\n```ruby\r\nfalse == nil # this is false\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_object = nil\r\n```\r\n\r\n\r\n```ruby\r\nmy_object == nil\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_object.nil? # this is the way to check nil\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\nRuby has infinite true expressions, only two false expressions, one is false and the other is nil. So 0 would evaluate to true, this is different form C and C++ language.\r\n\r\n\r\n```ruby\r\nif my_object\r\n puts \"true\"\r\nelse\r\n puts \"nil or false\"\r\nend\r\n\r\n```\r\n\r\n nil or false\r\n\r\n\r\n\r\n```ruby\r\nmy_object = 0\r\nif my_object\r\n puts \"this evaluate to true\"\r\nelse\r\n puts \"should have been false if we were coding in C.\"\r\nend\r\n\r\n```\r\n\r\n this evaluate to true\r\n\r\n\r\n\r\n```ruby\r\nfirst_condition = false\r\nsecond_condition = true\r\n\r\nif first_condition\r\n puts \"first condition is true\"\r\nelsif second_condition\r\n puts \"second condition is true\"\r\nelsif 0 > 3\r\n puts \"won't happend\"\r\nend\r\n\r\n```\r\n\r\n second condition is true\r\n\r\n\r\n\r\n```ruby\r\nhungry = true\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\nif not hungry \r\n puts \"keep working\"\r\nelse\r\n puts \"eat\"\r\nend\r\n```\r\n\r\n eat\r\n\r\n\r\nRuby gives us more than if else, it gives us unless else, which is more verbose than if not. Example\r\n\r\n\r\n```ruby\r\nunless hungry\r\n puts \"keep working\"\r\nelse\r\n puts \"Eat\"\r\nend\r\n\r\n```\r\n\r\n Eat\r\n\r\n\r\n### Conditional Operators : == , >= ,<= ,!=, !\r\n\r\n\r\n\r\n```ruby\r\n5 == '5'\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n\r\n```ruby\r\n5*4 == 4*5\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\n4 < 7\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\n8 > 9\r\n\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n\r\n```ruby\r\n\"tushar\" > \"tuteja\"\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n\r\n```ruby\r\n5 >= 5\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\n4 != 5\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\n!true\r\n```\r\n\r\n\r\n\r\n\r\n false\r\n\r\n\r\n\r\n### Composite Conditional Statements\r\n\r\n\r\n```ruby\r\nwork_done = true\r\nif work_done || hungry\r\n puts \"eat\"\r\nelse\r\n puts \"work\"\r\nend\r\n```\r\n\r\n eat\r\n\r\n\r\n\r\n```ruby\r\nputs false || true\r\nputs true || false\r\nputs true && true\r\nputs true && false\r\nputs false || false\r\n```\r\n\r\n true\r\n true\r\n true\r\n false\r\n false\r\n\r\n\r\n\r\n```ruby\r\nif not false\r\n puts \"it is true\"\r\nelse\r\n puts \"it is false\"\r\nend\r\n\r\n```\r\n\r\n it is true\r\n\r\n\r\n## Loops\r\n\r\n\r\n```ruby\r\ni = 0 \r\nwhile i < 5 \r\n puts \"ruby is fun\"\r\n i = i + 1\r\nend\r\n\r\n```\r\n\r\n ruby is fun\r\n ruby is fun\r\n ruby is fun\r\n ruby is fun\r\n ruby is fun\r\n\r\n\r\n\r\n```ruby\r\ni = 5\r\nwhile i != 0\r\n puts \"ruby is more verbose\"\r\n i = i - 1\r\nend\r\n```\r\n\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n\r\n\r\n\r\n```ruby\r\ni = 5\r\nuntil i == 0\r\n puts \"ruby is more verbose\"\r\n i = i - 1\r\nend\r\n\r\n```\r\n\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n ruby is more verbose\r\n\r\n\r\n\r\n```ruby\r\nfor i in 1..5\r\n puts \"ruby has many ways to do the same thing.\"\r\nend\r\n```\r\n\r\n ruby has many ways to do the same thing.\r\n ruby has many ways to do the same thing.\r\n ruby has many ways to do the same thing.\r\n ruby has many ways to do the same thing.\r\n ruby has many ways to do the same thing.\r\n\r\n\r\n\r\n\r\n\r\n 1..5\r\n\r\n\r\n\r\n\r\n```ruby\r\nfor i in 1...5\r\n puts \"..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5\"\r\nend\r\n```\r\n\r\n ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5\r\n ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5\r\n ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5\r\n ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5\r\n\r\n\r\n\r\n\r\n\r\n 1...5\r\n\r\n\r\n\r\n\r\n```ruby\r\n5.times do \r\n puts \"this needs to be done 5 times\"\r\nend\r\n```\r\n\r\n this needs to be done 5 times\r\n this needs to be done 5 times\r\n this needs to be done 5 times\r\n this needs to be done 5 times\r\n this needs to be done 5 times\r\n\r\n\r\n\r\n\r\n\r\n 5\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_string = \"Ruby is made for productivity.\"\r\nwords = my_string.split(\" \")\r\nwords.each do |word| #would discuss more of this in arrays and hashes\r\n puts word.capitalize # This would capitalize every word\r\nend\r\n```\r\n\r\n Ruby\r\n Is\r\n Made\r\n For\r\n Productivity.\r\n\r\n\r\n\r\n\r\n\r\n [\"Ruby\", \"is\", \"made\", \"for\", \"productivity.\"]\r\n\r\n\r\n\r\n## Arrays\r\n\r\n\r\n```ruby\r\na = [] # this creates an empty array\r\n```\r\n\r\n\r\n\r\n\r\n []\r\n\r\n\r\n\r\nArrays are lists in ruby, means they doesn't need to be homegenous.\r\n\r\n\r\n```ruby\r\na = [\"tushar\", 5, [4,5,6]]\r\n```\r\n\r\n\r\n\r\n\r\n [\"tushar\", 5, [4, 5, 6]]\r\n\r\n\r\n\r\nArrays are continous data structures\r\n\r\n\r\n\r\n```ruby\r\na = []\r\na[45] = 5\r\nputs a\r\n```\r\n\r\n [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5]\r\n\r\n\r\na[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero.\r\n\r\n\r\n```ruby\r\nputs a[45]\r\n```\r\n\r\n 5\r\n\r\n\r\n\r\n```ruby\r\nputs a[0]\r\n```\r\n\r\n \r\n\r\n\r\n\r\n```ruby\r\nputs a.length\r\n```\r\n\r\n 46\r\n\r\n\r\n## Hashes\r\n\r\nHashes are smilar to javascript dictionaries, the only difference is that they are ordered. \r\n\r\n\r\n```ruby\r\nmy_hash = {} # creates a new Hash\r\n\r\n```\r\n\r\n\r\n\r\n\r\n {}\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_hash = Hash.new # creates a new Hash\r\n```\r\n\r\n\r\n\r\n\r\n {}\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_hash = {:name => \"Tushar\", :city => \"Delhi\"} # Creates a hash \r\n```\r\n\r\n\r\n\r\n\r\n {:name=>\"Tushar\", :city=>\"Delhi\"}\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_hash[:name] # to access :name from my_hash\r\n```\r\n\r\n\r\n\r\n\r\n \"Tushar\"\r\n\r\n\r\n\r\n:name and :city are actually symbols. they are used for faster accessing, we may use strings as well.\r\n\r\n\r\n\r\n```ruby\r\nmy_hash = {\"name\" => \"tushar\"}\r\n```\r\n\r\n\r\n\r\n\r\n {\"name\"=>\"tushar\"}\r\n\r\n\r\n\r\n\r\n```ruby\r\nputs my_hash[\"name\"]\r\nputs my_hash[:name] # This won't work, you need to remember that strings and symbols are different\r\n```\r\n\r\n tushar\r\n \r\n\r\n\r\n\r\n```ruby\r\nmy_hash = {:name => \"Tushar\", :city => \"Delhi\", :age => 25}\r\n```\r\n\r\n\r\n\r\n\r\n {:name=>\"Tushar\", :city=>\"Delhi\", :age=>25}\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_hash.each do |key| # iterating over the keys\r\n puts key\r\nend\r\n\r\n```\r\n\r\n [:name, \"Tushar\"]\r\n [:city, \"Delhi\"]\r\n [:age, 25]\r\n\r\n\r\n\r\n\r\n\r\n {:name=>\"Tushar\", :city=>\"Delhi\", :age=>25}\r\n\r\n\r\n\r\nIf you look at the above code, the keys are ordered.\r\n\r\n\r\n```ruby\r\nmy_hash.each do |key,value|\r\n puts \"#{key} : #{value}\"\r\nend\r\n```\r\n\r\n name : Tushar\r\n city : Delhi\r\n age : 25\r\n\r\n\r\n\r\n\r\n\r\n {:name=>\"Tushar\", :city=>\"Delhi\", :age=>25}\r\n\r\n\r\n\r\n## Methods\r\n\r\n\r\n\r\n```ruby\r\ndef my_method\r\n puts \"hello method\"\r\nend\r\n\r\n```\r\n\r\n\r\n\r\n\r\n :my_method\r\n\r\n\r\n\r\n\r\n```ruby\r\nmy_method()\r\n```\r\n\r\n hello method\r\n\r\n\r\n\r\n```ruby\r\nmy_method # parenthesis are optional in ruby, it makes the code more verbose \r\n```\r\n\r\n hello method\r\n\r\n\r\n\r\n```ruby\r\ndef is_odd number # parenthesis are optional\r\n return number % 2 == 1\r\nend\r\n\r\nis_odd 1 # parenthesis are optional\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\ndef is_even number\r\n number % 2 == 0 #last expression in every function is a return statement by default.\r\nend\r\n\r\n```\r\n\r\n\r\n\r\n\r\n :is_even\r\n\r\n\r\n\r\n\r\n```ruby\r\nis_even 2\r\n```\r\n\r\n\r\n\r\n\r\n true\r\n\r\n\r\n\r\n\r\n```ruby\r\ndef join_words words_array\r\n result = \"\"\r\n words_array.each do |word|\r\n result = result + word + \" \" # String concatenation\r\n end\r\n result\r\nend\r\n\r\nputs join_words [\"Ruby\",\"is\",\"flexible\"]\r\n```\r\n\r\n Ruby is flexible \r\n\r\n\r\nWhat is you want to pass a variable number of arguments and you don't want to pass an array.\r\n\r\nVoila, we have *args arguement.\r\n\r\n\r\n```ruby\r\ndef join_words *args\r\n result = \"\"\r\n args.each do |word|\r\n result = result + word + \" \" # String concatenation\r\n end\r\n result\r\nend\r\n\r\nputs join_words \"Ruby\",\"is\",\"flexible\" # This code is more verbose\r\nputs join_words \"Ruby\",\"is\",\"flexible\",\",\", \"This\", \"code\" ,\"is\" ,\"more\" ,\"verbose\"\r\n\r\n```\r\n\r\n Ruby is flexible \r\n Ruby is flexible , This code is more verbose \r\n\r\n\r\n\r\n```ruby\r\n\r\n```\r\n", 5 | "note": "Don't delete this file! It's used internally to help with page regeneration." 6 | } -------------------------------------------------------------------------------- /_site/stylesheets/github-light.css: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 GitHub, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | .pl-c /* comment */ { 27 | color: #969896; 28 | } 29 | 30 | .pl-c1 /* constant, variable.other.constant, support, meta.property-name, support.constant, support.variable, meta.module-reference, markup.raw, meta.diff.header */, 31 | .pl-s .pl-v /* string variable */ { 32 | color: #0086b3; 33 | } 34 | 35 | .pl-e /* entity */, 36 | .pl-en /* entity.name */ { 37 | color: #795da3; 38 | } 39 | 40 | .pl-smi /* variable.parameter.function, storage.modifier.package, storage.modifier.import, storage.type.java, variable.other */, 41 | .pl-s .pl-s1 /* string source */ { 42 | color: #333; 43 | } 44 | 45 | .pl-ent /* entity.name.tag */ { 46 | color: #63a35c; 47 | } 48 | 49 | .pl-k /* keyword, storage, storage.type */ { 50 | color: #a71d5d; 51 | } 52 | 53 | .pl-s /* string */, 54 | .pl-pds /* punctuation.definition.string, string.regexp.character-class */, 55 | .pl-s .pl-pse .pl-s1 /* string punctuation.section.embedded source */, 56 | .pl-sr /* string.regexp */, 57 | .pl-sr .pl-cce /* string.regexp constant.character.escape */, 58 | .pl-sr .pl-sre /* string.regexp source.ruby.embedded */, 59 | .pl-sr .pl-sra /* string.regexp string.regexp.arbitrary-repitition */ { 60 | color: #183691; 61 | } 62 | 63 | .pl-v /* variable */ { 64 | color: #ed6a43; 65 | } 66 | 67 | .pl-id /* invalid.deprecated */ { 68 | color: #b52a1d; 69 | } 70 | 71 | .pl-ii /* invalid.illegal */ { 72 | color: #f8f8f8; 73 | background-color: #b52a1d; 74 | } 75 | 76 | .pl-sr .pl-cce /* string.regexp constant.character.escape */ { 77 | font-weight: bold; 78 | color: #63a35c; 79 | } 80 | 81 | .pl-ml /* markup.list */ { 82 | color: #693a17; 83 | } 84 | 85 | .pl-mh /* markup.heading */, 86 | .pl-mh .pl-en /* markup.heading entity.name */, 87 | .pl-ms /* meta.separator */ { 88 | font-weight: bold; 89 | color: #1d3e81; 90 | } 91 | 92 | .pl-mq /* markup.quote */ { 93 | color: #008080; 94 | } 95 | 96 | .pl-mi /* markup.italic */ { 97 | font-style: italic; 98 | color: #333; 99 | } 100 | 101 | .pl-mb /* markup.bold */ { 102 | font-weight: bold; 103 | color: #333; 104 | } 105 | 106 | .pl-md /* markup.deleted, meta.diff.header.from-file */ { 107 | color: #bd2c00; 108 | background-color: #ffecec; 109 | } 110 | 111 | .pl-mi1 /* markup.inserted, meta.diff.header.to-file */ { 112 | color: #55a532; 113 | background-color: #eaffea; 114 | } 115 | 116 | .pl-mdr /* meta.diff.range */ { 117 | font-weight: bold; 118 | color: #795da3; 119 | } 120 | 121 | .pl-mo /* meta.output */ { 122 | color: #1d3e81; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /_site/stylesheets/normalize.css: -------------------------------------------------------------------------------- 1 | /*! normalize.css v3.0.2 | MIT License | git.io/normalize */ 2 | 3 | /** 4 | * 1. Set default font family to sans-serif. 5 | * 2. Prevent iOS text size adjust after orientation change, without disabling 6 | * user zoom. 7 | */ 8 | 9 | html { 10 | font-family: sans-serif; /* 1 */ 11 | -ms-text-size-adjust: 100%; /* 2 */ 12 | -webkit-text-size-adjust: 100%; /* 2 */ 13 | } 14 | 15 | /** 16 | * Remove default margin. 17 | */ 18 | 19 | body { 20 | margin: 0; 21 | } 22 | 23 | /* HTML5 display definitions 24 | ========================================================================== */ 25 | 26 | /** 27 | * Correct `block` display not defined for any HTML5 element in IE 8/9. 28 | * Correct `block` display not defined for `details` or `summary` in IE 10/11 29 | * and Firefox. 30 | * Correct `block` display not defined for `main` in IE 11. 31 | */ 32 | 33 | article, 34 | aside, 35 | details, 36 | figcaption, 37 | figure, 38 | footer, 39 | header, 40 | hgroup, 41 | main, 42 | menu, 43 | nav, 44 | section, 45 | summary { 46 | display: block; 47 | } 48 | 49 | /** 50 | * 1. Correct `inline-block` display not defined in IE 8/9. 51 | * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera. 52 | */ 53 | 54 | audio, 55 | canvas, 56 | progress, 57 | video { 58 | display: inline-block; /* 1 */ 59 | vertical-align: baseline; /* 2 */ 60 | } 61 | 62 | /** 63 | * Prevent modern browsers from displaying `audio` without controls. 64 | * Remove excess height in iOS 5 devices. 65 | */ 66 | 67 | audio:not([controls]) { 68 | display: none; 69 | height: 0; 70 | } 71 | 72 | /** 73 | * Address `[hidden]` styling not present in IE 8/9/10. 74 | * Hide the `template` element in IE 8/9/11, Safari, and Firefox < 22. 75 | */ 76 | 77 | [hidden], 78 | template { 79 | display: none; 80 | } 81 | 82 | /* Links 83 | ========================================================================== */ 84 | 85 | /** 86 | * Remove the gray background color from active links in IE 10. 87 | */ 88 | 89 | a { 90 | background-color: transparent; 91 | } 92 | 93 | /** 94 | * Improve readability when focused and also mouse hovered in all browsers. 95 | */ 96 | 97 | a:active, 98 | a:hover { 99 | outline: 0; 100 | } 101 | 102 | /* Text-level semantics 103 | ========================================================================== */ 104 | 105 | /** 106 | * Address styling not present in IE 8/9/10/11, Safari, and Chrome. 107 | */ 108 | 109 | abbr[title] { 110 | border-bottom: 1px dotted; 111 | } 112 | 113 | /** 114 | * Address style set to `bolder` in Firefox 4+, Safari, and Chrome. 115 | */ 116 | 117 | b, 118 | strong { 119 | font-weight: bold; 120 | } 121 | 122 | /** 123 | * Address styling not present in Safari and Chrome. 124 | */ 125 | 126 | dfn { 127 | font-style: italic; 128 | } 129 | 130 | /** 131 | * Address variable `h1` font-size and margin within `section` and `article` 132 | * contexts in Firefox 4+, Safari, and Chrome. 133 | */ 134 | 135 | h1 { 136 | font-size: 2em; 137 | margin: 0.67em 0; 138 | } 139 | 140 | /** 141 | * Address styling not present in IE 8/9. 142 | */ 143 | 144 | mark { 145 | background: #ff0; 146 | color: #000; 147 | } 148 | 149 | /** 150 | * Address inconsistent and variable font size in all browsers. 151 | */ 152 | 153 | small { 154 | font-size: 80%; 155 | } 156 | 157 | /** 158 | * Prevent `sub` and `sup` affecting `line-height` in all browsers. 159 | */ 160 | 161 | sub, 162 | sup { 163 | font-size: 75%; 164 | line-height: 0; 165 | position: relative; 166 | vertical-align: baseline; 167 | } 168 | 169 | sup { 170 | top: -0.5em; 171 | } 172 | 173 | sub { 174 | bottom: -0.25em; 175 | } 176 | 177 | /* Embedded content 178 | ========================================================================== */ 179 | 180 | /** 181 | * Remove border when inside `a` element in IE 8/9/10. 182 | */ 183 | 184 | img { 185 | border: 0; 186 | } 187 | 188 | /** 189 | * Correct overflow not hidden in IE 9/10/11. 190 | */ 191 | 192 | svg:not(:root) { 193 | overflow: hidden; 194 | } 195 | 196 | /* Grouping content 197 | ========================================================================== */ 198 | 199 | /** 200 | * Address margin not present in IE 8/9 and Safari. 201 | */ 202 | 203 | figure { 204 | margin: 1em 40px; 205 | } 206 | 207 | /** 208 | * Address differences between Firefox and other browsers. 209 | */ 210 | 211 | hr { 212 | box-sizing: content-box; 213 | height: 0; 214 | } 215 | 216 | /** 217 | * Contain overflow in all browsers. 218 | */ 219 | 220 | pre { 221 | overflow: auto; 222 | } 223 | 224 | /** 225 | * Address odd `em`-unit font size rendering in all browsers. 226 | */ 227 | 228 | code, 229 | kbd, 230 | pre, 231 | samp { 232 | font-family: monospace, monospace; 233 | font-size: 1em; 234 | } 235 | 236 | /* Forms 237 | ========================================================================== */ 238 | 239 | /** 240 | * Known limitation: by default, Chrome and Safari on OS X allow very limited 241 | * styling of `select`, unless a `border` property is set. 242 | */ 243 | 244 | /** 245 | * 1. Correct color not being inherited. 246 | * Known issue: affects color of disabled elements. 247 | * 2. Correct font properties not being inherited. 248 | * 3. Address margins set differently in Firefox 4+, Safari, and Chrome. 249 | */ 250 | 251 | button, 252 | input, 253 | optgroup, 254 | select, 255 | textarea { 256 | color: inherit; /* 1 */ 257 | font: inherit; /* 2 */ 258 | margin: 0; /* 3 */ 259 | } 260 | 261 | /** 262 | * Address `overflow` set to `hidden` in IE 8/9/10/11. 263 | */ 264 | 265 | button { 266 | overflow: visible; 267 | } 268 | 269 | /** 270 | * Address inconsistent `text-transform` inheritance for `button` and `select`. 271 | * All other form control elements do not inherit `text-transform` values. 272 | * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera. 273 | * Correct `select` style inheritance in Firefox. 274 | */ 275 | 276 | button, 277 | select { 278 | text-transform: none; 279 | } 280 | 281 | /** 282 | * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` 283 | * and `video` controls. 284 | * 2. Correct inability to style clickable `input` types in iOS. 285 | * 3. Improve usability and consistency of cursor style between image-type 286 | * `input` and others. 287 | */ 288 | 289 | button, 290 | html input[type="button"], /* 1 */ 291 | input[type="reset"], 292 | input[type="submit"] { 293 | -webkit-appearance: button; /* 2 */ 294 | cursor: pointer; /* 3 */ 295 | } 296 | 297 | /** 298 | * Re-set default cursor for disabled elements. 299 | */ 300 | 301 | button[disabled], 302 | html input[disabled] { 303 | cursor: default; 304 | } 305 | 306 | /** 307 | * Remove inner padding and border in Firefox 4+. 308 | */ 309 | 310 | button::-moz-focus-inner, 311 | input::-moz-focus-inner { 312 | border: 0; 313 | padding: 0; 314 | } 315 | 316 | /** 317 | * Address Firefox 4+ setting `line-height` on `input` using `!important` in 318 | * the UA stylesheet. 319 | */ 320 | 321 | input { 322 | line-height: normal; 323 | } 324 | 325 | /** 326 | * It's recommended that you don't attempt to style these elements. 327 | * Firefox's implementation doesn't respect box-sizing, padding, or width. 328 | * 329 | * 1. Address box sizing set to `content-box` in IE 8/9/10. 330 | * 2. Remove excess padding in IE 8/9/10. 331 | */ 332 | 333 | input[type="checkbox"], 334 | input[type="radio"] { 335 | box-sizing: border-box; /* 1 */ 336 | padding: 0; /* 2 */ 337 | } 338 | 339 | /** 340 | * Fix the cursor style for Chrome's increment/decrement buttons. For certain 341 | * `font-size` values of the `input`, it causes the cursor style of the 342 | * decrement button to change from `default` to `text`. 343 | */ 344 | 345 | input[type="number"]::-webkit-inner-spin-button, 346 | input[type="number"]::-webkit-outer-spin-button { 347 | height: auto; 348 | } 349 | 350 | /** 351 | * 1. Address `appearance` set to `searchfield` in Safari and Chrome. 352 | * 2. Address `box-sizing` set to `border-box` in Safari and Chrome 353 | * (include `-moz` to future-proof). 354 | */ 355 | 356 | input[type="search"] { 357 | -webkit-appearance: textfield; /* 1 */ /* 2 */ 358 | box-sizing: content-box; 359 | } 360 | 361 | /** 362 | * Remove inner padding and search cancel button in Safari and Chrome on OS X. 363 | * Safari (but not Chrome) clips the cancel button when the search input has 364 | * padding (and `textfield` appearance). 365 | */ 366 | 367 | input[type="search"]::-webkit-search-cancel-button, 368 | input[type="search"]::-webkit-search-decoration { 369 | -webkit-appearance: none; 370 | } 371 | 372 | /** 373 | * Define consistent border, margin, and padding. 374 | */ 375 | 376 | fieldset { 377 | border: 1px solid #c0c0c0; 378 | margin: 0 2px; 379 | padding: 0.35em 0.625em 0.75em; 380 | } 381 | 382 | /** 383 | * 1. Correct `color` not being inherited in IE 8/9/10/11. 384 | * 2. Remove padding so people aren't caught out if they zero out fieldsets. 385 | */ 386 | 387 | legend { 388 | border: 0; /* 1 */ 389 | padding: 0; /* 2 */ 390 | } 391 | 392 | /** 393 | * Remove default vertical scrollbar in IE 8/9/10/11. 394 | */ 395 | 396 | textarea { 397 | overflow: auto; 398 | } 399 | 400 | /** 401 | * Don't inherit the `font-weight` (applied by a rule above). 402 | * NOTE: the default cannot safely be changed in Chrome and Safari on OS X. 403 | */ 404 | 405 | optgroup { 406 | font-weight: bold; 407 | } 408 | 409 | /* Tables 410 | ========================================================================== */ 411 | 412 | /** 413 | * Remove most spacing between table cells. 414 | */ 415 | 416 | table { 417 | border-collapse: collapse; 418 | border-spacing: 0; 419 | } 420 | 421 | td, 422 | th { 423 | padding: 0; 424 | } 425 | -------------------------------------------------------------------------------- /_site/stylesheets/stylesheet.css: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Slate Theme for GitHub Pages 3 | by Jason Costello, @jsncostello 4 | *******************************************************************************/ 5 | 6 | @import url(github-light.css); 7 | 8 | /******************************************************************************* 9 | MeyerWeb Reset 10 | *******************************************************************************/ 11 | 12 | html, body, div, span, applet, object, iframe, 13 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 14 | a, abbr, acronym, address, big, cite, code, 15 | del, dfn, em, img, ins, kbd, q, s, samp, 16 | small, strike, strong, sub, sup, tt, var, 17 | b, u, i, center, 18 | dl, dt, dd, ol, ul, li, 19 | fieldset, form, label, legend, 20 | table, caption, tbody, tfoot, thead, tr, th, td, 21 | article, aside, canvas, details, embed, 22 | figure, figcaption, footer, header, hgroup, 23 | menu, nav, output, ruby, section, summary, 24 | time, mark, audio, video { 25 | margin: 0; 26 | padding: 0; 27 | border: 0; 28 | font: inherit; 29 | vertical-align: baseline; 30 | } 31 | 32 | /* HTML5 display-role reset for older browsers */ 33 | article, aside, details, figcaption, figure, 34 | footer, header, hgroup, menu, nav, section { 35 | display: block; 36 | } 37 | 38 | ol, ul { 39 | list-style: none; 40 | } 41 | 42 | table { 43 | border-collapse: collapse; 44 | border-spacing: 0; 45 | } 46 | 47 | /******************************************************************************* 48 | Theme Styles 49 | *******************************************************************************/ 50 | 51 | body { 52 | box-sizing: border-box; 53 | color:#373737; 54 | background: #212121; 55 | font-size: 16px; 56 | font-family: 'Myriad Pro', Calibri, Helvetica, Arial, sans-serif; 57 | line-height: 1.5; 58 | -webkit-font-smoothing: antialiased; 59 | } 60 | 61 | h1, h2, h3, h4, h5, h6 { 62 | margin: 10px 0; 63 | font-weight: 700; 64 | color:#222222; 65 | font-family: 'Lucida Grande', 'Calibri', Helvetica, Arial, sans-serif; 66 | letter-spacing: -1px; 67 | } 68 | 69 | h1 { 70 | font-size: 36px; 71 | font-weight: 700; 72 | } 73 | 74 | h2 { 75 | padding-bottom: 10px; 76 | font-size: 32px; 77 | background: url('../images/bg_hr.png') repeat-x bottom; 78 | } 79 | 80 | h3 { 81 | font-size: 24px; 82 | } 83 | 84 | h4 { 85 | font-size: 21px; 86 | } 87 | 88 | h5 { 89 | font-size: 18px; 90 | } 91 | 92 | h6 { 93 | font-size: 16px; 94 | } 95 | 96 | p { 97 | margin: 10px 0 15px 0; 98 | } 99 | 100 | footer p { 101 | color: #f2f2f2; 102 | } 103 | 104 | a { 105 | text-decoration: none; 106 | color: #007edf; 107 | text-shadow: none; 108 | 109 | transition: color 0.5s ease; 110 | transition: text-shadow 0.5s ease; 111 | -webkit-transition: color 0.5s ease; 112 | -webkit-transition: text-shadow 0.5s ease; 113 | -moz-transition: color 0.5s ease; 114 | -moz-transition: text-shadow 0.5s ease; 115 | -o-transition: color 0.5s ease; 116 | -o-transition: text-shadow 0.5s ease; 117 | -ms-transition: color 0.5s ease; 118 | -ms-transition: text-shadow 0.5s ease; 119 | } 120 | 121 | a:hover, a:focus {text-decoration: underline;} 122 | 123 | footer a { 124 | color: #F2F2F2; 125 | text-decoration: underline; 126 | } 127 | 128 | em { 129 | font-style: italic; 130 | } 131 | 132 | strong { 133 | font-weight: bold; 134 | } 135 | 136 | img { 137 | position: relative; 138 | margin: 0 auto; 139 | max-width: 739px; 140 | padding: 5px; 141 | margin: 10px 0 10px 0; 142 | border: 1px solid #ebebeb; 143 | 144 | box-shadow: 0 0 5px #ebebeb; 145 | -webkit-box-shadow: 0 0 5px #ebebeb; 146 | -moz-box-shadow: 0 0 5px #ebebeb; 147 | -o-box-shadow: 0 0 5px #ebebeb; 148 | -ms-box-shadow: 0 0 5px #ebebeb; 149 | } 150 | 151 | p img { 152 | display: inline; 153 | margin: 0; 154 | padding: 0; 155 | vertical-align: middle; 156 | text-align: center; 157 | border: none; 158 | } 159 | 160 | pre, code { 161 | width: 100%; 162 | color: #222; 163 | background-color: #fff; 164 | 165 | font-family: Monaco, "Bitstream Vera Sans Mono", "Lucida Console", Terminal, monospace; 166 | font-size: 14px; 167 | 168 | border-radius: 2px; 169 | -moz-border-radius: 2px; 170 | -webkit-border-radius: 2px; 171 | } 172 | 173 | pre { 174 | width: 100%; 175 | padding: 10px; 176 | box-shadow: 0 0 10px rgba(0,0,0,.1); 177 | overflow: auto; 178 | } 179 | 180 | code { 181 | padding: 3px; 182 | margin: 0 3px; 183 | box-shadow: 0 0 10px rgba(0,0,0,.1); 184 | } 185 | 186 | pre code { 187 | display: block; 188 | box-shadow: none; 189 | background-color: aliceblue; 190 | } 191 | 192 | 193 | pre code::before{ 194 | content: ""; 195 | color: black; 196 | display: block; 197 | position: absolute; 198 | right: 0px; 199 | background-color:white; 200 | box-shadow: 1px 1px 1px white; 201 | box-sizing: border-box; 202 | 203 | } 204 | 205 | div.highlight-source-ruby pre::before{ 206 | content: ""; 207 | color: black; 208 | display: block; 209 | position: absolute; 210 | right: 0px; 211 | background-color:lightblue; 212 | box-sizing: border-box; 213 | box-shadow: 1px 1px 1px white; 214 | 215 | 216 | } 217 | 218 | 219 | blockquote { 220 | color: #666; 221 | margin-bottom: 20px; 222 | padding: 0 0 0 20px; 223 | border-left: 3px solid #bbb; 224 | } 225 | 226 | 227 | ul, ol, dl { 228 | margin-bottom: 15px 229 | } 230 | 231 | ul { 232 | list-style-position: inside; 233 | list-style: disc; 234 | padding-left: 20px; 235 | } 236 | 237 | ol { 238 | list-style-position: inside; 239 | list-style: decimal; 240 | padding-left: 20px; 241 | } 242 | 243 | dl dt { 244 | font-weight: bold; 245 | } 246 | 247 | dl dd { 248 | padding-left: 20px; 249 | font-style: italic; 250 | } 251 | 252 | dl p { 253 | padding-left: 20px; 254 | font-style: italic; 255 | } 256 | 257 | hr { 258 | height: 1px; 259 | margin-bottom: 5px; 260 | border: none; 261 | background: url('../images/bg_hr.png') repeat-x center; 262 | } 263 | 264 | table { 265 | border: 1px solid #373737; 266 | margin-bottom: 20px; 267 | text-align: left; 268 | } 269 | 270 | th { 271 | font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; 272 | padding: 10px; 273 | background: #373737; 274 | color: #fff; 275 | } 276 | 277 | td { 278 | padding: 10px; 279 | border: 1px solid #373737; 280 | } 281 | 282 | form { 283 | background: #f2f2f2; 284 | padding: 20px; 285 | } 286 | 287 | /******************************************************************************* 288 | Full-Width Styles 289 | *******************************************************************************/ 290 | 291 | .outer { 292 | width: 100%; 293 | } 294 | 295 | .inner { 296 | position: relative; 297 | max-width: 640px; 298 | padding: 20px 10px; 299 | margin: 0 auto; 300 | } 301 | 302 | #forkme_banner { 303 | display: block; 304 | position: absolute; 305 | top:0; 306 | right: 10px; 307 | z-index: 10; 308 | padding: 10px 50px 10px 10px; 309 | color: #fff; 310 | background: url('../images/blacktocat.png') #0090ff no-repeat 95% 50%; 311 | font-weight: 700; 312 | box-shadow: 0 0 10px rgba(0,0,0,.5); 313 | border-bottom-left-radius: 2px; 314 | border-bottom-right-radius: 2px; 315 | } 316 | 317 | #header_wrap { 318 | background: #212121; 319 | background: -moz-linear-gradient(top, #373737, #212121); 320 | background: -webkit-linear-gradient(top, #373737, #212121); 321 | background: -ms-linear-gradient(top, #373737, #212121); 322 | background: -o-linear-gradient(top, #373737, #212121); 323 | background: linear-gradient(top, #373737, #212121); 324 | } 325 | 326 | #header_wrap .inner { 327 | padding: 50px 10px 30px 10px; 328 | } 329 | 330 | #project_title { 331 | margin: 0; 332 | color: #fff; 333 | font-size: 42px; 334 | font-weight: 700; 335 | text-shadow: #111 0px 0px 10px; 336 | } 337 | 338 | #project_tagline { 339 | color: #fff; 340 | font-size: 24px; 341 | font-weight: 300; 342 | background: none; 343 | text-shadow: #111 0px 0px 10px; 344 | } 345 | 346 | #downloads { 347 | position: absolute; 348 | width: 210px; 349 | z-index: 10; 350 | bottom: -40px; 351 | right: 0; 352 | height: 70px; 353 | background: url('../images/icon_download.png') no-repeat 0% 90%; 354 | } 355 | 356 | .zip_download_link { 357 | display: block; 358 | float: right; 359 | width: 90px; 360 | height:70px; 361 | text-indent: -5000px; 362 | overflow: hidden; 363 | background: url(../images/sprite_download.png) no-repeat bottom left; 364 | } 365 | 366 | .tar_download_link { 367 | display: block; 368 | float: right; 369 | width: 90px; 370 | height:70px; 371 | text-indent: -5000px; 372 | overflow: hidden; 373 | background: url(../images/sprite_download.png) no-repeat bottom right; 374 | margin-left: 10px; 375 | } 376 | 377 | .zip_download_link:hover { 378 | background: url(../images/sprite_download.png) no-repeat top left; 379 | } 380 | 381 | .tar_download_link:hover { 382 | background: url(../images/sprite_download.png) no-repeat top right; 383 | } 384 | 385 | #main_content_wrap { 386 | background: #f2f2f2; 387 | border-top: 1px solid #111; 388 | border-bottom: 1px solid #111; 389 | } 390 | 391 | #main_content { 392 | padding-top: 40px; 393 | } 394 | 395 | #footer_wrap { 396 | background: #212121; 397 | } 398 | 399 | 400 | 401 | /******************************************************************************* 402 | Small Device Styles 403 | *******************************************************************************/ 404 | 405 | @media screen and (max-width: 480px) { 406 | body { 407 | font-size:14px; 408 | } 409 | 410 | #downloads { 411 | display: none; 412 | } 413 | 414 | .inner { 415 | min-width: 320px; 416 | max-width: 480px; 417 | } 418 | 419 | #project_title { 420 | font-size: 32px; 421 | } 422 | 423 | h1 { 424 | font-size: 28px; 425 | } 426 | 427 | h2 { 428 | font-size: 24px; 429 | } 430 | 431 | h3 { 432 | font-size: 21px; 433 | } 434 | 435 | h4 { 436 | font-size: 18px; 437 | } 438 | 439 | h5 { 440 | font-size: 14px; 441 | } 442 | 443 | h6 { 444 | font-size: 12px; 445 | } 446 | 447 | code, pre { 448 | min-width: 320px; 449 | max-width: 480px; 450 | font-size: 11px; 451 | } 452 | 453 | } 454 | -------------------------------------------------------------------------------- /_site/tutorial.md: -------------------------------------------------------------------------------- 1 | 2 | ## Basics 3 | 4 | ### Data Types 5 | 6 | 7 | ```ruby 8 | my_num = 15 9 | my_string = "Tushar" 10 | my_bool = true 11 | 12 | puts my_num, my_string, my_bool 13 | ``` 14 | 15 | 15 16 | Tushar 17 | true 18 | 19 | 20 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 21 | 22 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 23 | 24 | 25 | ```ruby 26 | print my_num, my_string, my_bool 27 | ``` 28 | 29 | 15Tushartrue 30 | 31 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 32 | 33 | Everything is a an Object in Ruby, 34 | 35 | Everything! 36 | 37 | To find out class of a ruby object we can call a 'class' method on any object. 38 | 39 | 40 | ```ruby 41 | puts my_num.class() , my_string.class() , my_bool.class() 42 | ``` 43 | 44 | Fixnum 45 | String 46 | TrueClass 47 | 48 | 49 | 50 | ```ruby 51 | 52 | ``` 53 | -------------------------------------------------------------------------------- /examples/rspec_testing/Document.rb: -------------------------------------------------------------------------------- 1 | class Document 2 | def initialize (author,title,content) 3 | @author = author 4 | @title = title 5 | @content = content 6 | end 7 | 8 | def author 9 | @author 10 | end 11 | 12 | def title 13 | @title 14 | end 15 | 16 | 17 | def content 18 | @content 19 | end 20 | 21 | def words 22 | @content.split() 23 | end 24 | 25 | def word_count 26 | words.length 27 | end 28 | 29 | 30 | end 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /examples/rspec_testing/document_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'document.rb' 3 | 4 | describe Document do 5 | before :each do 6 | @title = "Test" 7 | @author = "Tushar" 8 | @content = "Ruby is fun" 9 | @document = Document.new(@author,@title,@content) 10 | 11 | end 12 | 13 | it "tests if a document returns a new object" do 14 | puts "this test was run" 15 | expect(@document).not_to eq(nil) 16 | #document.should_not == nil 17 | end 18 | 19 | it "words should be 3" do 20 | expect(@document.word_count).to eq(3) 21 | end 22 | 23 | it "words should include all the words" do 24 | expect(@document.words).to include("Ruby") 25 | expect(@document.words).to include("is") 26 | expect(@document.words).to include("fun") 27 | end 28 | 29 | end 30 | 31 | -------------------------------------------------------------------------------- /examples/stub_testing/Document.rb: -------------------------------------------------------------------------------- 1 | class Document 2 | def initialize (author,title,content) 3 | @author = author 4 | @title = title 5 | @content = content 6 | end 7 | 8 | def author 9 | @author 10 | end 11 | 12 | def title 13 | @title 14 | end 15 | 16 | 17 | def content 18 | @content 19 | end 20 | 21 | def words 22 | @content.split() 23 | end 24 | 25 | def word_count 26 | words.length 27 | end 28 | 29 | 30 | end 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /examples/stub_testing/document_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'document.rb' 3 | 4 | describe Document do 5 | before :each do 6 | @title = "Test" 7 | @author = "Tushar" 8 | @content = "Ruby is fun" 9 | @document = Document.new(@author,@title,@content) 10 | 11 | end 12 | 13 | it "tests if a document returns a new object" do 14 | puts "this test was run" 15 | expect(@document).not_to eq(nil) 16 | #document.should_not == nil 17 | end 18 | 19 | it "words should be 3" do 20 | expect(@document.word_count).to eq(3) 21 | end 22 | 23 | it "words should include all the words" do 24 | expect(@document.words).to include("Ruby") 25 | expect(@document.words).to include("is") 26 | expect(@document.words).to include("fun") 27 | end 28 | 29 | end 30 | 31 | -------------------------------------------------------------------------------- /examples/stub_testing/printable_document.rb: -------------------------------------------------------------------------------- 1 | require_relative 'document.rb' 2 | class PrintableDocument < Document 3 | def print(printer) 4 | return "Printer Unavailable" unless printer.available? 5 | printer.render(@title) 6 | printer.render(@author) 7 | printer.render(@content) 8 | return "Done" 9 | end 10 | 11 | end 12 | 13 | -------------------------------------------------------------------------------- /examples/stub_testing/printable_document_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rspec' 2 | require_relative 'printable_document.rb' 3 | 4 | describe PrintableDocument do 5 | 6 | before :each do 7 | @author = "Tushar Tuteja" 8 | @title = "Test" 9 | @content = "Ruby is getting more fun" 10 | @printable_document = PrintableDocument.new(@author,@title,@content) 11 | 12 | end 13 | 14 | it "document should be created" do 15 | expect(@printable_document).not_to eq(nil) 16 | puts "printable document" 17 | end 18 | 19 | it "prints the command" do 20 | printer = Object.new 21 | allow(printer).to receive_messages(:available? => true, :render => nil) 22 | expect( @printable_document.print( printer ) ).to eq("Done") 23 | end 24 | 25 | 26 | end 27 | 28 | -------------------------------------------------------------------------------- /examples/stub_testing/printable_document_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require_relative 'printable_document.rb' 3 | 4 | class PrintableDocumentTest < Test::Unit::TestCase 5 | def setup 6 | @author = "Tushar Tuteja" 7 | @title = "Test" 8 | @content = "Ruby is getting more fun" 9 | @printable_document = PrintableDocument.new(@author,@title,@content) 10 | end 11 | 12 | 13 | def test_print 14 | my_printer = Object.new 15 | class << my_printer 16 | def available? 17 | true 18 | end 19 | 20 | def render content 21 | nil 22 | end 23 | 24 | end 25 | 26 | assert_equal (@printable_document.print(my_printer)),"Done" 27 | end 28 | 29 | 30 | end -------------------------------------------------------------------------------- /examples/unit_testing/Document.rb: -------------------------------------------------------------------------------- 1 | class Document 2 | def initialize (author,title,content) 3 | @author = author 4 | @title = title 5 | @content = content 6 | end 7 | 8 | def author 9 | @author 10 | end 11 | 12 | def title 13 | @title 14 | end 15 | 16 | 17 | def content 18 | @content 19 | end 20 | 21 | def words 22 | @content.split() 23 | end 24 | 25 | def word_count 26 | words.length 27 | end 28 | 29 | 30 | end 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /examples/unit_testing/document_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require_relative 'document.rb' 3 | class DocumentTest < Test::Unit::TestCase 4 | def setup 5 | @title = "Test" 6 | @authur = "Tushar" 7 | @content = "This is a test document" 8 | 9 | @document = Document.new(@authur,@title,@content) 10 | puts "inside setup method" 11 | end 12 | 13 | 14 | 15 | def test_doc_creation 16 | assert_not_equal @document,nil 17 | end 18 | def test_doc_author 19 | assert_equal @authur,@document.author 20 | end 21 | def test_doc_content 22 | assert_equal @content,@document.content 23 | end 24 | 25 | 26 | 27 | 28 | def test_doc_title 29 | assert_equal @title, @document.title 30 | end 31 | 32 | def teardown 33 | puts "Inside Tear Down Method" 34 | end 35 | 36 | end 37 | 38 | -------------------------------------------------------------------------------- /examples/unit_testing/my_test.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require_relative 'document.rb' 3 | 4 | class MyTest < Test::Unit::TestCase 5 | def setup 6 | @title = "Test" 7 | @authur = "Tushar" 8 | @content = "This is a test document" 9 | @document = Document.new(@author,@title,@content) 10 | puts @document.hash 11 | end 12 | 13 | def test_check_word_count 14 | 15 | assert_equal 5, @document.word_count 16 | end 17 | 18 | def test_check_content 19 | 20 | assert_equal @content , @document.content 21 | end 22 | 23 | def test_raise 24 | assert_raise {1/0} 25 | end 26 | 27 | 28 | def teardown 29 | puts "tear down" 30 | end 31 | 32 | 33 | end -------------------------------------------------------------------------------- /index.md: -------------------------------------------------------------------------------- 1 | 2 | # RubyTutorial 3 | 4 | We are using Jupyter Notebooks to make this tutorial. 5 | For those who don't know about Jupyter Notebooks, go [here](https://jupyter.org/). 6 | Below is a sample, about how notebooks work. 7 | 8 | ![alt text](https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/master/notebook_demo.gif "Without Smooth Scroll") 9 | 10 | ## Basics 11 | 12 | ### Data Types 13 | 14 | 15 | ```ruby 16 | my_num = 15 17 | my_string = "Tushar" 18 | my_bool = true 19 | my_own_string = "tuteja" 20 | 21 | puts my_num, my_string, my_bool 22 | puts my_own_string 23 | ``` 24 | 25 | 15 26 | Tushar 27 | true 28 | tuteja 29 | 30 | 31 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 32 | 33 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 34 | 35 | 36 | ```ruby 37 | print my_num, my_string, my_bool 38 | ``` 39 | 40 | 15Tushartrue 41 | 42 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 43 | 44 | Everything is a an Object in Ruby, 45 | 46 | Everything! 47 | 48 | To find out class of a ruby object we can call a 'class' method on any object. 49 | 50 | 51 | ```ruby 52 | puts my_num.class() , my_string.class() , my_bool.class() 53 | ``` 54 | 55 | Fixnum 56 | String 57 | TrueClass 58 | 59 | 60 | ## Arithmetic 61 | 62 | 63 | Basic Arithmetic operators are '+','-','*','/' and '%'. 64 | 65 | 66 | ```ruby 67 | five = 5 68 | two = 2 69 | seven = 5 + 2 70 | 71 | ``` 72 | 73 | 74 | 75 | 76 | 7 77 | 78 | 79 | 80 | 81 | ```ruby 82 | ten = five * two 83 | ``` 84 | 85 | 86 | 87 | 88 | 10 89 | 90 | 91 | 92 | 93 | ```ruby 94 | one = five % two 95 | ``` 96 | 97 | 98 | 99 | 100 | 1 101 | 102 | 103 | 104 | 105 | ```ruby 106 | two = five / two 107 | ``` 108 | 109 | 110 | 111 | 112 | 2 113 | 114 | 115 | 116 | 117 | ```ruby 118 | three = five - two 119 | 120 | ``` 121 | 122 | 123 | 124 | 125 | 3 126 | 127 | 128 | 129 | ## Strings 130 | 131 | 132 | ```ruby 133 | my_string = "Programming in Ruby Is Fun." 134 | ``` 135 | 136 | 137 | 138 | 139 | "Programming in Ruby Is Fun." 140 | 141 | 142 | 143 | 144 | ```ruby 145 | my_string.length() 146 | ``` 147 | 148 | 149 | 150 | 151 | 27 152 | 153 | 154 | 155 | 156 | ```ruby 157 | my_string.reverse() 158 | ``` 159 | 160 | 161 | 162 | 163 | ".nuF sI ybuR ni gnimmargorP" 164 | 165 | 166 | 167 | 168 | ```ruby 169 | puts my_string 170 | ``` 171 | 172 | Programming in Ruby Is Fun. 173 | 174 | 175 | 176 | ```ruby 177 | my_string.reverse!() 178 | ``` 179 | 180 | 181 | 182 | 183 | ".nuF sI ybuR ni gnimmargorP" 184 | 185 | 186 | 187 | 188 | ```ruby 189 | puts my_string 190 | ``` 191 | 192 | .nuF sI ybuR ni gnimmargorP 193 | 194 | 195 | 196 | ```ruby 197 | my_string = "Programming in Ruby Is Fun." 198 | my_string.split(" ") 199 | ``` 200 | 201 | 202 | 203 | 204 | ["Programming", "in", "Ruby", "Is", "Fun."] 205 | 206 | 207 | 208 | 209 | ```ruby 210 | words = my_string.split(" ") 211 | puts words #Gives an array, we'll come to it 212 | ``` 213 | 214 | ["Programming", "in", "Ruby", "Is", "Fun."] 215 | 216 | 217 | 218 | ```ruby 219 | word = "Programming" 220 | ``` 221 | 222 | 223 | 224 | 225 | "Programming" 226 | 227 | 228 | 229 | 230 | ```ruby 231 | word.downcase() #Get a word with lower case letters 232 | ``` 233 | 234 | 235 | 236 | 237 | "programming" 238 | 239 | 240 | 241 | 242 | ```ruby 243 | word.upcase() # get a word with upper case letters 244 | ``` 245 | 246 | 247 | 248 | 249 | "PROGRAMMING" 250 | 251 | 252 | 253 | 254 | ```ruby 255 | #This is a single line comment 256 | puts "comment" #this can come at the end of any expression 257 | =begin 258 | This is a multiline comment. 259 | =begin should start at the first character of the line to be it a comment, other wise it wont work 260 | 261 | =end 262 | ``` 263 | 264 | comment 265 | 266 | 267 | ## Naming Conventions 268 | 269 | For methods and variables we follow snake case like example_variable, example_function. 270 | For Classes we follow upper case letters, MyOwnClass. 271 | For functions which are risky to use, like reverse!, we add exclamation mark at the end. 272 | 273 | ## Function Chaining 274 | 275 | 276 | 277 | ```ruby 278 | my_string = "tushar tuteja" 279 | ``` 280 | 281 | 282 | 283 | 284 | "tushar tuteja" 285 | 286 | 287 | 288 | 289 | ```ruby 290 | my_string.reverse().upcase() # You can chain function calls one after the other 291 | ``` 292 | 293 | 294 | 295 | 296 | "AJETUT RAHSUT" 297 | 298 | 299 | 300 | ## String Formatter 301 | 302 | 303 | ```ruby 304 | my_name = "Tushar Tuteja" 305 | my_city = "Delhi" 306 | puts "Hi, I am #{my_name}, I belong to #{my_city}." #way to format complex strings. 307 | ``` 308 | 309 | Hi, I am Tushar Tuteja, I belong to Delhi. 310 | 311 | 312 | ## Control Flow 313 | 314 | 315 | ```ruby 316 | if true 317 | puts "true" 318 | else 319 | puts "false" 320 | end 321 | 322 | ``` 323 | 324 | true 325 | 326 | 327 | 328 | ```ruby 329 | false == "false" # this is false 330 | ``` 331 | 332 | 333 | 334 | 335 | false 336 | 337 | 338 | 339 | 340 | ```ruby 341 | false == nil # this is false 342 | ``` 343 | 344 | 345 | 346 | 347 | false 348 | 349 | 350 | 351 | 352 | ```ruby 353 | my_object = nil 354 | ``` 355 | 356 | 357 | ```ruby 358 | my_object == nil 359 | ``` 360 | 361 | 362 | 363 | 364 | true 365 | 366 | 367 | 368 | 369 | ```ruby 370 | my_object.nil? # this is the way to check nil 371 | ``` 372 | 373 | 374 | 375 | 376 | true 377 | 378 | 379 | 380 | Ruby has infinite true expressions, only two false expressions, one is false and the other is nil. So 0 would evaluate to true, this is different form C and C++ language. 381 | 382 | 383 | ```ruby 384 | if my_object 385 | puts "true" 386 | else 387 | puts "nil or false" 388 | end 389 | 390 | ``` 391 | 392 | nil or false 393 | 394 | 395 | 396 | ```ruby 397 | my_object = 0 398 | if my_object 399 | puts "this evaluates to true" 400 | else 401 | puts "should have been false if we were coding in C." 402 | end 403 | 404 | ``` 405 | 406 | this evaluates to true 407 | 408 | 409 | 410 | ```ruby 411 | first_condition = false 412 | second_condition = true 413 | 414 | if first_condition 415 | puts "first condition is true" 416 | elsif second_condition 417 | puts "second condition is true" 418 | elsif 0 > 3 419 | puts "won't happen" 420 | end 421 | 422 | ``` 423 | 424 | second condition is true 425 | 426 | 427 | 428 | ```ruby 429 | hungry = true 430 | ``` 431 | 432 | 433 | 434 | 435 | true 436 | 437 | 438 | 439 | 440 | ```ruby 441 | if not hungry 442 | puts "keep working" 443 | else 444 | puts "eat" 445 | end 446 | ``` 447 | 448 | eat 449 | 450 | 451 | Ruby gives us more than if else, it gives us unless else, which is more verbose than if not. Example 452 | 453 | 454 | ```ruby 455 | unless hungry 456 | puts "keep working" 457 | else 458 | puts "Eat" 459 | end 460 | 461 | ``` 462 | 463 | Eat 464 | 465 | 466 | ### Conditional Operators : == , >= ,<= ,!=, ! 467 | 468 | 469 | 470 | ```ruby 471 | 5 == '5' 472 | ``` 473 | 474 | 475 | 476 | 477 | false 478 | 479 | 480 | 481 | 482 | ```ruby 483 | 5*4 == 4*5 484 | ``` 485 | 486 | 487 | 488 | 489 | true 490 | 491 | 492 | 493 | 494 | ```ruby 495 | 4 < 7 496 | ``` 497 | 498 | 499 | 500 | 501 | true 502 | 503 | 504 | 505 | 506 | ```ruby 507 | 8 > 9 508 | 509 | ``` 510 | 511 | 512 | 513 | 514 | false 515 | 516 | 517 | 518 | 519 | ```ruby 520 | "tushar" > "tuteja" 521 | ``` 522 | 523 | 524 | 525 | 526 | false 527 | 528 | 529 | 530 | 531 | ```ruby 532 | 5 >= 5 533 | ``` 534 | 535 | 536 | 537 | 538 | true 539 | 540 | 541 | 542 | 543 | ```ruby 544 | 4 != 5 545 | ``` 546 | 547 | 548 | 549 | 550 | true 551 | 552 | 553 | 554 | 555 | ```ruby 556 | !true 557 | ``` 558 | 559 | 560 | 561 | 562 | false 563 | 564 | 565 | 566 | ### Composite Conditional Statements 567 | 568 | 569 | ```ruby 570 | work_done = true 571 | if work_done || hungry 572 | puts "eat" 573 | else 574 | puts "work" 575 | end 576 | ``` 577 | 578 | eat 579 | 580 | 581 | 582 | ```ruby 583 | puts false || true 584 | puts true || false 585 | puts true && true 586 | puts true && false 587 | puts false || false 588 | ``` 589 | 590 | true 591 | true 592 | true 593 | false 594 | false 595 | 596 | 597 | 598 | ```ruby 599 | if not false 600 | puts "it is true" 601 | else 602 | puts "it is false" 603 | end 604 | 605 | ``` 606 | 607 | it is true 608 | 609 | 610 | ## Loops 611 | 612 | 613 | ```ruby 614 | i = 0 615 | while i < 5 616 | puts "ruby is fun" 617 | i = i + 1 618 | end 619 | 620 | ``` 621 | 622 | ruby is fun 623 | ruby is fun 624 | ruby is fun 625 | ruby is fun 626 | ruby is fun 627 | 628 | 629 | 630 | ```ruby 631 | i = 5 632 | while i != 0 633 | puts "ruby is more verbose" 634 | i = i - 1 635 | end 636 | ``` 637 | 638 | ruby is more verbose 639 | ruby is more verbose 640 | ruby is more verbose 641 | ruby is more verbose 642 | ruby is more verbose 643 | 644 | 645 | 646 | ```ruby 647 | i = 5 648 | until i == 0 649 | puts "ruby is more verbose" 650 | i = i - 1 651 | end 652 | 653 | ``` 654 | 655 | ruby is more verbose 656 | ruby is more verbose 657 | ruby is more verbose 658 | ruby is more verbose 659 | ruby is more verbose 660 | 661 | 662 | 663 | ```ruby 664 | for i in 1..5 665 | puts "ruby has many ways to do the same thing." 666 | end 667 | ``` 668 | 669 | ruby has many ways to do the same thing. 670 | ruby has many ways to do the same thing. 671 | ruby has many ways to do the same thing. 672 | ruby has many ways to do the same thing. 673 | ruby has many ways to do the same thing. 674 | 675 | 676 | 677 | 678 | 679 | 1..5 680 | 681 | 682 | 683 | 684 | ```ruby 685 | for i in 1...5 686 | puts "..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5" 687 | end 688 | ``` 689 | 690 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 691 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 692 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 693 | ..(two dots) inclues 1 and 5 both, where as ...(three dots) doesn't include 5 694 | 695 | 696 | 697 | 698 | 699 | 1...5 700 | 701 | 702 | 703 | 704 | ```ruby 705 | 5.times do 706 | puts "this needs to be done 5 times" 707 | end 708 | ``` 709 | 710 | this needs to be done 5 times 711 | this needs to be done 5 times 712 | this needs to be done 5 times 713 | this needs to be done 5 times 714 | this needs to be done 5 times 715 | 716 | 717 | 718 | 719 | 720 | 5 721 | 722 | 723 | 724 | 725 | ```ruby 726 | my_string = "Ruby is made for productivity." 727 | words = my_string.split(" ") 728 | words.each do |word| #would discuss more of this in arrays and hashes 729 | puts word.capitalize # This would capitalize every word 730 | end 731 | ``` 732 | 733 | Ruby 734 | Is 735 | Made 736 | For 737 | Productivity. 738 | 739 | 740 | 741 | 742 | 743 | ["Ruby", "is", "made", "for", "productivity."] 744 | 745 | 746 | 747 | ## Arrays 748 | 749 | 750 | ```ruby 751 | a = [] # this creates an empty array 752 | ``` 753 | 754 | 755 | 756 | 757 | [] 758 | 759 | 760 | 761 | Arrays are lists in ruby, means they doesn't need to be homegenous. 762 | 763 | 764 | ```ruby 765 | a = ["tushar", 5, [4,5,6]] 766 | ``` 767 | 768 | 769 | 770 | 771 | ["tushar", 5, [4, 5, 6]] 772 | 773 | 774 | 775 | Arrays are continuous data structures 776 | 777 | 778 | 779 | ```ruby 780 | a = [] 781 | a[45] = 5 782 | puts a 783 | ``` 784 | 785 | [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 5] 786 | 787 | 788 | a[45] = 0, created 45 empty spaces and then added 5 at 45th index. Indexing starts from zero. 789 | 790 | 791 | ```ruby 792 | puts a[45] 793 | ``` 794 | 795 | 5 796 | 797 | 798 | 799 | ```ruby 800 | puts a[0] # Would print nothing 801 | ``` 802 | 803 | 804 | 805 | 806 | 807 | ```ruby 808 | puts a.length 809 | ``` 810 | 811 | 46 812 | 813 | 814 | ## Sets 815 | 816 | Sets are used to keep unique elements, no duplicates are allowed. 817 | 818 | 819 | 820 | ```ruby 821 | my_set = Set.new() # New Empty Set 822 | my_set.add(1) # Add one Element 823 | 824 | my_set.each do |v| 825 | puts v 826 | end 827 | 828 | my_set.add([1,2,3,4,5]) # adding [1,2,3,4,5] as a single element 829 | 830 | my_set.each do |v| 831 | puts v 832 | end 833 | 834 | 835 | ``` 836 | 837 | 1 838 | 1 839 | [1, 2, 3, 4, 5] 840 | 841 | 842 | 843 | 844 | 845 | # 846 | 847 | 848 | 849 | 850 | ```ruby 851 | a = [1,2,3,4,5,1,2,3,4,5] 852 | my_set = a.to_set # Easy way to create a set from array 853 | puts my_set.size 854 | ``` 855 | 856 | 5 857 | 858 | 859 | 860 | ```ruby 861 | my_set = Set.new([1,2,3,1,2,3]) # Easy way to create a set while initializing 862 | ``` 863 | 864 | 865 | 866 | 867 | # 868 | 869 | 870 | 871 | ## Hashes 872 | 873 | Hashes are smilar to javascript dictionaries, the only difference is that they are ordered. 874 | 875 | 876 | ```ruby 877 | my_hash = {} # creates a new Hash 878 | 879 | ``` 880 | 881 | 882 | 883 | 884 | {} 885 | 886 | 887 | 888 | 889 | ```ruby 890 | my_hash = Hash.new # creates a new Hash 891 | ``` 892 | 893 | 894 | 895 | 896 | {} 897 | 898 | 899 | 900 | 901 | ```ruby 902 | my_hash = {:name => "Tushar", :city => "Delhi"} # Creates a hash 903 | ``` 904 | 905 | 906 | 907 | 908 | {:name=>"Tushar", :city=>"Delhi"} 909 | 910 | 911 | 912 | 913 | ```ruby 914 | my_hash[:name] # to access :name from my_hash 915 | ``` 916 | 917 | 918 | 919 | 920 | "Tushar" 921 | 922 | 923 | 924 | :name and :city are actually symbols. they are used for faster accessing, we may use strings as well. 925 | 926 | 927 | 928 | ```ruby 929 | my_hash = {"name" => "tushar"} 930 | ``` 931 | 932 | 933 | 934 | 935 | {"name"=>"tushar"} 936 | 937 | 938 | 939 | 940 | ```ruby 941 | puts my_hash["name"] 942 | puts my_hash[:name] # This won't work, you need to remember that strings and symbols are different 943 | ``` 944 | 945 | tushar 946 | 947 | 948 | 949 | 950 | ```ruby 951 | my_hash = {:name => "Tushar", :city => "Delhi", :age => 25} 952 | ``` 953 | 954 | 955 | 956 | 957 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 958 | 959 | 960 | 961 | 962 | ```ruby 963 | my_hash.each do |key| # iterating over the keys 964 | puts key 965 | end 966 | 967 | ``` 968 | 969 | [:name, "Tushar"] 970 | [:city, "Delhi"] 971 | [:age, 25] 972 | 973 | 974 | 975 | 976 | 977 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 978 | 979 | 980 | 981 | If you look at the above code, the keys are ordered. 982 | 983 | 984 | ```ruby 985 | my_hash.each do |key,value| 986 | puts "#{key} : #{value}" 987 | end 988 | ``` 989 | 990 | name : Tushar 991 | city : Delhi 992 | age : 25 993 | 994 | 995 | 996 | 997 | 998 | {:name=>"Tushar", :city=>"Delhi", :age=>25} 999 | 1000 | 1001 | 1002 | ## Methods 1003 | 1004 | 1005 | 1006 | ```ruby 1007 | def my_method 1008 | puts "hello method" 1009 | end 1010 | 1011 | ``` 1012 | 1013 | 1014 | 1015 | 1016 | :my_method 1017 | 1018 | 1019 | 1020 | 1021 | ```ruby 1022 | my_method() 1023 | ``` 1024 | 1025 | hello method 1026 | 1027 | 1028 | 1029 | ```ruby 1030 | my_method # parenthesis are optional in ruby, it makes the code more verbose 1031 | ``` 1032 | 1033 | hello method 1034 | 1035 | 1036 | 1037 | ```ruby 1038 | def is_odd number # parenthesis are optional 1039 | return number % 2 == 1 1040 | end 1041 | 1042 | is_odd 1 # parenthesis are optional 1043 | ``` 1044 | 1045 | 1046 | 1047 | 1048 | true 1049 | 1050 | 1051 | 1052 | 1053 | ```ruby 1054 | def is_even number 1055 | number % 2 == 0 #last expression in every function is a return statement by default. 1056 | end 1057 | 1058 | ``` 1059 | 1060 | 1061 | 1062 | 1063 | :is_even 1064 | 1065 | 1066 | 1067 | 1068 | ```ruby 1069 | is_even 2 1070 | ``` 1071 | 1072 | 1073 | 1074 | 1075 | true 1076 | 1077 | 1078 | 1079 | 1080 | ```ruby 1081 | def join_words words_array 1082 | result = "" 1083 | words_array.each do |word| 1084 | result = result + word + " " # String concatenation 1085 | end 1086 | result 1087 | end 1088 | 1089 | puts join_words ["Ruby","is","flexible"] 1090 | ``` 1091 | 1092 | Ruby is flexible 1093 | 1094 | 1095 | What is you want to pass a variable number of arguments and you don't want to pass an array. 1096 | 1097 | Voila, we have *args arguement. 1098 | 1099 | 1100 | ```ruby 1101 | def join_words *args 1102 | result = "" 1103 | args.each do |word| 1104 | result = result + word + " " # String concatenation 1105 | end 1106 | result 1107 | end 1108 | 1109 | puts join_words "Ruby","is","flexible" # This code is more verbose 1110 | puts join_words "Ruby","is","flexible",",", "This", "code" ,"is" ,"more" ,"verbose" 1111 | 1112 | ``` 1113 | 1114 | Ruby is flexible 1115 | Ruby is flexible , This code is more verbose 1116 | 1117 | 1118 | ## Classes 1119 | 1120 | 1121 | ```ruby 1122 | class MyClass # Defines a new Class 1123 | end 1124 | 1125 | ``` 1126 | 1127 | 1128 | ```ruby 1129 | my_class = MyClass.new # Creates a new instance of MyClass Object 1130 | ``` 1131 | 1132 | 1133 | 1134 | 1135 | # 1136 | 1137 | 1138 | 1139 | By default every class Inherits from Object class. When We Called MyClass.new , we called a constructor of Object class. 1140 | 1141 | 1142 | ```ruby 1143 | class MyClass 1144 | def initialize #Constructor method 1145 | puts "Constructor of MyClass is called" 1146 | end 1147 | end 1148 | 1149 | ``` 1150 | 1151 | 1152 | 1153 | 1154 | :initialize 1155 | 1156 | 1157 | 1158 | 1159 | ```ruby 1160 | my_class = MyClass.new 1161 | ``` 1162 | 1163 | Constructor of MyClass is called 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | # 1170 | 1171 | 1172 | 1173 | Classes contain methods and variables. 1174 | By Default Methods are of two types 1175 | * Instance Methods, that belongs to one instance of a class 1176 | * class methods, that belongs to one class. 1177 | Visibility of Methods is of three types 1178 | * private 1179 | * protected 1180 | * public, by default every method is public. 1181 | 1182 | Variables could be of three types 1183 | * Local Variable in a function 1184 | * Instance variables 1185 | * Class variables 1186 | 1187 | We'll discuss all of above in the following section 1188 | 1189 | 1190 | ```ruby 1191 | class Person 1192 | def initialize (name,age) 1193 | @name = name # this is how we declare instance variable, by having @ at the start 1194 | @age = age # same with age 1195 | end 1196 | 1197 | end 1198 | 1199 | ``` 1200 | 1201 | 1202 | 1203 | 1204 | :initialize 1205 | 1206 | 1207 | 1208 | 1209 | ```ruby 1210 | person = Person.new("Tushar",25) 1211 | ``` 1212 | 1213 | 1214 | 1215 | 1216 | # 1217 | 1218 | 1219 | 1220 | 1221 | ```ruby 1222 | person.public_methods 1223 | ``` 1224 | 1225 | 1226 | 1227 | 1228 | [:name, :name=, :details, :is_adult, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :is_a?, :extend, :define_singleton_method, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :display, :send, :object_id, :to_s, :method, :public_method, :singleton_method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] 1229 | 1230 | 1231 | 1232 | We didn't define public_methods function, but it was called and it gave us some results. This is method that every object inherits from Object class. 1233 | 1234 | public_method functions gives us all the functions that could be called on that Object. In total Object class gives us 56 methods, which is a good thing and a bad thing. Bad thing as in now we have 56 names where our own name can collide. example, if we were to make SMS class, we would have send method, that would override the send method from Object class. 1235 | 1236 | 1237 | Lets add some more functionality to our Person class 1238 | 1239 | 1240 | ```ruby 1241 | class Person 1242 | def initialize (name,age) 1243 | @name = name # this is how we declare instance variable, by having @ at the start 1244 | @age = age # same with age 1245 | end 1246 | 1247 | def details # By default this method would be public 1248 | "Name: #{@name}" 1249 | end 1250 | 1251 | 1252 | 1253 | end 1254 | 1255 | ``` 1256 | 1257 | 1258 | 1259 | 1260 | :details 1261 | 1262 | 1263 | 1264 | 1265 | ```ruby 1266 | person = Person.new("Tushar",25) 1267 | ``` 1268 | 1269 | 1270 | 1271 | 1272 | # 1273 | 1274 | 1275 | 1276 | 1277 | ```ruby 1278 | person.details 1279 | ``` 1280 | 1281 | 1282 | 1283 | 1284 | "Name: Tushar" 1285 | 1286 | 1287 | 1288 | Every Instance variable in any object is a private variable. You need to write getter methods for them. I can not access name and age directly from the person Object. So lets add those methods. This is a good news, as we don't want to reveal the age of the person, but we should have a way to check if the person is adult or not. 1289 | 1290 | 1291 | ```ruby 1292 | class Person 1293 | def initialize (name,age) 1294 | @name = name # this is how we declare instance variable, by having @ at the start 1295 | @age = age # same with age 1296 | end 1297 | 1298 | def details # By default this method would be public 1299 | "Name: #{@name}" 1300 | end 1301 | 1302 | def name 1303 | @name 1304 | end 1305 | 1306 | def is_adult 1307 | @age > 18 1308 | end 1309 | 1310 | 1311 | 1312 | 1313 | end 1314 | 1315 | ``` 1316 | 1317 | 1318 | 1319 | 1320 | :is_adult 1321 | 1322 | 1323 | 1324 | 1325 | ```ruby 1326 | person = Person.new("Tushar",25) 1327 | puts person.name, person.is_adult 1328 | 1329 | ``` 1330 | 1331 | Tushar 1332 | true 1333 | 1334 | 1335 | We cannot change name or age once set, what if we want to change them, we need to add a setter method. Which is done in ruby as follows. 1336 | 1337 | 1338 | ```ruby 1339 | class Person 1340 | def initialize (name,age) 1341 | @name = name # this is how we declare instance variable, by having @ at the start 1342 | @age = age # same with age 1343 | end 1344 | 1345 | def details # By default this method would be public 1346 | "Name: #{@name}" 1347 | end 1348 | 1349 | def name 1350 | @name 1351 | end 1352 | 1353 | def name= name # we are defining a special method, with '=' in the signature 1354 | @name = name 1355 | end 1356 | 1357 | def is_adult 1358 | @age > 18 1359 | end 1360 | 1361 | 1362 | end 1363 | 1364 | ``` 1365 | 1366 | 1367 | 1368 | 1369 | :is_adult 1370 | 1371 | 1372 | 1373 | 1374 | ```ruby 1375 | person = Person.new("Tushar",25) 1376 | puts person.name 1377 | person.name = "Tuteja" 1378 | puts person.name 1379 | ``` 1380 | 1381 | Tushar 1382 | Tuteja 1383 | 1384 | 1385 | ### Private Methods in Ruby 1386 | 1387 | 1388 | ```ruby 1389 | class Person 1390 | def initialize (name,age) 1391 | @name = name # this is how we declare instance variable, by having @ at the start 1392 | @age = age # same with age 1393 | end 1394 | 1395 | def details # By default this method would be public 1396 | "Name: #{@name}" 1397 | end 1398 | 1399 | def name 1400 | @name 1401 | end 1402 | 1403 | def name= name # we are defining a special method, with '=' in the signature 1404 | @name = name 1405 | end 1406 | 1407 | def is_adult 1408 | @age > 18 1409 | end 1410 | 1411 | private # special keyword in ruby, after this everything we write would be private 1412 | def full_details # A private function 1413 | return @name, @age # Yes you can return more than one value, that would be returned as an array 1414 | end 1415 | 1416 | 1417 | end 1418 | ``` 1419 | 1420 | 1421 | 1422 | 1423 | :full_details 1424 | 1425 | 1426 | 1427 | 1428 | ```ruby 1429 | person = Person.new("Tushar",25) 1430 | person.full_details # Would throw an error 1431 | ``` 1432 | 1433 | 1434 | NoMethodError: private method `full_details' called for # 1435 | 1436 |
:1:in `
' 1437 | 1438 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1439 | 1440 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1441 | 1442 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1443 | 1444 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1445 | 1446 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1447 | 1448 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1449 | 1450 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1451 | 1452 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1453 | 1454 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1455 | 1456 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1457 | 1458 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1459 | 1460 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1461 | 1462 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1463 | 1464 | 1465 | ### Inheritance 1466 | 1467 | 1468 | ```ruby 1469 | class RichPerson < Person # RichPerson Inherits from Person 1470 | def print_details 1471 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1472 | end 1473 | 1474 | end 1475 | ``` 1476 | 1477 | 1478 | 1479 | 1480 | :print_details 1481 | 1482 | 1483 | 1484 | 1485 | ```ruby 1486 | rich_person = RichPerson.new("Tushar",25) 1487 | ``` 1488 | 1489 | 1490 | ArgumentError: wrong number of arguments (given 2, expected 4) 1491 | 1492 |
:5:in `initialize' 1493 | 1494 |
:in `new' 1495 | 1496 |
:in `
' 1497 | 1498 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1499 | 1500 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1501 | 1502 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1503 | 1504 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1505 | 1506 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1507 | 1508 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1509 | 1510 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1511 | 1512 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1513 | 1514 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1515 | 1516 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1517 | 1518 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1519 | 1520 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1521 | 1522 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1523 | 1524 | 1525 | 1526 | ```ruby 1527 | rich_person.print_details 1528 | ``` 1529 | 1530 | ["Tuteja", 25] 1531 | 1532 | 1533 | Child Class Objects can Call any private function of parent class. 1534 | 1535 | lets add some more functionality, lets suppose we want to add money in RichPerson class 1536 | 1537 | 1538 | ```ruby 1539 | class RichPerson < Person # RichPerson Inherits from Person 1540 | def initialize name, age, money 1541 | super name, age # special keyword in ruby to call constructor of parent class 1542 | @money = money 1543 | end 1544 | 1545 | def money 1546 | @money 1547 | end 1548 | 1549 | def print_details 1550 | puts full_details # RichPerson Objects are able to call private functions of their parent classes, 1551 | end 1552 | 1553 | end 1554 | ``` 1555 | 1556 | 1557 | 1558 | 1559 | :print_details 1560 | 1561 | 1562 | 1563 | 1564 | ```ruby 1565 | rich_person = RichPerson.new("Bill Gates",25,1) 1566 | ``` 1567 | 1568 | 1569 | 1570 | 1571 | # 1572 | 1573 | 1574 | 1575 | 1576 | ```ruby 1577 | rich_person.money 1578 | ``` 1579 | 1580 | 1581 | 1582 | 1583 | 1 1584 | 1585 | 1586 | 1587 | It is a hassle to write getter method, like we wrote above for money and setter method like we wrote for name. 1588 | For this ruby gives us short cuts. 1589 | 1590 | 1591 | ```ruby 1592 | class RichPerson 1593 | attr_accessor :name, :money # Gives us getter and setter methods for name and money 1594 | attr_reader :age # gives us only getter 1595 | attr_writer :phone # gives us only setter 1596 | 1597 | def initialize(name, money,age,phone) 1598 | @name = name 1599 | @money = money 1600 | @age = age 1601 | @phone = phone 1602 | end 1603 | 1604 | end 1605 | 1606 | rich_person = RichPerson.new("Tushar",1,25,9999999999) 1607 | ``` 1608 | 1609 | 1610 | 1611 | 1612 | # 1613 | 1614 | 1615 | 1616 | 1617 | ```ruby 1618 | rich_person.age # attr_reader, created a method by the name age that returns @age 1619 | ``` 1620 | 1621 | 1622 | 1623 | 1624 | 25 1625 | 1626 | 1627 | 1628 | 1629 | ```ruby 1630 | puts rich_person.name # attr_accesor, created a method by the name that returns @name 1631 | rich_person.name = "Tuteja"# attr_accesor, created a method by the name= that sets @name for us 1632 | puts rich_person.name 1633 | ``` 1634 | 1635 | Tushar 1636 | Tuteja 1637 | 1638 | 1639 | Similarly we can only change phone number but cannot see it 1640 | 1641 | 1642 | # Blocks 1643 | 1644 | 1645 | ```ruby 1646 | def special_function 1647 | yield 1648 | end 1649 | 1650 | 1651 | ``` 1652 | 1653 | 1654 | 1655 | 1656 | :special_function 1657 | 1658 | 1659 | 1660 | special_function is a type of funciton which requires a block of code to run. If we call it without supplying any block it would give an error as follows. How does the function knows that it requires a block ? The answer is the yield statement, as soon we put a yield statement, the function knows that this is where I'll give control to a block supplied to me. 1661 | 1662 | 1663 | ```ruby 1664 | special_function 1665 | ``` 1666 | 1667 | 1668 | LocalJumpError: no block given (yield) 1669 | 1670 |
:1:in `special_function' 1671 | 1672 |
:in `
' 1673 | 1674 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1675 | 1676 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:44:in `eval' 1677 | 1678 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval' 1679 | 1680 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request' 1681 | 1682 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch' 1683 | 1684 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run' 1685 | 1686 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel' 1687 | 1688 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run' 1689 | 1690 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/gems/iruby-0.2.9/bin/iruby:5:in `' 1691 | 1692 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `load' 1693 | 1694 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/iruby:22:in `
' 1695 | 1696 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval' 1697 | 1698 | /Users/tushartuteja/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `
' 1699 | 1700 | 1701 | There are two ways to give a block. One is using a do end block and another is using {} braces. 1702 | 1703 | 1704 | ```ruby 1705 | special_function do 1706 | puts " a block is given to this function " 1707 | end 1708 | 1709 | # In This special function we'll receive a block of code that would be executed. 1710 | 1711 | ``` 1712 | 1713 | a block is given to this function 1714 | 1715 | 1716 | Where did the execution of that block occur ? Let's see another example to figure that out. 1717 | 1718 | 1719 | 1720 | ```ruby 1721 | def special_function 1722 | puts " before yield statement " 1723 | yield 1724 | puts " after yield statement " 1725 | end 1726 | 1727 | 1728 | ``` 1729 | 1730 | 1731 | 1732 | 1733 | :special_function 1734 | 1735 | 1736 | 1737 | ww'll give a block of code using curly braces this time. 1738 | 1739 | 1740 | ```ruby 1741 | special_function {puts "A block of code is given"} 1742 | ``` 1743 | 1744 | before yield statement 1745 | A block of code is given 1746 | after yield statement 1747 | 1748 | 1749 | 1750 | ```ruby 1751 | def special_function 1752 | yield 1753 | yield 1754 | end 1755 | 1756 | ``` 1757 | 1758 | 1759 | 1760 | 1761 | :special_function 1762 | 1763 | 1764 | 1765 | 1766 | ```ruby 1767 | special_function {puts "A block of code is given"} 1768 | ``` 1769 | 1770 | A block of code is given 1771 | A block of code is given 1772 | 1773 | 1774 | You can have more than one yield statement, but you can only pass one block. 1775 | 1776 | What if our block of code requires a parameter to be executed ? 1777 | 1778 | 1779 | 1780 | ```ruby 1781 | def special_function 1782 | yield 5, [1,2,3] # these arguments would be passed to the block of code we'll supply 1783 | end 1784 | 1785 | ``` 1786 | 1787 | 1788 | 1789 | 1790 | :special_function 1791 | 1792 | 1793 | 1794 | 1795 | ```ruby 1796 | special_function { |first_number, second_array | puts first_number,second_array } 1797 | ``` 1798 | 1799 | 5 1800 | [1, 2, 3] 1801 | 1802 | 1803 | 1804 | ```ruby 1805 | def iterator n 1806 | i = 0 1807 | while i < n 1808 | yield i 1809 | i = i + 1 1810 | end 1811 | end 1812 | ``` 1813 | 1814 | 1815 | 1816 | 1817 | :iterator 1818 | 1819 | 1820 | 1821 | 1822 | ```ruby 1823 | iterator(10) do |x| 1824 | puts "this is the iteration number #{x}" 1825 | end 1826 | ``` 1827 | 1828 | this is the iteration number 0 1829 | this is the iteration number 1 1830 | this is the iteration number 2 1831 | this is the iteration number 3 1832 | this is the iteration number 4 1833 | this is the iteration number 5 1834 | this is the iteration number 6 1835 | this is the iteration number 7 1836 | this is the iteration number 8 1837 | this is the iteration number 9 1838 | 1839 | 1840 | 1841 | ```ruby 1842 | 1843 | ``` 1844 | -------------------------------------------------------------------------------- /notebook_demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodingNinjasCodes/RubyTutorial/fe1c7ec8cc6985fd104380e2e8f6ab99d6636ff7/notebook_demo.gif -------------------------------------------------------------------------------- /stylesheets/github-light.css: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 GitHub, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | .pl-c /* comment */ { 27 | color: #969896; 28 | } 29 | 30 | .pl-c1 /* constant, variable.other.constant, support, meta.property-name, support.constant, support.variable, meta.module-reference, markup.raw, meta.diff.header */, 31 | .pl-s .pl-v /* string variable */ { 32 | color: #0086b3; 33 | } 34 | 35 | .pl-e /* entity */, 36 | .pl-en /* entity.name */ { 37 | color: #795da3; 38 | } 39 | 40 | .pl-smi /* variable.parameter.function, storage.modifier.package, storage.modifier.import, storage.type.java, variable.other */, 41 | .pl-s .pl-s1 /* string source */ { 42 | color: #333; 43 | } 44 | 45 | .pl-ent /* entity.name.tag */ { 46 | color: #63a35c; 47 | } 48 | 49 | .pl-k /* keyword, storage, storage.type */ { 50 | color: #a71d5d; 51 | } 52 | 53 | .pl-s /* string */, 54 | .pl-pds /* punctuation.definition.string, string.regexp.character-class */, 55 | .pl-s .pl-pse .pl-s1 /* string punctuation.section.embedded source */, 56 | .pl-sr /* string.regexp */, 57 | .pl-sr .pl-cce /* string.regexp constant.character.escape */, 58 | .pl-sr .pl-sre /* string.regexp source.ruby.embedded */, 59 | .pl-sr .pl-sra /* string.regexp string.regexp.arbitrary-repitition */ { 60 | color: #183691; 61 | } 62 | 63 | .pl-v /* variable */ { 64 | color: #ed6a43; 65 | } 66 | 67 | .pl-id /* invalid.deprecated */ { 68 | color: #b52a1d; 69 | } 70 | 71 | .pl-ii /* invalid.illegal */ { 72 | color: #f8f8f8; 73 | background-color: #b52a1d; 74 | } 75 | 76 | .pl-sr .pl-cce /* string.regexp constant.character.escape */ { 77 | font-weight: bold; 78 | color: #63a35c; 79 | } 80 | 81 | .pl-ml /* markup.list */ { 82 | color: #693a17; 83 | } 84 | 85 | .pl-mh /* markup.heading */, 86 | .pl-mh .pl-en /* markup.heading entity.name */, 87 | .pl-ms /* meta.separator */ { 88 | font-weight: bold; 89 | color: #1d3e81; 90 | } 91 | 92 | .pl-mq /* markup.quote */ { 93 | color: #008080; 94 | } 95 | 96 | .pl-mi /* markup.italic */ { 97 | font-style: italic; 98 | color: #333; 99 | } 100 | 101 | .pl-mb /* markup.bold */ { 102 | font-weight: bold; 103 | color: #333; 104 | } 105 | 106 | .pl-md /* markup.deleted, meta.diff.header.from-file */ { 107 | color: #bd2c00; 108 | background-color: #ffecec; 109 | } 110 | 111 | .pl-mi1 /* markup.inserted, meta.diff.header.to-file */ { 112 | color: #55a532; 113 | background-color: #eaffea; 114 | } 115 | 116 | .pl-mdr /* meta.diff.range */ { 117 | font-weight: bold; 118 | color: #795da3; 119 | } 120 | 121 | .pl-mo /* meta.output */ { 122 | color: #1d3e81; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /stylesheets/normalize.css: -------------------------------------------------------------------------------- 1 | /*! normalize.css v3.0.2 | MIT License | git.io/normalize */ 2 | 3 | /** 4 | * 1. Set default font family to sans-serif. 5 | * 2. Prevent iOS text size adjust after orientation change, without disabling 6 | * user zoom. 7 | */ 8 | 9 | html { 10 | font-family: sans-serif; /* 1 */ 11 | -ms-text-size-adjust: 100%; /* 2 */ 12 | -webkit-text-size-adjust: 100%; /* 2 */ 13 | } 14 | 15 | /** 16 | * Remove default margin. 17 | */ 18 | 19 | body { 20 | margin: 0; 21 | } 22 | 23 | /* HTML5 display definitions 24 | ========================================================================== */ 25 | 26 | /** 27 | * Correct `block` display not defined for any HTML5 element in IE 8/9. 28 | * Correct `block` display not defined for `details` or `summary` in IE 10/11 29 | * and Firefox. 30 | * Correct `block` display not defined for `main` in IE 11. 31 | */ 32 | 33 | article, 34 | aside, 35 | details, 36 | figcaption, 37 | figure, 38 | footer, 39 | header, 40 | hgroup, 41 | main, 42 | menu, 43 | nav, 44 | section, 45 | summary { 46 | display: block; 47 | } 48 | 49 | /** 50 | * 1. Correct `inline-block` display not defined in IE 8/9. 51 | * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera. 52 | */ 53 | 54 | audio, 55 | canvas, 56 | progress, 57 | video { 58 | display: inline-block; /* 1 */ 59 | vertical-align: baseline; /* 2 */ 60 | } 61 | 62 | /** 63 | * Prevent modern browsers from displaying `audio` without controls. 64 | * Remove excess height in iOS 5 devices. 65 | */ 66 | 67 | audio:not([controls]) { 68 | display: none; 69 | height: 0; 70 | } 71 | 72 | /** 73 | * Address `[hidden]` styling not present in IE 8/9/10. 74 | * Hide the `template` element in IE 8/9/11, Safari, and Firefox < 22. 75 | */ 76 | 77 | [hidden], 78 | template { 79 | display: none; 80 | } 81 | 82 | /* Links 83 | ========================================================================== */ 84 | 85 | /** 86 | * Remove the gray background color from active links in IE 10. 87 | */ 88 | 89 | a { 90 | background-color: transparent; 91 | } 92 | 93 | /** 94 | * Improve readability when focused and also mouse hovered in all browsers. 95 | */ 96 | 97 | a:active, 98 | a:hover { 99 | outline: 0; 100 | } 101 | 102 | /* Text-level semantics 103 | ========================================================================== */ 104 | 105 | /** 106 | * Address styling not present in IE 8/9/10/11, Safari, and Chrome. 107 | */ 108 | 109 | abbr[title] { 110 | border-bottom: 1px dotted; 111 | } 112 | 113 | /** 114 | * Address style set to `bolder` in Firefox 4+, Safari, and Chrome. 115 | */ 116 | 117 | b, 118 | strong { 119 | font-weight: bold; 120 | } 121 | 122 | /** 123 | * Address styling not present in Safari and Chrome. 124 | */ 125 | 126 | dfn { 127 | font-style: italic; 128 | } 129 | 130 | /** 131 | * Address variable `h1` font-size and margin within `section` and `article` 132 | * contexts in Firefox 4+, Safari, and Chrome. 133 | */ 134 | 135 | h1 { 136 | font-size: 2em; 137 | margin: 0.67em 0; 138 | } 139 | 140 | /** 141 | * Address styling not present in IE 8/9. 142 | */ 143 | 144 | mark { 145 | background: #ff0; 146 | color: #000; 147 | } 148 | 149 | /** 150 | * Address inconsistent and variable font size in all browsers. 151 | */ 152 | 153 | small { 154 | font-size: 80%; 155 | } 156 | 157 | /** 158 | * Prevent `sub` and `sup` affecting `line-height` in all browsers. 159 | */ 160 | 161 | sub, 162 | sup { 163 | font-size: 75%; 164 | line-height: 0; 165 | position: relative; 166 | vertical-align: baseline; 167 | } 168 | 169 | sup { 170 | top: -0.5em; 171 | } 172 | 173 | sub { 174 | bottom: -0.25em; 175 | } 176 | 177 | /* Embedded content 178 | ========================================================================== */ 179 | 180 | /** 181 | * Remove border when inside `a` element in IE 8/9/10. 182 | */ 183 | 184 | img { 185 | border: 0; 186 | } 187 | 188 | /** 189 | * Correct overflow not hidden in IE 9/10/11. 190 | */ 191 | 192 | svg:not(:root) { 193 | overflow: hidden; 194 | } 195 | 196 | /* Grouping content 197 | ========================================================================== */ 198 | 199 | /** 200 | * Address margin not present in IE 8/9 and Safari. 201 | */ 202 | 203 | figure { 204 | margin: 1em 40px; 205 | } 206 | 207 | /** 208 | * Address differences between Firefox and other browsers. 209 | */ 210 | 211 | hr { 212 | box-sizing: content-box; 213 | height: 0; 214 | } 215 | 216 | /** 217 | * Contain overflow in all browsers. 218 | */ 219 | 220 | pre { 221 | overflow: auto; 222 | } 223 | 224 | /** 225 | * Address odd `em`-unit font size rendering in all browsers. 226 | */ 227 | 228 | code, 229 | kbd, 230 | pre, 231 | samp { 232 | font-family: monospace, monospace; 233 | font-size: 1em; 234 | } 235 | 236 | /* Forms 237 | ========================================================================== */ 238 | 239 | /** 240 | * Known limitation: by default, Chrome and Safari on OS X allow very limited 241 | * styling of `select`, unless a `border` property is set. 242 | */ 243 | 244 | /** 245 | * 1. Correct color not being inherited. 246 | * Known issue: affects color of disabled elements. 247 | * 2. Correct font properties not being inherited. 248 | * 3. Address margins set differently in Firefox 4+, Safari, and Chrome. 249 | */ 250 | 251 | button, 252 | input, 253 | optgroup, 254 | select, 255 | textarea { 256 | color: inherit; /* 1 */ 257 | font: inherit; /* 2 */ 258 | margin: 0; /* 3 */ 259 | } 260 | 261 | /** 262 | * Address `overflow` set to `hidden` in IE 8/9/10/11. 263 | */ 264 | 265 | button { 266 | overflow: visible; 267 | } 268 | 269 | /** 270 | * Address inconsistent `text-transform` inheritance for `button` and `select`. 271 | * All other form control elements do not inherit `text-transform` values. 272 | * Correct `button` style inheritance in Firefox, IE 8/9/10/11, and Opera. 273 | * Correct `select` style inheritance in Firefox. 274 | */ 275 | 276 | button, 277 | select { 278 | text-transform: none; 279 | } 280 | 281 | /** 282 | * 1. Avoid the WebKit bug in Android 4.0.* where (2) destroys native `audio` 283 | * and `video` controls. 284 | * 2. Correct inability to style clickable `input` types in iOS. 285 | * 3. Improve usability and consistency of cursor style between image-type 286 | * `input` and others. 287 | */ 288 | 289 | button, 290 | html input[type="button"], /* 1 */ 291 | input[type="reset"], 292 | input[type="submit"] { 293 | -webkit-appearance: button; /* 2 */ 294 | cursor: pointer; /* 3 */ 295 | } 296 | 297 | /** 298 | * Re-set default cursor for disabled elements. 299 | */ 300 | 301 | button[disabled], 302 | html input[disabled] { 303 | cursor: default; 304 | } 305 | 306 | /** 307 | * Remove inner padding and border in Firefox 4+. 308 | */ 309 | 310 | button::-moz-focus-inner, 311 | input::-moz-focus-inner { 312 | border: 0; 313 | padding: 0; 314 | } 315 | 316 | /** 317 | * Address Firefox 4+ setting `line-height` on `input` using `!important` in 318 | * the UA stylesheet. 319 | */ 320 | 321 | input { 322 | line-height: normal; 323 | } 324 | 325 | /** 326 | * It's recommended that you don't attempt to style these elements. 327 | * Firefox's implementation doesn't respect box-sizing, padding, or width. 328 | * 329 | * 1. Address box sizing set to `content-box` in IE 8/9/10. 330 | * 2. Remove excess padding in IE 8/9/10. 331 | */ 332 | 333 | input[type="checkbox"], 334 | input[type="radio"] { 335 | box-sizing: border-box; /* 1 */ 336 | padding: 0; /* 2 */ 337 | } 338 | 339 | /** 340 | * Fix the cursor style for Chrome's increment/decrement buttons. For certain 341 | * `font-size` values of the `input`, it causes the cursor style of the 342 | * decrement button to change from `default` to `text`. 343 | */ 344 | 345 | input[type="number"]::-webkit-inner-spin-button, 346 | input[type="number"]::-webkit-outer-spin-button { 347 | height: auto; 348 | } 349 | 350 | /** 351 | * 1. Address `appearance` set to `searchfield` in Safari and Chrome. 352 | * 2. Address `box-sizing` set to `border-box` in Safari and Chrome 353 | * (include `-moz` to future-proof). 354 | */ 355 | 356 | input[type="search"] { 357 | -webkit-appearance: textfield; /* 1 */ /* 2 */ 358 | box-sizing: content-box; 359 | } 360 | 361 | /** 362 | * Remove inner padding and search cancel button in Safari and Chrome on OS X. 363 | * Safari (but not Chrome) clips the cancel button when the search input has 364 | * padding (and `textfield` appearance). 365 | */ 366 | 367 | input[type="search"]::-webkit-search-cancel-button, 368 | input[type="search"]::-webkit-search-decoration { 369 | -webkit-appearance: none; 370 | } 371 | 372 | /** 373 | * Define consistent border, margin, and padding. 374 | */ 375 | 376 | fieldset { 377 | border: 1px solid #c0c0c0; 378 | margin: 0 2px; 379 | padding: 0.35em 0.625em 0.75em; 380 | } 381 | 382 | /** 383 | * 1. Correct `color` not being inherited in IE 8/9/10/11. 384 | * 2. Remove padding so people aren't caught out if they zero out fieldsets. 385 | */ 386 | 387 | legend { 388 | border: 0; /* 1 */ 389 | padding: 0; /* 2 */ 390 | } 391 | 392 | /** 393 | * Remove default vertical scrollbar in IE 8/9/10/11. 394 | */ 395 | 396 | textarea { 397 | overflow: auto; 398 | } 399 | 400 | /** 401 | * Don't inherit the `font-weight` (applied by a rule above). 402 | * NOTE: the default cannot safely be changed in Chrome and Safari on OS X. 403 | */ 404 | 405 | optgroup { 406 | font-weight: bold; 407 | } 408 | 409 | /* Tables 410 | ========================================================================== */ 411 | 412 | /** 413 | * Remove most spacing between table cells. 414 | */ 415 | 416 | table { 417 | border-collapse: collapse; 418 | border-spacing: 0; 419 | } 420 | 421 | td, 422 | th { 423 | padding: 0; 424 | } 425 | -------------------------------------------------------------------------------- /stylesheets/stylesheet.css: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | Slate Theme for GitHub Pages 3 | by Jason Costello, @jsncostello 4 | *******************************************************************************/ 5 | 6 | @import url(github-light.css); 7 | 8 | /******************************************************************************* 9 | MeyerWeb Reset 10 | *******************************************************************************/ 11 | 12 | html, body, div, span, applet, object, iframe, 13 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 14 | a, abbr, acronym, address, big, cite, code, 15 | del, dfn, em, img, ins, kbd, q, s, samp, 16 | small, strike, strong, sub, sup, tt, var, 17 | b, u, i, center, 18 | dl, dt, dd, ol, ul, li, 19 | fieldset, form, label, legend, 20 | table, caption, tbody, tfoot, thead, tr, th, td, 21 | article, aside, canvas, details, embed, 22 | figure, figcaption, footer, header, hgroup, 23 | menu, nav, output, ruby, section, summary, 24 | time, mark, audio, video { 25 | margin: 0; 26 | padding: 0; 27 | border: 0; 28 | font: inherit; 29 | vertical-align: baseline; 30 | } 31 | 32 | /* HTML5 display-role reset for older browsers */ 33 | article, aside, details, figcaption, figure, 34 | footer, header, hgroup, menu, nav, section { 35 | display: block; 36 | } 37 | 38 | ol, ul { 39 | list-style: none; 40 | } 41 | 42 | table { 43 | border-collapse: collapse; 44 | border-spacing: 0; 45 | } 46 | 47 | /******************************************************************************* 48 | Theme Styles 49 | *******************************************************************************/ 50 | 51 | body { 52 | box-sizing: border-box; 53 | color:#373737; 54 | background: #212121; 55 | font-size: 16px; 56 | font-family: 'Myriad Pro', Calibri, Helvetica, Arial, sans-serif; 57 | line-height: 1.5; 58 | -webkit-font-smoothing: antialiased; 59 | } 60 | 61 | h1, h2, h3, h4, h5, h6 { 62 | margin: 10px 0; 63 | font-weight: 700; 64 | color:#222222; 65 | font-family: 'Lucida Grande', 'Calibri', Helvetica, Arial, sans-serif; 66 | letter-spacing: -1px; 67 | } 68 | 69 | h1 { 70 | font-size: 36px; 71 | font-weight: 700; 72 | } 73 | 74 | h2 { 75 | padding-bottom: 10px; 76 | font-size: 32px; 77 | background: url('../images/bg_hr.png') repeat-x bottom; 78 | } 79 | 80 | h3 { 81 | font-size: 24px; 82 | } 83 | 84 | h4 { 85 | font-size: 21px; 86 | } 87 | 88 | h5 { 89 | font-size: 18px; 90 | } 91 | 92 | h6 { 93 | font-size: 16px; 94 | } 95 | 96 | p { 97 | margin: 10px 0 15px 0; 98 | } 99 | 100 | footer p { 101 | color: #f2f2f2; 102 | } 103 | 104 | a { 105 | text-decoration: none; 106 | color: #007edf; 107 | text-shadow: none; 108 | 109 | transition: color 0.5s ease; 110 | transition: text-shadow 0.5s ease; 111 | -webkit-transition: color 0.5s ease; 112 | -webkit-transition: text-shadow 0.5s ease; 113 | -moz-transition: color 0.5s ease; 114 | -moz-transition: text-shadow 0.5s ease; 115 | -o-transition: color 0.5s ease; 116 | -o-transition: text-shadow 0.5s ease; 117 | -ms-transition: color 0.5s ease; 118 | -ms-transition: text-shadow 0.5s ease; 119 | } 120 | 121 | a:hover, a:focus {text-decoration: underline;} 122 | 123 | footer a { 124 | color: #F2F2F2; 125 | text-decoration: underline; 126 | } 127 | 128 | em { 129 | font-style: italic; 130 | } 131 | 132 | strong { 133 | font-weight: bold; 134 | } 135 | 136 | img { 137 | position: relative; 138 | margin: 0 auto; 139 | max-width: 739px; 140 | padding: 5px; 141 | margin: 10px 0 10px 0; 142 | border: 1px solid #ebebeb; 143 | 144 | box-shadow: 0 0 5px #ebebeb; 145 | -webkit-box-shadow: 0 0 5px #ebebeb; 146 | -moz-box-shadow: 0 0 5px #ebebeb; 147 | -o-box-shadow: 0 0 5px #ebebeb; 148 | -ms-box-shadow: 0 0 5px #ebebeb; 149 | } 150 | 151 | p img { 152 | display: inline; 153 | margin: 0; 154 | padding: 0; 155 | vertical-align: middle; 156 | text-align: center; 157 | border: none; 158 | } 159 | 160 | pre, code { 161 | width: 100%; 162 | color: #222; 163 | background-color: #fff; 164 | 165 | font-family: Monaco, "Bitstream Vera Sans Mono", "Lucida Console", Terminal, monospace; 166 | font-size: 14px; 167 | 168 | border-radius: 2px; 169 | -moz-border-radius: 2px; 170 | -webkit-border-radius: 2px; 171 | } 172 | 173 | pre { 174 | width: 100%; 175 | padding: 10px; 176 | box-shadow: 0 0 10px rgba(0,0,0,.1); 177 | overflow: auto; 178 | } 179 | 180 | code { 181 | padding: 3px; 182 | margin: 0 3px; 183 | box-shadow: 0 0 10px rgba(0,0,0,.1); 184 | } 185 | 186 | pre code { 187 | display: block; 188 | box-shadow: none; 189 | background-color: aliceblue; 190 | } 191 | 192 | 193 | pre code::before{ 194 | content: ""; 195 | color: black; 196 | display: block; 197 | position: absolute; 198 | right: 0px; 199 | background-color:white; 200 | box-shadow: 1px 1px 1px white; 201 | box-sizing: border-box; 202 | 203 | } 204 | 205 | div.highlight-source-ruby pre::before{ 206 | content: ""; 207 | color: black; 208 | display: block; 209 | position: absolute; 210 | right: 0px; 211 | background-color:lightblue; 212 | box-sizing: border-box; 213 | box-shadow: 1px 1px 1px white; 214 | 215 | 216 | } 217 | 218 | 219 | blockquote { 220 | color: #666; 221 | margin-bottom: 20px; 222 | padding: 0 0 0 20px; 223 | border-left: 3px solid #bbb; 224 | } 225 | 226 | 227 | ul, ol, dl { 228 | margin-bottom: 15px 229 | } 230 | 231 | ul { 232 | list-style-position: inside; 233 | list-style: disc; 234 | padding-left: 20px; 235 | } 236 | 237 | ol { 238 | list-style-position: inside; 239 | list-style: decimal; 240 | padding-left: 20px; 241 | } 242 | 243 | dl dt { 244 | font-weight: bold; 245 | } 246 | 247 | dl dd { 248 | padding-left: 20px; 249 | font-style: italic; 250 | } 251 | 252 | dl p { 253 | padding-left: 20px; 254 | font-style: italic; 255 | } 256 | 257 | hr { 258 | height: 1px; 259 | margin-bottom: 5px; 260 | border: none; 261 | background: url('../images/bg_hr.png') repeat-x center; 262 | } 263 | 264 | table { 265 | border: 1px solid #373737; 266 | margin-bottom: 20px; 267 | text-align: left; 268 | } 269 | 270 | th { 271 | font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; 272 | padding: 10px; 273 | background: #373737; 274 | color: #fff; 275 | } 276 | 277 | td { 278 | padding: 10px; 279 | border: 1px solid #373737; 280 | } 281 | 282 | form { 283 | background: #f2f2f2; 284 | padding: 20px; 285 | } 286 | 287 | /******************************************************************************* 288 | Full-Width Styles 289 | *******************************************************************************/ 290 | 291 | .outer { 292 | width: 100%; 293 | } 294 | 295 | .inner { 296 | position: relative; 297 | max-width: 640px; 298 | padding: 20px 10px; 299 | margin: 0 auto; 300 | } 301 | 302 | #forkme_banner { 303 | display: block; 304 | position: absolute; 305 | top:0; 306 | right: 10px; 307 | z-index: 10; 308 | padding: 10px 50px 10px 10px; 309 | color: #fff; 310 | background: url('../images/blacktocat.png') #0090ff no-repeat 95% 50%; 311 | font-weight: 700; 312 | box-shadow: 0 0 10px rgba(0,0,0,.5); 313 | border-bottom-left-radius: 2px; 314 | border-bottom-right-radius: 2px; 315 | } 316 | 317 | #header_wrap { 318 | background: #212121; 319 | background: -moz-linear-gradient(top, #373737, #212121); 320 | background: -webkit-linear-gradient(top, #373737, #212121); 321 | background: -ms-linear-gradient(top, #373737, #212121); 322 | background: -o-linear-gradient(top, #373737, #212121); 323 | background: linear-gradient(top, #373737, #212121); 324 | } 325 | 326 | #header_wrap .inner { 327 | padding: 50px 10px 30px 10px; 328 | } 329 | 330 | #project_title { 331 | margin: 0; 332 | color: #fff; 333 | font-size: 42px; 334 | font-weight: 700; 335 | text-shadow: #111 0px 0px 10px; 336 | } 337 | 338 | #project_tagline { 339 | color: #fff; 340 | font-size: 24px; 341 | font-weight: 300; 342 | background: none; 343 | text-shadow: #111 0px 0px 10px; 344 | } 345 | 346 | #downloads { 347 | position: absolute; 348 | width: 210px; 349 | z-index: 10; 350 | bottom: -40px; 351 | right: 0; 352 | height: 70px; 353 | background: url('../images/icon_download.png') no-repeat 0% 90%; 354 | } 355 | 356 | .zip_download_link { 357 | display: block; 358 | float: right; 359 | width: 90px; 360 | height:70px; 361 | text-indent: -5000px; 362 | overflow: hidden; 363 | background: url(../images/sprite_download.png) no-repeat bottom left; 364 | } 365 | 366 | .tar_download_link { 367 | display: block; 368 | float: right; 369 | width: 90px; 370 | height:70px; 371 | text-indent: -5000px; 372 | overflow: hidden; 373 | background: url(../images/sprite_download.png) no-repeat bottom right; 374 | margin-left: 10px; 375 | } 376 | 377 | .zip_download_link:hover { 378 | background: url(../images/sprite_download.png) no-repeat top left; 379 | } 380 | 381 | .tar_download_link:hover { 382 | background: url(../images/sprite_download.png) no-repeat top right; 383 | } 384 | 385 | #main_content_wrap { 386 | background: #f2f2f2; 387 | border-top: 1px solid #111; 388 | border-bottom: 1px solid #111; 389 | } 390 | 391 | #main_content { 392 | padding-top: 40px; 393 | } 394 | 395 | #footer_wrap { 396 | background: #212121; 397 | } 398 | 399 | 400 | 401 | /******************************************************************************* 402 | Small Device Styles 403 | *******************************************************************************/ 404 | 405 | @media screen and (max-width: 480px) { 406 | body { 407 | font-size:14px; 408 | } 409 | 410 | #downloads { 411 | display: none; 412 | } 413 | 414 | .inner { 415 | min-width: 320px; 416 | max-width: 480px; 417 | } 418 | 419 | #project_title { 420 | font-size: 32px; 421 | } 422 | 423 | h1 { 424 | font-size: 28px; 425 | } 426 | 427 | h2 { 428 | font-size: 24px; 429 | } 430 | 431 | h3 { 432 | font-size: 21px; 433 | } 434 | 435 | h4 { 436 | font-size: 18px; 437 | } 438 | 439 | h5 { 440 | font-size: 14px; 441 | } 442 | 443 | h6 { 444 | font-size: 12px; 445 | } 446 | 447 | code, pre { 448 | min-width: 320px; 449 | max-width: 480px; 450 | font-size: 11px; 451 | } 452 | 453 | } 454 | -------------------------------------------------------------------------------- /tutorial.md: -------------------------------------------------------------------------------- 1 | 2 | ## Basics 3 | 4 | ### Data Types 5 | 6 | 7 | ```ruby 8 | my_num = 15 9 | my_string = "Tushar" 10 | my_bool = true 11 | 12 | puts my_num, my_string, my_bool 13 | ``` 14 | 15 | 15 16 | Tushar 17 | true 18 | 19 | 20 | Ruby is a dynamically typed Language, as we see in the above code, we assigned a number, a string value and a boolean to three different variables 21 | 22 | 'puts' is a function that takes a list of arguements and prints to console on a new line, You may use print statement as well, the only difference is, it prints in continuation. 23 | 24 | 25 | ```ruby 26 | print my_num, my_string, my_bool 27 | ``` 28 | 29 | 15Tushartrue 30 | 31 | my_num, my_string and my_bool are variables which are holding different objects. For naming variables in ruby we use snakecase as a convention. 32 | 33 | Everything is a an Object in Ruby, 34 | 35 | Everything! 36 | 37 | To find out class of a ruby object we can call a 'class' method on any object. 38 | 39 | 40 | ```ruby 41 | puts my_num.class() , my_string.class() , my_bool.class() 42 | ``` 43 | 44 | Fixnum 45 | String 46 | TrueClass 47 | 48 | 49 | 50 | ```ruby 51 | 52 | ``` 53 | --------------------------------------------------------------------------------