 Semoga kamu berdua melakukannya hari ini, walaupun jika kamu mempunyai sesuatu yang berkeselamatan. Tapi saya harap kamu bergantung tentang apa yang kamu akan belajar untuk kerja ini hari ini. Jadi... Kamu bergantung? Ya! Ya! Baiklah. Jadi untuk orang-orang yang masih sedikit... Saya akan menghubungi beberapa minit lagi, cuba mendapatkan kamu bergantung dan berikan kepada kamu apa yang kamu akan belajar hari ini, okey? Jadi dengan itu, mari saya memperkenalkan karakter-karakter yang akan berminik hari ini. Pertama, ia saya! Okey, seperti penggantung Pokémon yang berjalan-jalan di sebuah kejadian. Okey, perkara-perkara saya yang bergantung. Okey, jadi nama saya Jensie. Saya juga bergantung menjadi salah satu pembantu pelajar yang berjalan-jalan untuk kakak-kak pembantu pelajar yang berjalan-jalan. Dan saya sekarang seorang pelajar yang berjalan-jalan dari universiti Singapura dan Desain, SWTD. Jadi sebenarnya, banyak pembantu pelajar yang berjalan-jalan, mereka mempunyai lebih banyak pengalaman industri yang saya melakukannya. Tapi, kamu mungkin mempunyai, okey, kenapa saya mempunyai satu pembantu pelajar ini? Untuk mana, jawapan saya adalah... Saya mempunyai untuk ini dalam penggantung yufu saya. Okey, jadi saya di sini hari ini. Baiklah. Tapi, seperti yang saya katakan, ini adalah kali pertama saya melakukannya dalam syarikat yang panjang. Jadi jika anda ada pertanyaan di atas jalan-jalan, tolong bergerak atau tangan, bergerak atau keadaan. Dalam pembantu pelajar yang berjalan-jalan, jika anda ada feedback, tolong beritahu saya, okey? Baiklah, jadi... Oh, tidak. Baiklah, masalah AV. Okey, jadi daripada pembantu pelajar yang berjalan-jalan, anda juga dapat melihat... Anda dapat melihat banyak rubi, betul? Anda akan melihat... Anda akan melihat banyak dengan dia dan dia sangat gembira melihat anda. Ada orang lain? Adakah anda menerima pelajar di sini menggunakan diri anda? Ya. Baiklah, jadi ini adalah rambut anda. Anda akan mengenai cara untuk menggunakan rambut anda. Dan mungkin anda fikir anda akan menangis untuk rambut rambut. Tapi sebenarnya anda akan mengenai rambut kedua yang dikenali, yang dipanggil BASH. Okey, BORN AGAIN SHELL. Jadi ini adalah Phoenix di sini juga menggunakan diri anda. Anda perlu mengenai BASH untuk mengenai... untuk mengenai rambut anda. Okey, jadi itu sebab anda mengenai rambut lain yang dipanggil BASH. Jadi, daripada dua... dua karakter yang penting, anda juga akan melihat kereta yang lain daripada karakter yang penting, kan? Dan anda akan mempunyai kereta, kereta, panggilan, dan anda akan mempunyai karakter yang penting. Okey, jadi ini adalah kereta dengan sekitar tiga perkara. Pertama, anda akan mengenai cara untuk menggunakan rambut atau juga mengenai kawasan. Pertama, anda akan mengenai basic Ruby. Dan akhirnya, pada perkara yang terakhir, anda akan melakukan projek yang mudah, anda akan dapat membuat sebuah servis Http yang menghubungkan ke sebuah database... pada akhir kereta ini. Dan jika anda tak tahu apa itu bermaksud anda, jangan risau, pada akhir kereta ini anda akan mempunyai idea yang baik. Okey? Jadi, sebelum saya sebenarnya menggunakan diri anda, saya ingin beri anda... saya ingin bercakap sedikit tentang prinsipan pendidikan saya di belakang kereta ini. Jadi, anda dapat faham kenapa anda belajar perkara yang anda belajar. Jadi, perkara pertama yang saya ada untuk diri saya adalah, sebab ini... ini sebuah pelethora yang sangat... berkualiti tinggi yang tinggi untuk rambut Ruby online. Kenapa anda mahu datang hari ini? Kenapa anda mahu keluar dari sini dan menikmati 5 jam mendengar saya? Jadi, bagaimanapun, perkara ini adalah apa yang saya menolakkan? Jika tidak, anda boleh kembali ke rumah anda dan melakukan perkara itu. Saya akan berkata, 5 jam kerja tidak dapat mengubah masa dan peraturan anda sendiri. Tetapi, saya mahu beri anda lebih daripada apa yang menggunakan penerbangan Ruby untuk beri anda. Pertanyaan kedua yang saya ada untuk saya adalah, apa yang mempunyai keadaan yang saya mempunyai... untuk memperkenalkan aplikasi sebenar? Kerana saya memperkenalkan apabila saya memperkenalkan aplikasi yang saya memperkenalkan... dengan aplikasi yang saya memperkenalkan... perkenalkan tidak adalah perkara yang paling susah. Kerana perkara yang paling susah adalah memperkenalkan... perkenalkan sebuah ekosistem yang besar... yang anda menunggu sebagai penerbangan aplikasi. Dan betul-betul, anda tidak memperkenalkannya banyak di sekolah. Jadi, saya mahu beri anda... beri anda penerbangan untuk beberapa perkara yang anda mempunyai... untuk memperkenalkan diri anda dan tidak hanya untuk memperkenalkan. Jadi, saya mahu menggunakan diagram ini untuk menunjukkan apa yang saya maksudkan. Ini sebenarnya daripada... sebuah pembaharan yang penting daripada penerbangan Ruby Red Dot... pada tahun yang lalu, oleh penerbangan ini disebabkan Godfrey Chan. Dan anda mengatakan bahawa... ini adalah semua perkara... jika jalan jalan di sini... ini adalah semua perkara yang anda perlu tahu... untuk mempunyai... untuk mempunyai di setiap jalan... untuk mempunyai perkara basikal... untuk mempunyai perkara jalan jalan... atau sebuah pembaharan aplikasi. Jadi, saya beritahu, ia tidak mudah. Banyak perkara. Meskipun CsGrad... kita belajar semua perkara ini. Tetapi, semua perkara ini... yang kita tidak tahu... yang kita perlu belajar atau belajar. Jadi, saya hanya mahu membantu anda... menjelaskan jalan ini. Bukan hanya belajar tentang Programming 101... tetapi juga... memberikan perhatian tentang semua perkara ini... HGTP, REST... Ya, anda akan melihat semua ini nanti. Dan... bagi saya, Godfrey Chan, dia juga bercakap tentang perkara ini... yang disebabkan Horminutik Cikgu. Saya tidak tahu jika anda beritahu perkara ini... Horminutik Cikgu. Sangat cim. Tetapi, sebenarnya... ini adalah metodologi... untuk interpretasi teksual. Jadi, mereka berkata... untuk memahami perkaraan teksual... anda harus memahami perkara-perkara... untuk memahami perkara-perkara teksual... dan dia berkata... dia menggunakan perkara ini... sebagai analogis... untuk mempelajari... pembinaan software. Dan saya rasa... ia adalah analogis yang sangat hebat... kerana... sebenarnya... untuk contoh... saya akan kata... anda mempelajari Rubeon Reels. Anda mempelajari... untuk membuat... bagaimana... perkara ini telah dilakukan... di Rubeon Reels. Dan kemudian... anda melihat... kemudian anda memulakan... lebih banyak perkaraan teksual... mengapa perkara ini telah dilakukan... dan sebab anda sudah mempunyai... bagaimana perkara ini telah dilakukan... di Rubeon Reels. Anda dapat memahami... kenapa perkara ini... kemudian anda mempunyai... perkara ini telah dilakukan... dan sebab anda mempunyai... perkara ini telah dilakukan... di Rubeon Reels. Anda juga mempunyai... kenapa Rubeon Reels... melakukan perkara ini telah dilakukan. Jadi ia adalah proses yang berjalan. Saya melihat... sebab ini... terus berlaku... berlaku... berlaku... berlaku lagi. Jadi ia akan berlaku. Jadi... apa maksud anda... dalam cara membuat ini? Saya harap... untuk mengawak proses ini. Jadi... anda akan mempunyai... proses yang berlaku... anda akan mempunyai... proses yang berlaku... tapi saya tidak mahu anda berhubung... menggunakan, bukan? Jadi saya akan memperkenalkan... dengan sebuah lepas... lebih banyak... konsep yang lebih besar... mengapa anda melakukan... mengapa anda melakukan. Dan... jadi perniagaannya... adalah... pada akhir hari ini... apa ia? Jadi mari saya beritahu... sebuah cerita yang terserah... ok? Jadi saya mengambil... sebuah pelajaran di sebuah cerita modern... dan professeor itu beritahu kami... sebuah pelajaran... yang berlaku di sebuah cerita... di sebuah cerita. Jadi sebuah cerita... adalah... sebuah sebuah litarati... litarati pada sebuah pelajaran... atau sebuah pelajaran... dikawal. Mereka akan mengembirkan... kebantuan... untuk... berdekatan... dari kebantuan... dan mendapatkan... kelebihan dengan perhatian mereka. Jadi seorang... seorang ini akan mengembirkan kebantuan. Kemudian di atas... mereka akan mengembirkan... kebantuan. Kemudian... mereka akan mengerikan... kebantuan. Okey? Jadi anda dapat melihat... kebantuan ini? Saya mahu... dapat meletakkan kerja tersebar... untuk anda... Jika anda tinggalkan ke atas, saya akan membuatnya. Jadi, ia adalah masalah kerja ini hari ini. Bukan hanya mengenali program Ruby. Baiklah. Sekarang dengan itu, saya tahu anda sangat gembira untuk mendapatkan koding, tetapi saya perlu bercakap sedikit lebih tentang bagaimana untuk mengenali program. Bukan hanya mengenali program, tetapi keadaan yang anda ada, segala-galanya. Jadi, pertama, anda perlu mempunyai keadaan untuk membuat keadaan sendiri. Sebenarnya, dalam keadaan saya, saya sebenarnya membuat keadaan untuk anda. Jadi, jika anda melihat keadaan, keadaan, ini adalah beberapa alasan yang anda boleh melihat apabila anda mempunyai masa. Saya telah memberi anda beberapa keadaan. Anda boleh memilih untuk mengenali atau tidak. Tetapi, hanya melihat. Jadi, ada satu perkara. Yang terakhir adalah, bersiap untuk dipercayai. Programnya susah. Tetapi, penting, jangan belajar untuk dipercayai. Jangan membuat keadaan sendiri yang mempunyai keadaan yang betul untuk dipercayai. Saya rasa saya benar-benar suka cerita dari blog ini. Jadi, belajar bagaimana untuk mengenali keadaan dengan keadaan sepenuhnya. Jadi, jangan terus berdiri di depan komputer sepanjang hari dan tidak dapat keadaan. Jadi, keadaan keadaan tidak mempercayai keadaan yang dipercayai. Selanjutnya, mempercayai dengan keadaan internet anda. Jadi, saya tidak tahu jika anda telah mendengar nama Google Full, seperti Google & Full, seperti Kung Fu, tetapi dengan Google, anda harus mempercayai dengan keadaan keadaan. Jadi, saya tidak akan mempercayai keadaan Google. Jadi, apa-apa pun yang anda cari, apa-apa pun yang anda cari, mempercayai Kung Fu di keadaan keadaan itu. Tolonglah. Akhirnya, sebab saya di sini hari ini, cara yang sangat efektif untuk mempelajari diri anda. Jadi, anda tidak perlu menjadi ekspert untuk mempelajari. Sebenarnya, saya hanya seorang pelajar. Kemudian, orang yang terbaik untuk mempelajari orang lain adalah pelajar sendiri. Kerana anda mempunyai empat untuk pelajar lain. Kerana beberapa orang, jika mereka terlalu baik atau terlalu lama, untuk hidup mereka, mereka tidak dapat mengenali kenapa anda mempunyai keadaan keadaan. Sekarang, sekarang saya sedang mempelajari. Jadi, sebab saya lelaki ketiga, saya beritahu saya, saya mempelajari, saya tidak sangat percaya. Kemudian, dia seperti, saya sangat natural, sangat mudah. Sebaik-baik saja, anda dapat mempelajari. Jadi, kadang-kadang anda bercakap dengan ekspert, mereka tidak dapat membantu anda. Sebaik-baik saja, keadaan anda harus mempelajari apabila anda belajar mempelajari. Sekarang, saya beritahu lebih banyak perkara teknikal. Ini sangat penting. Kerana, anda sekarang menggunakan ide online, tetapi kemudian, apabila anda ingin melakukan perkara ini sendiri, anda perlu belajar bagaimana mempelajari keadaan keadaan keadaan keadaan sendiri. Jadi, saya akan berikan kepada anda bagaimana perkara ini menggunakan ide online. Jadi, anda dapat mempelajari. Jadi, seperti yang saya beritahu, anda menggunakan Cloud9. Cloud9 adalah ide web-based. Jadi, itu bermaksud, sebenarnya, ada internet yang menggunakan masin yang anda masih menghubungkan ke dalam browser anda. Siapa yang tahu sistem operasi anda menggunakan pada Cloud9? Ubuntu. Siapa yang pernah dengar Ubuntu atau menggunakan sebelumnya? Okey, Ubuntu adalah distribusi Linux. Jadi, itu sebenarnya, seorang perempuan yang mengenai Linux. Untuk beri anda sebuah latihan historikal di sekitar 1970s, AT&T dan Bell Labs, mereka membeli sistem operasi ini disebabkan Unix. Kemudian, Unix menjadi sangat popular di sebuah akademi dan ia dibuat. Tapi ia adalah sistem propriatari. Tapi kemudian, beberapa orang, banyak orang membuat sistem operasi yang menggunakan Unix pada masa itu. Satu orang, sebenarnya, adalah company Steve Jobs dan akhirnya menjadi Mac OS yang anda tahu hari ini. Mac OS adalah sistem operasi yang menggunakan Unix. Sekarang, orang lain, ada seorang perempuan yang menggunakan Linus Torval, seorang perempuan yang menggunakan apa yang kita nampak hari ini sebagai kerana Linus Torval. Ini adalah sebuah basis untuk sistem operasi Linus Torval yang anda tahu hari ini. Jadi, salah satu daripada mereka, yang anda tahu dengan baik-baik saja, pada telefon anda, jika anda menggunakan Android, itu sistem operasi Linus Torval. Ubuntu adalah satu lagi. Kenapa orang menggunakan, kenapa Cloud9 menggunakan Linus Torval dan kenapa menggunakan Linus Torval sekarang? Sebab, untuk Ruby, untuk perempuan-perempuan, untuk perempuan-perempuan, banyak perempuan-perempuan yang menggunakan Linus Torval. Jadi, bahagian ini dan sebuah ekosistem, mereka menggunakan yang terbaik pada environment Linus Torval atau environment Unix. Kenapa saya ingin beritahu ini? Sebab, sebab sistem operasi, sebelum itu, sistem operasi adalah bahagian software yang berdiri melepaskan komputer anda dan aplikasi penggunaan. Jadi, perkara yang menggunakan sistem terbaik anda, sistem terbaik anda, sistem terbaik anda, semua perkara ini menggunakan sistem operasi. Kenapa saya ingin beritahu semua ini? Saya ingin beritahu tentang orang ini? Windows. Berapa banyak anda menggunakan Windows? Okey, lebih dari sebaik-baiknya orang anda, sebenarnya, banyak orang menggunakan Windows. Dan saya ingin beritahu yang tersebut, komuniti Ruby adalah sedikit tidak bergantung kepada penggunaan Windows, sebab ini seorang perempuan David Hainmeyer Hansen. Ada sesiapa yang tahu yang dia? Jadi, orang ini juga seorang komuniti DHH, sebenarnya, perempuan-perempuan, dia telah menjadi anti Microsoft atau Tuhan tahu berapa lama. Jadi, saya rasa dia juga menginginkan komuniti Ruby & Rails. Jadi, support untuk Windows dan Ruby & Rails bukan yang terbaik. Jadi, jika anda cuba membuat perubahan di tempat yang berlaku dengan Windows, anda dapat melihat banyak masalah sebab, pertama, orang-orang di ekosistem, mereka semua, kebanyakan perempuan menggunakan Macs. Dan, jika perempuan tidak bergantung dengan Windows, semua ini adalah perempuan, mereka bergantung, mereka bergantung, akan membuat semua masalah ini. Tapi jika mereka semua menggunakan Macs dan anda memberi mereka masalah Windows, jelasnya, ia akan berlaku di bawah perempuan mereka. Jadi, sebab itu, perubahan di Ruby & Rails tidak sangat bergantung dengan Windows. Tetapi, untuk orang-orang anda menggunakan Windows, saya akan berkata, cara yang paling menyerah, adalah sebenarnya, untuk mencari sistem Linux pada masyarakat anda. Tapi, jika anda bergantung, anda boleh bercakap nanti. Saya tidak akan bercakap banyak tentang itu. Okey. Kita boleh mulakan dengan koding kami. Jadi, saya mahu anda berjalan ke pejabat anda. Cuma, Cuma, saya seharusnya beritahu orang-orang yang bergantung untuk bergantung. Sebenarnya, anda tidak mempunyai perkara di pejabat anda, sebab yang pertama, anda tidak mempunyai pejabat daripada perempuan dan perempuan. Jadi, salah satu perkara, anda perlu mempunyai pejabat yang terhadap yang terhadap pejabat yang terhadap ke pejabat anda. Dan, dari pejabat yang terhadap anda sendiri ke pejabat anda sendiri. Jadi, apabila anda membuat pejabat kerja, kemudian pejabat akan di sana. Yang kedua, saya merupakan apabila anda mempunyai pemadangan gerai di perempuan anda, anda perlu memperbaiki perempuan dengan perempuan yang terhadap anda. Bukan perempuan anda, bukan perempuan kerja anda, bukan perempuan yang terhadap anda, tapi perempuan yang terhadap anda. Okey. Jadi, apabila anda memperbaiki perempuan yang terhadap anda anda tahu apabila anda memperbaiki perempuan yang terhadap anda. Bagaimana kita membuat kejabat kerja-kerja untuk kedasarnya melepaskan perempuan yang terhadap anda? Mereka memperbaiki perempuan yang terhadap anda. Okey okey, semua anda ada internet tapi, kita lihat ke arah pejabat anda dan melihat, anda boleh lihat Di sini ada CLI 101.MD Yang kursus saya dah selesai sekarang Yang saya masih cuba kongsi Ya, cuba membuka muka CLI 101 Okey, berada apa? Kita akan membuat ini tanpa kumpulan Dan kita akan mencari Mereka akan beritahu saya Mereka akan beritahu saya Saya juga ada WIFI issues sekarang, sebabnya Tapi saya akan mencari file CLI-11 Saya tidak dapat mencari file ini Saya akan mencari file CLI-11 dulu Dan saya akan mencari dengan apa yang berlaku dengan perangkat komand Yang sebenarnya... Saya minta maaf, anda mesti melihat perkara yang tak baik Tapi saya akan cuba menghubungi Dan kemudian anda akan melihat So, your command line is my distinct in the command line So, the command line is people call the terminal also called shell you hear this term so of using interchangeably So, the shell is just a very generic term for user interface So, you hear people talking about graphical shell And this is actually what most of you interact with the computer through your graphical user interface You also call it graphical shell So, what you guys are going to learn today is how to use your command line interface which is a command line shell So, I'm going to try to get you guys to familiarize yourself with some basic command line commands because you guys will need to know how to work with some you must have some basic level of familiarity to be a software developer So, let's do some warm up Okay, follow through on your notes Okay, I'm sorry I cannot I don't know why my wife is killing, is dying on me now but you can actually open your notes in your cloud line once you open your CLI 101 live preview file and something like this should not show up at the site so you can refer to it at the site as you go along So, I'm just going to demo everything on my own machine but just remember when I type stuff into here type it in your terminal Alright, you know the terminal is a window at the bottom that's your command line Increase once, yeah I'll try my first one Which one? Kit What? It's not being detected Kit Right, let me refresh you Terrible, I don't know why it's so scary I don't know that I need restart my computer Yes, thank you, Kit Okay, thank you, Kit, for being a lifesaver I'm back online Okay, so go to CLI 101 Preview Live preview file, you can refer to it at the site so I'll be more or less following the structure but sometimes the notes go into a bit more detail and I may talk about some stuff that is not in the notes also Okay, so we're going to play around with our terminal so the most basic program we always write traditionally, programmers we write Hello World So, how do we do Hello World in our terminal? So, go to your terminal now you see I'm selecting my terminal Can you guys see? Or too small? Okay, Adona Big enough? No, yes, no Too big Okay So, how do we do Hello World in our terminal? Echo Double quote Hello World and you should see your terminal print out Hello World to you So, it's basically echoing back Hello World to you So, this is not very helpful but just to get you a feel of you know, how to work with your terminal The next thing you can do is something called, a command called ls Type it in, press enter So, ls stands for list It will list all the files in your current directory So, you guys know when you use your GUI your graphical interface you can open a folder you can see a list of files ls does the same thing So, you see my current directory here indicated by this line now I'm at this funny tail day thing dash workspace this is my current directory and it's listing all the files in my current directory So, you can see it actually corresponds to what you see on your side pane now you can list files you can echo Hello World how about navigating around it's like your GUI you can press back or click and open a new folder that kind of thing So, we use a command called cd stands for change directory So, if you type cd double dot press enter this means change directory to my parent directory the double dot means the parent directory So, if you see just now I was in workspace now I cd double dot I'm now in only this tail day thing this is actually my home directory How do I know I change my directory other than by looking at this part here I can also list again to see what I have So, now you see I'm a different directory I have 2 different folders lit workspace By the way, directory and folder mean the same thing Okay How do I know So, you can actually there's also actually a command to tell you what is the the path of my current directory So, it's called pwd stands for present working directory So, your current directory is also called your working directory So, if you type pwd you see that my full my full path of my current directory is slash home slash ubuntu Even though it's actually this tail day thing over here in your command line this tail day actually expands to your home directory So, the full name of this directory is slash home slash ubuntu but this tail day thing is like a shorthand in your terminal So, we can go back to workspace So, cd workspace If I pwd you see I'm in slash home slash ubuntu slash workspace You guys following so far So, we're just changing directories and you notice your file system your directory is your folders It's actually just like a tree You can go up their parents and their branches So, we can actually visualize that in our terminal using none other than the tree command So, if we go back I want to go back to slash home slash ubuntu So, if you're in workspace cd back out to slash home slash ubuntu and we're not in this directory now run the command tree press enter Okay Ideally full screen this so you can see everything but it's actually a quite a long output you can't really see everything directly but this is a tree representation of everything that's in this directory So, when you did list when you did LS just now you just listed the first level You just listed the directories under the current directory Tree will expand everything recursively because a tree structure because if you have directories and directories in directories So, tree will expand everything and show you everything that's in your current directory So, there's a lot of stuff as you can tell So, remember just now I said the tail day thing actually expands to slash home slash ubuntu we can actually test it out if we do echo tail day it actually echoes back slash home slash ubuntu So, that just shows you that in your shell this tail day expands we call it shell expansion this tail day expands into this string actually Okay, so we can change directory we can list the photos in our directory next thing is how do I create new directories and new files So, there's this command called mkdir what do you think it stands for make directory right okay So, you can make directory name it whatever you want so, I'm going to name it newdir new directory okay, sorry let me let me go back to my workspace first okay, in my workspace try to follow along in my workspace let me make makedir new directory or you can name it whatever you want whatever you put here this will be the name of your new directory okay So, how do I know it's created I can list again ls and you see I have this new directory over here but now it's currently empty because I just created a new directory it's empty by default so how do I check there's empty I can list new directory so just now we just did list like this just list on its own but now I'm going to list new directory so this extra thing here is something we call an argument so this ls command it can take in arguments and the argument will determine what you give it what output actually gives you so in this case how does list interpret this thing that you give it interpret it as the directory you want it to list right so if you tell it ls new directory it will list the new directory la if you don't give it any directory to list it will just by default list the current directory okay so if you list new directory there's no output because new directory is currently empty okay so if you talk about listing directory what about using ls on the normal file because the directory they contain other directory and false they don't contain other files and other directories right so what does ls do for our normal file let's try ls readme.md sorry turn the previous one okay go slower okay okay do you guys think I should go slower okay I'll go slower okay you want to see this completed so like I said we've been using ls on directory so far but the question is can we do ls on a file also right because we know directories they contain they contain stuff but files they files don't contain other files they don't contain other directories so what happens when we ls a file so we can just try so you try to ls readme.md this is just a normal file and what you get is just a name okay you take it or not very helpful but something more useful okay but just so you know you can ls both directories and files okay so now we've learnt how to create a new directory with make directory how about creating new files so there's this command called touch t-o-c-h t-o-u-c-h sorry so I want you to touch new directory slash one dot text so this means create a file at this path so what is this path this path is new directory slash one dot text that means you create a file called one dot text inside new directory right and we can create multiple files at the same time so we don't have to keep typing touch new directory touch touch so we can actually do it all in one line so we can touch new directory slash two dot text and this will basically create two files simultaneously in new directory okay and how do we tell again how do we tell whether this are created a new directory anyone LS LS what yep LS new directory okay there you see we've created these two new files okay and how do we output the contents of a file so you remember I said you're going to meeting this character right so there's a command called cat it doesn't actually stand for you know the animal cat stands for a concatenate you can believe that it's animal cat but anyway you can use cat to list the contents of the file right so we can cat new directory one dot text but then again it's an empty file so we don't get anything so what I want you guys to do is open up new directory one dot text in your text editor right this pane over here add some stuff I love Ruby so awesome okay then save it and re-run the cat command in your terminal again so cat new directory slash one dot text and then you should see the contents of your file output in your terminal okay okay at this point any questions we've done some very basic navigating around our file navigating in files and directories listing files listing directories listing the contents of a file creating new files creating new directories okay so maybe I'll just give you guys some time to play around a bit try to CD to different directories or touch new files this and that and cat the files and see whether you can familiarize yourself so about maybe about one minute I'll be walking around any questions mission denied create a new directory oh can you go to workspace are you in workspace sorry because now you're at home home right go to what happened should be able to do this yeah you were in the wrong directory just now okay so what happened was just now you were you were in this directory right but I needed you to be in this workspace directory to execute the commands yeah not my home yeah not home go to workspace so that's why you couldn't do the make directory just now so just make sure your thing your thing here matches mine when you do the commands sure but shouldn't be here and I need to go back to workspace so Ubuntu workspace okay so now you should see this workspace okay and now a make directory yeah mkdir so slash right no mkdir just do that mkdir then whatever the name so just now I already made it just now I made it already so you cannot make it one of the same name again so maybe you call it second yeah so next yeah so this will create the new directory then if you ls again you can see it right okay and then you can look at to so next step sure I love Ruby yeah okay if you having if you are a bit lost right you can go to the nodes here here CLI here this one each one yeah this one okay preview they are having the same issues or not be able to create the same directory because they are in the wrong maybe you can go okay so live preview yeah live preview then you can because I'm actually more or less following this so if you get a bit lost all the stuff I'm doing it's actually here so you just refer to this okay guys you guys may have some of you are getting issues like you had permission didn't I cannot create a new directory please make sure you are in the correct directory before you try to execute the commands you see I'm in workspace right maybe your CD in your enthusiasm about CD your CD out to God knows where already please come back to workspace the shortcut to do this just CD till day slash workspace okay and from wherever you are you will go back to your workspace make sure you do everything make sure wherever you do this thing matches with what you see on your screen okay okay who wants me to go through the creating a new file part again okay I'll just go through it again because some of you guys were a bit lost just now so how to create a new file we can create new directories with make directory we can create new files with a command called touch okay so whatever you whatever arguments you pass to this touch command there will be the name of the file that you're going to create so if you touch new directory 3.txt I will create a new file called 3.txt okay so you can actually look at your side panel here you see I created the file already right you see this I just created it from my command line okay and again how do I how do I tell if I don't have this side panel how do I tell from my command line itself I can list that new directory and you see that 3.txt this was just created by me right but when you use touch the file you create will be empty so how do you edit your file you can just open it in your text editor and add whatever you want okay and then to list the contents of your file you can use the cat command right the cat command cat new directory 3.txt and this will list whatever is inside the file that you just edited okay so if there's nothing that means your file is empty so it's okay I mean if your file is empty you try to list the contents of course you get nothing so if you edit your file and you add stuff and then you try to cat the file again then you'll get something okay let me explain to you a bit what exactly is a command so basically the terminal and this this thing called the shell that's running inside the terminal these are just programs their programs running on the machine on the clock 9 machine and somewhere probably you know on their server somewhere so these are just programs like these are code that other people have written and they run on this machine so the commands most of the commands that you're executing they are also just programs written by people but they are much smaller programs lah they're just like what we call utility programs right that you can use in your terminal so stuff like LS cat these are actually programs written by other people relatively small programs but programs nonetheless okay you have to learn a lot of times as a programmer you have to learn how to use other people's code lah you have to learn to use things that other people have written for you okay now let me talk a bit about terminology i'm being a bit pedantic but the terminal is slightly different from your shell even though people use the terms sort of interchangeably they'll say your shell your terminal whatever strictly speaking the terminal program is a different program from your shell program so like i said remember you had to learn this language called bash okay remember this you have to learn this language called bash which stands for born again shell to use your terminal so this is a separate program and it's also a language in itself it's a what we call a domain specific language that means specific to this domain right okay so bash is a domain specific language it is the language you need to learn to use your terminal so it's only for you know using your terminal you can't really use it to program more general stuff like ruby for example but like i said the terminal program is actually something that is separate from your shell program so that's why i draw two different things it's not just one thing but you need to learn how to use bash to use your terminal okay that's why people tend to refer to them as if they're the same thing but then strictly speaking they're not really but there's some people so the terminal that you're using in your cloud 9 workspace is a terminal called GNOME terminal but there are actually many other different terminal programs it's like you have browsers you have Firefox, Google Chrome all this right Safari the same thing with terminal programs there are actually many different terminal programs that you can use but the default that you're using is GNOME terminal so sometimes people they use on a Mac the default terminal is item this is a different program from what the terminal you're using on cloud 9 right now but that is even if you're using item on Mac the shell that you're using it's still bash okay so you understand the difference right the shell is the shell the terminal is the terminal so why is it called the terminal you know historical, tidbit all those bearded wizards you know what they coded on last time this kind of thing right these were called terminals they were connected to the actual computers the main frames and these are the terminals themselves are dumb there's no computation it's just a user interface so that's where the terminal term comes from and basically programs like the one that you're using on cloud 9 right now they are emulating these old computer terminals from 1950-1960s okay so that's a bit of a terminology right now let me talk okay any questions so far anyone still stuck with LS, CAD, whatever okay now that you know some of these basic commands I want to teach you how to be a little bit more effective using bash so there are some shortcuts that will make you more efficient so the first big thing is autocompletion so if you're wondering why I can type so fast it's not really because I type so fast it's because I use something called autocomplete so you notice let's say I want to type workspace I just type a W O and I press tab press your tab button to autocomplete right so if let's say I want the CD to workspace I can just press I can just type the first few characters of the path and I press tab bash will be intelligent enough to autocomplete for me so what you're thinking what if there are multiple things that start with the same few letters right then you have to type as many as is needed to differentiate between all the options so for example let's say I want to find the list of I want to execute a command that starts with the letter C A I can tab a few times and bash will return me this whole list right so you guys type C A here and press tab like 2 times bash will give you this whole list of options these are actually all the commands that it knows that actually start with C A scroll up push it upwards because some people cannot see it push it upwards oh okay okay I can't push it I can't okay I'll move it okay okay okay so if you guys type C A and you double tab bash will give you a list of commands so if you want to run the calendar command you have to type enough so that bash can narrow down the options you type tab again it will auto complete for you then you can try execute the command and see what it gives you I mean you don't really have to care about what this calendar command does you won't really use it much but you know this is just to show you that bash has this very nice auto complete feature so when you want to CD to some very very long very long name you don't know you don't want to type the whole thing out right I can just type two letters tab two letters tab okay another thing about bash is that it actually has a history that means it keeps track of a history of the commands that you execute which means that if you want to execute some command that you did previously you can easily do it in bash so use your arrow keys your up down arrow keys to scroll through your command history so if you want to execute the exact same command that you did last time you can easily do it okay scroll through if your arrow keys you can see all the commands that you did just now and once you want to execute any one of them just press enter so this is very handy when you want to repeat exact command or if you want to repeat almost exactly the same but change a little bit then you want to type everything out again you just scroll through your history and get the exact one you can edit it to whatever you want and then you execute okay another thing you may find useful is that you can do this double exclamation mark you double exclamation mark you press enter it will execute your most recent command so it's exactly the same as using your arrow key to scroll up once and press enter but you may or may not find this more handy easier to type okay and finally the last thing about using bash history is that sometimes the command you want to execute is kind of really far back off in your history you did it quite a few commands ago you don't want to scroll through all the way so what you can do is something called a reverse intelligence search so you can actually type a search string and to match whatever command that you executed previously so to do this press control R and you'll see this reverse intelligence search thing pop up let's say I want to execute the cat command from just now right and then it will show me the list of possible commands and if you want to scroll through this you just keep pressing that control R will scroll backwards in your history you can press control S oops sorry okay sorry my thing is lagging a bit but basically you can control R to scroll through and you can control S to okay I think the control S key binding is interfering I think the there's something called nice interfering with it but okay just use control R to scroll through and if you want to exit this thing you can use oh my gosh okay normally on your local machine this command will work but cloud 9 I think is interfering with some of my keyboard shortcuts so if you want to exit this on cloud 9 press control C okay you want to quit the reverse intelligence search thing yeah so if you're here and you want to exit go back to your normal terminal just control C okay so just play around if your reverse intelligence search thing and you just see how to use your arrow keys your auto complete refresh what you did in the previous section about CDing listing catting whatever and if you have any questions let us know control R okay control R to enter reverse eye search yeah it should be command I think it's command R is it I don't know it doesn't work is it like liking let's try open a new one for some reason whatever like your terminal stops working or whatever just open a new one so let's try yeah I think just now there was something hanging do you follow us what I'm talking about okay it's hanging but how I can shoot back I want I need to start from everything so if you want to if you want to open a new terminal you can open one here okay yeah then you can close the O1 but I don't need here I'd like to oh yeah yeah then you can move it drag it and you can put it to the bottom you should see you should see there was a black rectangle then it shows you can dock it there alright okay I'm not sure how to get it all the way to the bottom but yeah you can do something like that I'm not sure how to exactly because I'm not that familiar but you can move it to the bottom yeah so to exit this you can press control C so control C so you try control R again control the command control control okay so let's say you want to search for something you did just now something that start with cat but because this is a new this is a new shell so this is a new terminal so you didn't execute any commands yet so there will be nothing in the history so you can you can try doing is it recording the history here or not really it is recording this is a new shell there's not I quit the old one okay where is the where is the recorded previous for example like it won't record the previous one because it's a different process already you get what I mean it's like if you open two different browsers it won't do different browser programs it won't remember now I'm not telling about different browsers now I'm talking about this exact terminal they open new video right yeah it's not the same process okay not the same process I'm asking previous video from the previous terminal is it recording no it won't record it's gone it won't record okay so if you guys are more or less done playing around with your best history and your autocomplete and all that we're going to talk a little bit about input output redirection okay so just now firstly if you move out to some other directory please CD back to your workspace first thing make sure you are now just now we saw we can echo hello world right so this is actually a form of output echo actually outputs the hello world thing to your screen but what if I want to so called redirect this output into let's say a file so I can actually put this text into a file okay bash allows us to do this with something called IO input output redirection so I can do something like whatever string you want this arrow sign here means redirect into whatever file name so I'm just going to redirect it into one dot text now I just want you to remember previously one dot text at this stuff okay now I'm just going to redirect the output of echo into this one dot text file you see what happens okay now go back to one dot text right now the output echo was saved into one dot text and it totally over it overwrote whatever was there previously so just be aware that the you know this single arrow thing will overwrite you know when it redirects the output from echo it will overwrite your file so what if you want to you don't want normally you don't want to overwrite your file you just want to append to the end as you want to append a new line to your file you can use so so use the arrow key to go up to the previous command then you can edit it you can use the double double carrot and add your new string okay and if you go back to one dot text in your text editor you see that the new string was added to end of the line so can anyone tell me if I don't want to use my text editor how I can tell that the file was changed what command should I use cat right okay so just to recap we see the outputs we can see the content of the file with cat okay so now we've seen something called IO input output redirection another very common thing to do is not just redirect output of one command into a file but to redirect output of one command into a different command okay this is very common when people use the command line because all these commands right they're actually small they actually do something specific but you can chain all these commands together they're powerful right so we call this piping okay this command this redirection of output of one command to another command we call it piping and this bar here is called the pipe operator okay so let me fill up one dot text with some stuff first fill it up with whatever you want okay notice now I'm overwriting one dot text right the single carrot now I want to append to it so double carrot okay so now I know that my one dot text has a bunch of lines with the word bash inside what if I want to just get all the lines that only have the word bash okay we can use this we can do this with something called grep this command called grep don't ask me what it means I have no idea all the other commands so far we've seen they are like English words but by lazy people grep is something okay I don't know why is it called grep don't ask me okay but what we can do is instead of just cat new directory one dot text we can do this our pipe symbol grep the string we want to search in this case it will be bash right so you guys just in case you're confused you can put a double quotes it's also fine and you see the output it will return you only the lines of the file that has the word bash and it will even highlight the word bash for you okay so this is this example of piping basically what we know what we're doing two things here right the first thing is we cat this file then we pipe redirect the output of this the output of this part into this grep command right so we know that the output of this part are these three lines I take these three lines give it to grep and tell grep hey match it against this string bash okay and grep will give us the matching lines and it will be displayed on the terminal you know the matching word highlighted alright so we can do a lot of fun stuff with this right so imagine remember just now we run three and it's wow so long I cannot even show it on your screen what if I just want to find the files that end with .txt like the few files we created just now so we can tree pipe grep .txt right and then we just get the three files we created just now right from that huge tree right or what about the files that end with .md right by the way md stands for markdown it's a markup language like HTML okay and then you see all the md files listed okay so usually grep is like one of the most basic commands where you actually have to use where you get to use pipes and all those and those people who are very familiar with their command line they will chain commands after command after command and also it's like magic but really you only really need to be well acquainted with a few of the more basic and common commands to be able to use the terminal for your needs okay so if you look at your notes I will I say that I give you some examples that you can try with with grep and re and piping so try all this stuff here okay by the way this this hash thing right this is a command so don't type this in this is just a command to let you know what this is trying to do and to give you more instructions but just try all this stuff try running these commands in your terminal just play around okay so I will just be doing a bit of it as I go so you want to look at the screen okay there's no right or wrong so you can run this a few times see what happens with a shaft okay maybe you can guess what shaft does and if we try this command called head hyphen n1 what do we get what if we change the number behind n or what if I use tail okay so play around maybe you guys can figure out what shaft head tail does by if you don't know how something works it's not always going to be very well documented by other people you're not always going to be able to explain it to you so just explore your own and form your own contractors okay if you guys have any questions please about one minute and of course for those of you who are bored and done already too easy for you you can just go read a bit further in the notes and see what's ahead okay so I presume that you at least manage to run all those commands that I've given you as exercises so let's move on and talk about something called command options okay remember last time we tried to list ls read me and then it just return read me it's like oh so lame why am I using list on read me okay the good the good news is that you can pass what we call options to your commands that will change the behavior of your command to do certain different things so ls has an option called dash l so when you use batch commands it's basically specified by the hyphen or a double hyphen okay so in this case we use hyphen l we list hyphen l read me this option this hyphen l option basically tells ls to use what we call a long listing format so if you see we don't just get read me now we get all this maybe a bit incomprehensible but you can roughly tell what it is maybe this is a bit weird but this is like the date file size this is the name this one is a bit okay this mod is a bit involved but roughly speaking this is what we call the file permissions because different users on your system will have different permissions to modify or read certain files so this actually list the file permissions for different users on your system and this is actually if I'm not wrong like either the owner of the file or the group and the group that the owner so certain user user groups so certain users are part of us certain there are different groups on the system so users are part of different groups but for you know you don't have to really worry about this just extra stuff but basically the takeaway is that you can specify command options to get a command to do different things and you know it's not just one option the thing that makes using a command line so useful but also very terrifying to beginners is that there are many options you can pass to most of your commands so your command is actually very versatile but you have to know what options to specify so one or another common option that we use with LS and dash L is another option called H stands for human readable so actually the only difference if you compare is the file size now is in kilobyte okay originally this is in bytes which gets really long for large files okay so I want you notice a bit something here we type the command dash L dash H we can also do this this is equally valid dash LH it's the same thing so I'm wondering what am I learning all this stuff it's not directly applicable but it's about getting a basic literacy with using bash sometimes when people ask you to install stuff they give you setup instructions they'll be all the instructions of what to run sometimes when you want to debug stuff it will be helpful if you understand what is actually going on if not you just blindly copy pay stuff you don't know what's going on so like I said this workshop is not just going to be about learning ruby it's going to be learning about the whole ecosystem or idea of concepts of skills that you have to pick up to be a software developer it's not just about learning ruby okay also learning about command line because in the way that ruby run a ruby code it should command line like how we start server how we download dependency sub-concepts all that is then command line so in order for you to use ruby you need to be able to delegate between folder find out what files are there and run the correct command so you need to know some basics like don't need to be expert but at least know how to get around file, look for files so you can find things yeah okay which brings me to a very important topic Read the manual you notice it lighted over this F you can guess what it means so as programmers you have to be learn resource you have to find out learn stuff on your own so a very important part of this is learning how to read documentation right so what about bash I want to introduce to you a command called man which actually shows you the man page of a command let's say man LS tadaa manual for how to use the list command and when I said there were 10,000 options I was not kidding okay so most people I mean you ask me I also don't go and I don't spend hours reading man pages so no right but you just have to pick up the skill to how to roughly skim man pages to pick out the stuff that you need to know to execute the command to understand the command understand basically know enough to understand what you're doing okay so learning how to yeah okay learning how to read your man page is very important and so one important thing about learning how to read your man page is learn to navigate your man page right maybe now you're scrolling with your touchpad okay but with your man page you can actually scroll with your JK keys so J to move down I'm actually moving down with J K to scroll up and one very important thing you want to search for a certain option how do you search backslash you see this pop up here I want to search for the dash L type whatever search term enter but of course you'll turn out many many search results right so how do you navigate to search results use N N goes to the next result if you want to go to the previous result shift N right so I want to use N to go to the actual there so this is the actual one dash L means use a long listing format okay so US must at least have some basic generative how to navigate the man page la okay another useful thing if you want to jump right to the top because the top is the top usually they'll give you a brief description of how the program is run but now you've scroll all the way to the bottom how do you go back to the top easily GG doesn't mean good game just GG okay press GG and you go to the top you want to scroll to the bottom shift G bottom usually not so interesting just like copyright see also that kind of stuff but usually you want to scroll top GG and then now you're stuck in your man page how do you quit I mean you see it here right so press Q to quit and you're back in your terminal so I want you guys to play around reading man pages so exciting please review all the comments we did just now and man that command and you know a brief description I know it's very intimidating when I started learning common line also was this black magic but you will gradually get used to it and you just have to learn how to extract the information that you need from the man page okay so like do man cat try man CD if you man CD got some issue and you're interested in finding out more you can read the notes okay man grab man tree man shaft if you had some hypothesis about what shaft did just now you can man shaft and confirm it man head man tail I know it sounds a bit weird and if you want to like some inception thing you can man man also okay man is also a command so you want to find the man page of the man command you can man man okay yeah I mean we should call it ladies page no it's man page because it's manual okay it's not okay so read the friendly manual guys very important later when you learn Ruby also learn how to read Ruby documentation you use other people's dependent code written by other people if you learn to read their documentation okay we are going to okay I'll give you guys like 2-3 minutes play around we are reaching the end of our command line what do you call it orientation this is full screen 3pm okay guys you guys more or less done playing with man is really a baraj of words okay I admit it's not easy to read but just had to get used to it okay now we'll move on to more or less the last part and this where it will get more real and more interesting you'll ask me why I have to learn all this this is where it will start you know getting more compelling the reasons okay so we're going to be learning about how to use this command called curl so we saw the pangolin just now it's called curl okay just to be just to clarify like these are stuff I came up myself it's not like the official mascot of curl the curl command is the pangolin okay to use curl I have to talk a bit about how about how the web works so the internets the internet is run on what we call a client server model or rather like the web la the web runs on internet and the web is run on the client server model so you have what we call we call web servers that are these computers that are connected to internet that run something called server software so when people say server they usually mean they actually can mean two things first it means the actual machine the hardware that's actually acting as a server second thing it can mean is act the software itself right and usually server hardware like the hardware server must run the server software so usually when people say server they refer to both at the same time but just just so you know you can actually run server software on your computer even though your computer is acting as a client most of the time fetching data from web servers out there I use my browser I fetch stuff from github my browser acting as what I call a client so the web is built on this kind of client server model so consider what happens when you visit something like www.google.com so this google.com is something called what happens is something called a domain name and it's actually resolved into another thing called IP address so the IP address is what uniquely identifies the server machine on the internet but the domain name is the human readable name but a single domain name can actually map to multiple IP addresses because google doesn't just have one server there's many many servers to serve all the demand so there's actually a huge part of the infrastructure of the internet is concerned with mapping these domain names to IP addresses but we won't talk that much about it today what I want to talk about this is because when you let's say visit www.google.com in your browser what happens is that the google.com this domain name is resolved to this IP address and then from this IP address your browser knows which server to fetch the data from the HTML pages and once your browser has fetched the HTML page it proceeds to parse the page process it and display and as a nice page on your browser so maybe you might be thinking I have missed out something I say that www.google.com is a domain name but then you always hear about typing in URL so what is a URL what does it do with domain names so URL stands for Uniform Resource Locator and it's defined in some internet standard and there are some constituent parts of this URL so the first thing we have so actually when I type in www.google.com you notice when I press enter it's actually expanded into this whole htbs slash whatever so it's not just the www.google.com that's the URL it's just that the browser automatically expands the domain name into the full URL so there's one part of the URL which is the scheme second part is what we call the host in our case the host is also the domain name but the host may not necessary be the domain name for example you can type an IP address directly and it will still work and the last part of the URL is the path and if you're really interested in intricacies of what constitutes the URL you can go and read the notes htbs stands for HGTP Secure so when you connect to your eye banking you better make sure you're doing it over hbs because the data transmission is encrypted okay so the scheme of the URL basically kind of in this case it tells me what protocol to use to serve the data so in this case I'm using a protocol called HGTP S so you'll hear a lot about protocols when you talk about internet and I'll talk a little about protocols later on but just so you know that we have this protocol called HBS right and in this case our host is the domain name okay so like I said your browser uses this URL to fetch the HTML pages and the command curl actually does the exact same thing as your browser but it doesn't do all the fancy processing and displaying it to screen it basically just fetches the HTML page and vomits it out at you as raw text so you guys did HTML, CSS last week right you guys should know how raw HTML looks like okay that's what output you'll get from curl so let's try now curl www.google.com believe it or not Google's homepage looks very simple but this is all the crap that's actually on there you see a lot of javascript and all that fancy stuff but you guys can sort of recognise that this is HTML kind of very hard to read but this is HTML okay now you can try this also type the full URL in and you also work so actually by default actually curl says you should pass in a URL lah but if you give it just one part of it you'll try to guess what how it should use it to complete to the full URL it's just like typing www.google.com in your browser it completes to the full URL for you okay so we actually use curl to do something called make a HTTP request remember we're using this protocol called HTTP it stands for Hypertext Transfer Protocol so our clients on the web we make HTTP request to the web servers but HTTP governs the way the clients and servers talk so the client makes HTTP request the server gives a HTTP response okay so curl it's actually a tool for making this kind of HTTP request right so if you still not satisfied as explanation of what curl does what should you do man curl transfer URL right then you're using one of the supported protocols DIG file FTP FTP as golfer I also don't know like the most important thing is you know have to understand HTTP right this at least should be familiar to you but yeah there are 1001 internet protocols okay so when you're on your own time go and read about curl but now let's play a bit around a bit more we can curl type ladies page yay now you can clearly see his HTML there's no like javascript there to mess you up and I bet Elisha's name is somewhere in there how do I find out type into grep Elisha yay there but actually you see the whole thing is actually one long super long line so it still outputs the whole thing but now it highlights Elisha for me which is pretty helpful I guess okay so like 2-3 minit play around with curl a little read a man page curl whatever URL you want and we are going to cap it off with a very short mini project then we will have a break okay so read the manual read the friendly manual guys just play emojis oh right I think there's some it's just it can recognize the encoding the emoji is just encoded it's not really about the HTML it's like the terminal it's like the terminal program to recognize the encoding so we will be seeing curl again later in doing the ruby part so make sure you know him well you will also be helping us install a ruby or not it will not be part of this workshop but later on if you're interested you can come and talk to us okay because it will take too long everyone has different setups right it will take too long to I don't mind at home but I am facing a few errors tried using this or using RVM okay you could install ruby or not so you can install ruby but you cannot install rails yes and also the latest version of ruby which is like 2.0.4 what documentation are you following to install this I just search on the next stack okay later on I'll tell you I'll point you to some resources that you can look at that might be might be more straightforward because this one uses something called RVM just to manage different ruby versions and use it and I know some of the coaches there who use it maybe the more familiar they can tell you no humble is a separate thing humble is a MAC thing right there's another version manager for managing the ruby versions I use something called ASDF okay I can point you to the documentation later on we can talk about it okay okay guys so to wrap up our learning about how to use the command line I've got a mini project for you so I'll give you about 5 minutes ish 3pm to work it out and okay just to make a so follow this part here I'll leave it on the screen follow this part then you can refer to this handy table of all the stuff we've learnt just now and stop before this node with bold and caps and all this so just follow this part and stop before here stop before this part I don't want you to mess up anything CLI CLI yes this one yep go all the way you can just click this mini project yes okay just follow and don't go beyond the birds and bull sorry and if you guys are done just raise your hand and I'll tell you how to proceed or if you are super sure of what you're doing you can read the nodes and figure out how to do it yourself but if you're not sure like raise your hand first whatever you guys tea break is waiting for you guys so if this helps to spur your efforts okay by the way I forgot to mention something we may find it useful so you see create three files instead of directory with some text name whatever you like so just now I taught you how to use the touch command but it just creates an empty file so actually if you just do something like echo hello into a sum file that doesn't really exist ah this will actually create that new file okay right so it's not just through touch it just creates an empty file if you want to create a file if your stuff already inside you can just echo directly and pipe it redirect sorry redirect into that file so far so good oh how do you create them when I was in the first directory I created the second one and I list that like see it's like the tree ah no they are not you see it's the same level or not so if you open this that stuff this it's not as empty right so that's the same level you will if it's inside the second actually will be indented so after I list this how do I save the output in the file remember how do we redirect how do we how do we how do we go back to the file as well how do we redirect no cat will list the contents of the file but now I want you to list the stuff in your current directory and save that output into a file ah that's all that you know so you have to use this this one I mean no use this LS yeah which is here right so I list this then what so you can so I'm supposed to do save the output in a file yeah yeah inside let me see whether this is actually something you can do maybe I will look something maybe you can do this so if you okay so I only used it just remember I did the echo whatever whatever you can not just echo is also a command yeah so it's not just the output of echo that you can redirect into a file okay right so that means this list right there's some output right I redirect that output into a okay so you took the list output you redirected into this last thing and this was created in the first directory yes but you should create a second directory but there's just to demo okay so now create a second so after we redirect this output into the second directory is it into a file in the second directory so to create a file first yeah no you don't have to create a file first so remember this right okay okay let me if I just do something like it's created a new file called new yeah but it's under the first directory so if I want to create the file in the second directory the path I pass in here yeah must be second directory slash the file name okay okay so I just put second directory slash the file yep so I can because I don't remember my previous you know putting done right because I opened new now I need to for example to copy and to put it in my file so you want oh you yeah follow my file so so you created yeah put this one right you want but you but actually same anyway you want this okay you say just note because you put the space here so it actually cut off the text part so you see just one dot because of this space here it actually excluded the TXT part so now your file name is just one dot you realise you don't want to talk about that means it's it's not where is your actually okay you cannot see from here because no no because now you're not in your workspace now you're in the you know what I'm saying your workspace is this directory but now your your personal directory it's not in your workspace right it's not in your workspace if it's the workspace if you're in your workspace if you put PWD it should be this whole okay so actually just now you should walk through the exercise again okay okay so actually just now right scroll scroll just now yeah so just now I said go back to your workspace first lah but you weren't in your workspace at the beginning so that's why you created your file in the your directory in the room yeah you created it here I bet right so and now I no you must CD you have to CD there's no CD okay so yeah maybe I should have I'm saying so this means this actually and begin now yeah I'm sorry okay I should have made it clearer but yeah basically this from your workspace means CD go back to your workspace first lah so I agree it's a bit confusing so I'm sorry about that CD right but yeah okay I'm sorry it's a bit okay I know it's hard but I'll go through this for you guys okay so you guys will not be so confused okay guys I'm gonna go through it on my own screen so in case you guys have any confusion whatever and then we can have tea break and have fun okay so the first thing is I said from your workspace so I didn't say CD workspace I said from your workspace I admit it's a bit confusing but I meant you should CD back to your workspace before you do whatever lah okay so from your workspace so make sure that you're in your workspace first so CD to workspace okay so CD workspace oh my god workspace okay see back to workspace and the next thing is to create a new directory right so make directory first the what is with this okay so now if I list you'll see a first the over here now I said create 3 files inside a directory with some text name the whatever you like so I can say go 1 first the okay and I can change it to 2 what did I press sorry I press something on my keyboard it's doing weird stuff with me now okay and finally the third file so if I list first directory 1 there should be 1 text 3 text 2 dot text and I try to cat you should see that I have the string inside okay now I said create a new directory inside the previous directory so inside first directory create a second directory so what you can do is you can go into your first directory first and make directory second directory okay so if I list again you'll see I made the second directory now list the files in first directory and save it to a file called listing dot text so remember list will list my files right but how do I save into another file remember we learn about IO redirection so we use the carat and I want to save it in the file in second directory so you must specify that the file is in second directory okay so if we list second directory now you should see our listing dot text let me check there is the correct contents okay now I said copy the contents of listing dot text into a new file in the first directory so this is a command that I didn't go through just now but I gave it in the list for copy so I want to copy the contents of listing dot text which is in the second directory into a file in my first directory okay so you see I created a file called listing copy dot text and if you kept it you should see that the contents match listing dot text that you created previously okay finally append the string tag ladies is awesome to one of your files so we know how I append it to one dot text and if I kept one dot text I see the tag list is awesome there now I said move one of your files from first directory to the second directory so again this is a command I didn't cover but I was in the table MV stands for move so move let's say I move one dot text into second directory so I pass it the file to move and I pass it to the destination so I'm saying this basically says move one dot text into second directory right so if you list second directory now one dot text is inside and if you list your current directory one dot text is no longer there okay okay use curl to fetch data from some URL of choice I'll just use tag ladies again and save it to a new file in your first directory so use I will redirection again and if I kept my file that I just created there I got from curl and finally use tree to visualize your current directory structure so now I'm in workspace slash first directory so if I tree I will show the contents of first directory so this is all the stuff you created just now okay and then you can use grab to you know search for something say I search for okay so that wraps up the mini project okay I admit I guess I was a bit ambitious I know you guys are beginners is a bit hard to follow through a lot about it takes more time that I expected lah but anyway let's move on to the final part before we wrap up okay okay this part where I say proceed with caution so you know in Buddhism there's this thing called impermanence I want you guys to experience that subjectively so I want you to destroy everything you just made this is all the commands I type just now okay let me copy this sorry let me put it in this file okay this is all the stuff this is basically all the stuff I did just now okay I give you guys about one minute also to follow through sorry no this is not on the repo sorry I just I just made I just created this okay you guys more or less caught up so by the time when you do 3 at 89 you should be able to see all the stuff that you did okay how many of you guys are more or less done because in interest of time I want to move on or no majority are not done yet it was this eh say something please done not done done can I move on okay those who are who are having troubles please read the notes the notes are very comprehensive right and then you can slowly catch up on your own okay but I say this is not easy learning all this stuff is not easy you have to spend your own hours of practice and reading okay so I was about to make some very dramatic philosophical statement about how you should appreciate impermanence of life but I want you destroy all the stuff that you made just now how do you do it okay this is a quite a dangerous command it's like you're pressing delete but like shift delete permanently delete okay so very hard to so don't delete the wrong directory of course you can just create a new if you really screw stuff up you can create a new cloud network space but let's go and take a lot of time so just make sure you don't many typos whatever so there's this command called RM stands for remove I want you to delete the first directory we created just now cloud space cd alright this just to be sure you see the first directory here right I want you delete this okay so make sure you don't make any typos if you name it something different from first directory make the make the corresponding changes lah so RM slash R R stands for recursive because we're deleting a directory right it's a whole tree of stuff so recursive means go through that delete everything inside recursive first directory okay and press enter and don't worry your computer won't explode if you list now your first directory is gone congratulations okay we have come to the end of the command line part so you guys can you know play around a bit more or you go and have your tea break go toilet whatever you'll come back in about 15 minutes time and start on ruby okay yeah and I will put this up for your reference 101 that was CLI 101 this is ruby 101 now I want you to open oh no I want you to open your ruby nodes so it's called ruby 101 under nodes and you can open the preview again okay so let me introduce ruby to you because I know a lot of you you're just starting programming and you're like why should I learn ruby and whatever ruby is a high level general purpose programming language so high level means it's a lot of abstraction it's like as a human I can sort of write this code easily versus writing have you heard of assembly or machine code right that's very low level kind of programming right ruby is very high level okay so you don't have to care about details of how your hardware is like what architecture is using that kind of thing okay so general purpose so we learn some now bash is domain specific so it's really only used for interacting with your terminal ruby is general purpose you can write all sorts of thing in ruby web applications graphics programs game people do game program in ruby okay all these things so it's a general purpose language and it is also what we call interpreted language so you may have you may have heard of languages like Java, C++ C all these languages before you can run the program right so they actually need to save a new file with this bytecode this more low level code right and they run and then they run this file whereas in ruby you can run your ruby file straight away okay so this is what we mean by interpreted okay so ruby is a very nice language for beginners why because the syntax is lovely they say that ruby is designed for programmer happiness so also why is the ideal language for beginners is because the ecosystem is very beginner friendly okay there's a very awesome ruby community in Singapore as you can tell because we are all from the ruby community here right I don't think there's something that you find in a lot of other languages let's say you C++ C Java I don't think you can find this kind of community for good reasons probably because the kind of stuff um it's more difficult for beginners to get into okay so another thing is the ecosystem is fairly unified so a lot of ruby programmers we do Rails because Rails is the framework they will learn versus like something like javascript well down there I got one framework A there I got framework B C, D, E, F, G and everyone is using something different as a beginner you're going to get very confused so as a beginner the most important thing for you is to focus on learning one thing and learn it well first to ruby or javascript or python or whatever it's transferable so if you learn ruby well you're able to learn all these other programming languages right more easily and different languages they use for different things so in ruby the primary application is web applications okay but if you even if you don't want to build web optimally let's say I'm more interested in data science then you can always go on and learn other languages that are more amenable to those kind of applications alright now I want you to create a new file you can name it okay save it first and name it hello world.rb okay and don't save it in notes save it in okay why not create a new folder sorry create a new folder in your okay not in notes sorry create a new folder in your workspace okay name it whatever you want like my code or something oh gosh what's going on okay name it like something my code okay sorry it's a bit messed up but just save it in this folder okay so if you see this my code for the ignore this okay so you see this my code hello world.rb in bash we did echo hello world in ruby is almost exactly the same so put hello world save to file contract now I want you to go to your terminal again okay so now you're cd to your your my code directory okay so your file should be there how to run this file the ruby command ruby hello world.rb okay and we just run the command and just output hello world on our console so that's our first ruby program not terribly useful but it gets you an idea of how to actually write and run it so this ruby here it's another command it's just like a bash command so like all bash commands most bash commands you have a main page so if you're interested you can go and look at it okay so you need to use this command to run your ruby programs now there's another way of running ruby which is very helpful for beginners because it allows you to rather than writing a file and saving you can launch something called an interactive shell where you can run ruby line by line and it will give the results back to you so there's this tool called irb and if you enter it will fire up what we call the ruby interactive shell so now it's a shell again shell is just a term for our user interface so this is a way for you to interact with ruby dynamically instead of typing stuff in the file save and then you have to run the file you can type ruby code directly into this shell over here and it will be executed it will evaluate so we can do hello world in our shell also so we can put hello world and more or less the output is the same you notice there's this funny nail thing if you're curious about this go and read the notes I don't want to spend too much time on this but more or less it does the same thing it puts hello world will print hello world to your screen so irb it stands for the interactive ruby shell so you'll fire up something called this shell which is something so that means you type in something it will read it and when you press enter it will evaluate it and it will print the results out on the screen for you to see so you can interact you can try our ruby dynamically oh can't see the text can you see it now oh no it's blocked is it increase the screen the text size puts hello world I don't know what happened okay everyone can get your hello world running in your irb right so irb okay just okay note just follow me now just follow me now okay okay irb enter you will enter this you'll see this prompt here this interactive shell allows you to write ruby code directly and get the results printed back to you so rather than save your ruby code in a file and running it you can actually type into this shell and you will you can get your result immediately la very good for learning sorry it doesn't exit okay what happens if I need to enter my oh it's still in the if I need to enter my irb okay can you show the code before I finish okay okay okay guys there are two ways of running ruby on your machine let me go back to the first way first the first way is you save your ruby code in a file first so you see i have this hello world the rbfile okay I created this file and I put the words put hello world and I save it okay so how do I run this ruby code how do I run this file so now I'm in my I'm in this directory right ruby hello so ruby hello world dot rb oh sorry a bit of a typo over there okay ruby hello world dot rb okay you should see hello world printed on your terminal if anyone has any issues at this point okay so ah control d the steps from your make direct okay okay to the hello world and then the touch oh I didn't touch or anything okay so guys the cat the cat where they create a new file oh no there's nothing it's true the GUI so before that right before that when you make directory and you said they got confused what is the folder and make directly share that folder and then also the cat hello world dot txt is to create a new file but some of them are already lost you mean at this stuff before that before that all your commands to put before that okay there's nothing I did everything through the GUI it's not I didn't do anything through the terminal no no okay guys I'm not doing anything how to create this new file I'm not doing it through the terminal you can do it if you want if you know how to use the terminal but just use cloud 9 user interface go to file new file right file new file you control s to save it somewhere wherever you save it I don't care make sure you know where you save it okay I'm gonna save it under this my code directory okay everyone okay if saving creating a new file this is just cloud 9 thing okay then in this in this file puts hello world open this file on cloud 9 puts hello world inside the file not on your terminal okay then save it now in your terminal make sure you go into that same directory where you save the file so in this case I mean my code because that's where I created my file in this directory ruby hello world.rb okay then press enter and you should see hello world on your screen any confusion so far I didn't use touch or make there actually anything I just created the file from cloud 9 so there is one way of running a ruby program okay using ruby and then the file name the other way I want to teach you guys to run a ruby program is to use something called interactive ruby shell which is called IRB so follow me type IRB on your terminal press enter you should see this prompt here and I can also execute the exact same ruby code I saved in the file inside this IRB shell so if I do puts hello world enter you see IRB actually prints hello world for me and actually also prints a bit of extra thing don't worry about that for now okay and you notice you're still in the IRB shell you still have this prompt here so if you want to exit and go back to your normal terminal control D okay now why is IRB useful is because I don't have to save my program in a separate file then run ruby that file if I want to test out something very quickly I can do in IRB but normally of course when you write longer programs you have to save in a file run it but IRB is very good for beginners learning about the language okay control D to quit okay so I want you guys to play around in IRB a bit more so go back to IRB if you exited just now and we can do math in ruby sorry so IRB is something called the ruby interactive shell so you can use it to execute ruby code immediately instead of saving it in a file and then you have to go to your terminal again and run ruby the file name you can open this IRB shell and you type ruby directly inside ruby code directly inside and it will evaluate it for you and print the results onto your screen okay so it's very good for learning how the language works because you can get the feedback immediately okay so I want you to play around a bit more other than doing puts hello world you can do math right one plus one enter ruby gives you the result okay play around a bit more just try some math if you want to multiply stuff we use the asterisk okay what about taking stuff to the power can you guys see if I want to calculate 5 squared in ruby the syntax syntax means how to write the language the syntax for taking to a power is the double asterisk so 5 star star 2 means take 5 to the power of 2 okay 25 we can also do division so 5 divided by 2 you notice 5 divided by 2 is 2 why is that because we know 5 divided by 2 is 2.5 what happens is that ruby by default does something called integer division so if you give it integers that means whole numbers 5 and 2 are whole numbers you give it whole numbers and you try to divide whole numbers it will still return a whole number to you so it's not going to return you 2.5 if you want the exact value 2.5 you can make sure one of the numbers is not a whole number that means you put a decimal point there and ruby knows that this is not an integer this is supposed to be something called float that's how your computers represent numbers that are not just whole numbers so if we do 5.0 over 2 we should get 2.5 and you can just do 5 over 2.0 you should also get 2.5 so basically as long as one of your numbers is a float you will get a float result but if both are integers you're going to get an integer result float integers you can also do the same thing whether it's float integer you can do subtraction addition all that it's just that division this tends to be a point of confusion a lot of people ask why 5 over 2 is not 2.5 it's because there's actually what we call different data types being used following not following as it's somehow following okay just first first that the first thing that we said right doing a good follow-up that's not a command on your bar it's you're technically writing a movie like that so you're supposed to do it to be spiced so you can make how word has dot now extension like I said so you open a file you write a language you're writing code in it but it puts follow but it can't be random text and then you save it follow word dot which means ruby script then you go into your bash the formula and you turn bash to execute the ruby and for that you use ruby which means it's same use ruby language so then ruby follow word dot so it's not the same as all the commands basically ruby is not bash bash is a separate language ruby is a separate language so you have to learn different what we call different syntax okay the language the way the language is written is different okay so we can do arithmetic we can add numbers multiply numbers turns out we can also do something like multiply strings so this this is like something like hello world right it's something a data type called a string so basically just a sequence of characters we can actually also use the star to solve like multiply strings it's not really multiplication lah but you see what I mean so we hello world star 3 we actually create a string which is hello world repeated 3 times so it's it's mostly string concatenation not really multiplication okay and we can also sort of add strings together so again concatenation okay so we added the two strings together to get a a bigger string now if you think I can sort of like multiply a string by number what if you do it the other way round you can try please try and you first you just saw your first error in ruby okay your first error message a lot about being a programmer a lot of it a lot of time you'll spend is reading error messages so you better know how to read error messages of course at the beginning it's going to look very cryptic you're like what is this string can't be coerced in a fixed num like what is fixed num what do you mean by all this right as you learn the language better you'll know how you'll get to a feel for how this error messages are written and you'll be able to in a program you'll know how to debug it these error messages are actually telling you something helpful so this is telling me that I'm actually trying to do three multiplied by a string so even though I could this actually the string multiplied by three actually makes sense it's like the string repeated three times but what does a number multiplied by a string mean does it make sense to you not really right so in this is the here okay so an important takeaway is you know learn how to read error messages okay so that's enough of playing around with IRB let me talk a little bit about what we call data types so I mentioned it briefly just now already so in a computer program we have to represent our data through various types because how do I know that this is a a number or how do I know that this is a sequence of characters this kind of thing so I know that this is like a sequence of numbers something like that the program has to store these things has to assign a certain type to your data so these are called data types so we saw some stuff like integers already right integers something called floats strings but Ruby has many other data types right and okay so five is integer 5.0 is a float strings something called symbols so if you put a double colon and a sequence of characters this is what we call a symbol type in Ruby so it's a bit kind of like a string but slightly different I won't go into details now because I think you'll just get confused but just be aware that symbols exist okay another thing and a very useful type called arrays so a list of values so I can have array of numbers of integers so imagine like you have three boxes and each box has one one box has one another box has two another box has three those are arrays it allows you to store a sequence of values and of course you can have arrays with different types inside so I don't it doesn't need to be an array with all numbers I can also have an array with a number and then a string and so on okay serius and another important data type is something called the hash or in other languages they might refer to it as a dictionary this is a data structure that allows you to store what we call key value pairs so something like this I'm saying this is a dictionary with two keys A and B the value associated with key A is one the value associated with key B is two okay this is hashers are used a lot right and you will see later when we start doing more complicated programs right so if I want to do something like store my name and I don't know my favourite number this is totally made up I can store it in a hash with where the key is a string with my name and the value is the number okay so you notice the syntax looks a bit different between these two hashers right but they are both they both are surrounded by the curly brace okay why is the syntax different so this one the keys are the type of the key is a symbol this one the type of the key is a string okay when you have symbol keys you have to use this you can use this slightly different syntax okay it's more it's easier to type it's just the letters and the colon and then the value okay but just be aware that they are both hashers okay okay another data type you'll see is something called nil nil is a value used to indicate that there is nothing there kind of right and another important type is boolean a boolean types so true or false okay so if the value true and the value false why do we need these boolean values because programming is all about logic right how to control your logic you have to have conditions conditionals and these conditionals you have to pass in some boolean values whether this is true then you do this if this is false then you do that that kind of thing so we need to have these boolean values in our program okay how do I actually figure out what are the values I know I'm typing all this stuff I'm telling you that you know this is a string this is a number whatever this is an integer but you can't actually see for yourself right how do you see for yourself you can use class and this will tell me the class or the type of this value so in this case it actually gives me fixnum I told you it's an integer yes fixnum is an integer it's just a bit of an implementation detail but don't worry for all purposes think of fixnum as an integer right Ruby calls them fixnums but they're basically integers okay what about 5.0.class float right what about an array array okay right so this is how you actually inspect the type of your of your data okay now is that I need to talk about a very important thing called variables because now so far we've only been typing the values the raw values directly in IRB but a lot of times we want to get these values and pass them around and do some extra computation on these values so we need to store these values somewhere and refer to them afterwards so we do this using something called variables okay so imagine I'm doing this thing now a very important point to note this looks like math x equals to 10 it's not math it's the the meaning of this equals it's different from what you're familiar in math in math it's like equality in programming it's called assignment meaning we are taking the value 10 assigning it to x okay and then if I use x now you notice it now has the value 10 right IRB actually evaluates the value of this variable and prints it out for us so now after I assign 10 to x the value is now 10 la so I like to imagine the metaphor I always used to explain this is you know using octopus so imagine the value 10 is somewhere in your computer memory and your variable is like this octopus called x that is grabbing onto this value 10 okay and after I've assigned x to the value 10 I can actually reassign x to a different value right so let's say now I want x to assign 15 to x so if I type x now the value of x is 15 so just imagine visually reassignment it's just grabbing on your 10 is still there somewhere in your memory it's just grabbing on to a different value 15 your variable is still called x it's the same octopus okay nah and with your variable now now your value store on your variable you can use your variable to do some computations so now x is 15 right we can do stuff with x so you can x divided by 5 x plus 3 x times 20 all this right and then we will just oh sorry sorry make sure there's a space between your your dividing sign and 5 so x divided by 5 we should get 3 right x plus 10 we should get 25 right we can use the variable in place of the actual value to do all my computation okay so that's why we need you have to understand the concept of our variables okay we need to pass values around we need to store them somehow we need to reference them by some name right okay so I can actually also do this so let me reassign x to another value now it's a string hello world what happens if I assign x to another variable so if I if I check the value of x x is now hello world if I check the value of y y is now hello world as well so basically I took the value of x and assigned it to another variable called y and graphically you can visualize it like that first I have x x is assigned the value of hello world and then I create a new variable y that is also assigned hello world right and you notice they are grabbing onto the same the same value it's not like I copied this and and grab onto different signpost it's the same value so the x and y are referring to the same value in your computer memory okay so how can I illustrate this what if I want to if I want to change the value of x what we call in place so we know x refers to this particular place in your computer memory and there's some value stop there I want to modify that value there directly okay so let's say I want to convert my hello world string into all lower case so there's a method for this called down case exclamation mark so if I do x dot down case exclamation mark you can see the result is hello world in lower case and if I check the value of x it's now hello world in lower case but the tricky thing here is now if I check the value of y it is also hello world lower case why is it because remember just now I said they're holding on to the same value you are changing value directly even though your reference referring to it true x but you're just changing that same value so when you try to refer to the value true y again you're just you're going to get the value you changed when you refer to it through x okay I just check their holding on to the same value if I say x is hello world and y is equal to x both x then it won't change because what happens if you change if you say x equals to 1 now right basically this octopus will remove will flag off this and extend tentacle to a 1 but this octopus is still holding on to this right so you get the visual imagery of that okay what's happening okay so like I said let me skip like I said if we down case if we do x dot down case we are actually modifying this value so obviously when you check the value true y you're going to get the value that you modified true x okay okay so before I finish this section of variables important thing is to how to name your variables in my notes I use a lot of stuff like x, y, z this kind of thing because you know this is just just to show you how the language works I'm not actually writing a real program but when you actually write real programs naming your variables full of names like x, y, z, a, b, c what is this supposed to do if someone reach your code they're like what is what is x it doesn't tell me anything about what x is storing so normally normally later you'll see when we start writing something more complex we want to name our variables sensibly and good variable names are very important in becoming you know a good programmer okay because it's about communication you're communicating to other people and to your future self what your code does imagine you write this one week later does it tell you what x is? no, right? if you say something like my name equals ginsi and then I come back to my code later and I look at the variable my name ah, I know I know what's the meaning of that value stored in the variable okay okay now I'm going to talk about a very important thing called control flow so like I said just now we have boolean values if true do something otherwise if it's false do something else this kind of logic condition do something if otherwise some other question do something else this kind of thing is called control flow we're trying to control how our our program the logic of our program right so you normally see flow charts when we try to describe how the logical how the logic in a program works so the first control flow structure we're going to look at something called loops so you want a program that does something repeatedly over and over and over again your shell it's actually looping right it's actually a program that's running on a loop it's doing the same thing over and over again just looping waiting there for you to give it input right so actually the control flow structure that a shell program is using is a loop so how do we write loops in Ruby so now we stop playing with IRB because we need to write multiple lines of text so another thing is IRB you can only write a single line you can actually write multiple lines but it gets very tricky it gets very difficult ok so now i want you to go to exercises Ruby 101 create a new file in this folder ok name it loops.rb ok and let me exit my IRB shell from the terminal so how do we write a loop in Ruby like i said Ruby actually read a lot like English so we do loop do and next line we do end so whatever inside between this do and the end is called loop body so that's the code that's going to be repeatedly executed ok can put whatever string you want now let's try to run this so again ok go back to workspace first and then go into exercises Ruby 101 ok you should see your loops.rb inside CTRL D ok is this ok unless i have to play around with preferences ok ok so i want you to run this Ruby program how do you do it you guys should know by now Ruby loops.rb ok bigger ok let me increase my font size a bit is this font size better ok a bit bigger ok so if you've tried running this already ok run it Ruby loops.rb and you see your terminal is printing infinite loop cannot say that fast enough but it's basically looping infinitely it's never going to exit right because this program is just saying loop and then put this value to screen so how do you quit this program to get back to your terminal CTRL C ok you have to maybe it's cloud has a bit laggy so maybe you have to spam it a few times just CTRL C, CTRL C until it quits ok so you just saw how to do loops in Ruby right that's one very basic control structure control flow structure ok another very basic control flow structure ok before that let's write something more substantial in our loop first ok so I want to write a small game that will go on forever because I want to torture you guys let's write a game first that where you just roll a die and you just keep rolling a die and you just put the output of the die to screen ok so how do we simulate a dice roll in Ruby ok let's use our knowledge of variables first we write a variable called num assign it the value of this random.rand 6 plus 1 ok this looks like a bit black magic but basically random.rand 6 you're using something called a function in Ruby but basically maybe I'll talk about functions later on but basically this random.rand thing returns a random number between 0 and 5 ok not 1 and 6 between 0 and 5 always starts from 0 up to but not including the number you pass in so if you pass in 6 it will stop at 5 so this generally is a random number from 0 to 5 obviously no dice that we know no die that we know of has a number 0 so we add 1 to the final result right so that we'll get a random number between 1 and 6 ok so that's our dice roll puts you rolled hash curly braces numb closing curly brace ok what is this funny hash curly brace thing now you know stuff in double quotes are basically strings ok so actually you're putting a string you're printing a string to screen but you see this funny hash double quote thing double curly braces thing it is a way it's something called string interpolation that means we have a variable numb that stores a certain value right we want we want to put that value into our string so we interpolate the value into our string so we use this funny hash curly braces syntax to do string interpolation so that means let's say numb is 6 what will be printed is you rolled 6 ok if numb is 1 printed you rolled 1 ok and just try out now run this program again ok you can see I'm rolling dice again again over never going to end ok then try to quit this program maybe a bit laggy so obviously infinite loops are not very well some programs you need to loop keep looping right like your shell but most of the time we don't want our programs to keep running forever ok so we have what we call a looping condition that means break out from this loop when I meet this condition ok so in this case we're going to design a very simple game I want to break out I want to stop rolling dice when I roll 6 ok how do I do that break if numb equals equals 2 equal signs are 6 actually a lot of things going on here first is the break because this means break out of the loop ok and this is actually like English break if numb has the value 6 now notice it's a double equal sign it's not a single equal sign so the single equal sign and the double equal sign mean very different things so don't confuse them a lot of beginners tend to get confused between the two but don't confuse them double equal sign means that we are comparing the value stored in numb with 6 we are not assigning 6 to numb we are comparing the value stored in numb with 6 equal this is equality in math ok and if they are equal it will return what we call a Boolean value remember true and false just now so if they are equal it will return the value true if it's not equal it will return the value false right so it's really like English break from the loop if my number is 6 ok and let's try this now it shouldn't loop forever this time sorry I forgot to save my file so always remember to save your file after editing ok ok ok anyone have any issues with our loop so far you understand about looping conditions right basically you're saying that I want to break out of this loop after this condition is met ok so this loop will no longer run forever it's gonna stop looping when my number when I roll the number 6 ok so if I run the program it will run until I roll 6 la so you notice that this type of loop the way it loops is keep looping until I meet some condition then break out right but what if a lot of times you want to loop a fixed number of times let's say I only want to loop 30 times so let's say there are I don't know how many like 30 40 participants here I want every I want all of you to play this game I know it sucks but I want you guys to play it and how do I simulate this in the program so there's another way of looping called using a method called times so let's say there are like 40 of you guys here 40 dot times do end right and it's not strictly necessary but to make your code look nicer you can indent everything right usually people will indent whatever that's inside the do and block body ok let me change it down to like 3 times so you can so it won't run so long ok so if you run this now you notice that my program my I actually ran the inner loop so this is like a loop within a loop so I ran this inner loop 3 times right so if you see my output here I exited right from the inner loop because I wrote 6 and then I tried to do this loop again so now I wrote 3434 5 6 I exited the inner loop but now I'm still in the outer loop so I do this again right and then I did inner loop again until the end ok so this is basically saying for 3 times do this right you break it down into this component the first loop is do this 3 times what is the this is the inner loop ok anyone got any problems yes so let me make it more clear exactly what this 3 is supposed to mean so I wanted this to be like the number of participants right so remember I talked about naming your variables well let's name it let's assign the value 3 to the number of participants obviously it's way more than 3 but you know just for demonstration then I replace this 3 here with num participants ok and your program will run exactly the same we just made it well you could say a bit more readable because I can see what the 3 is supposed to mean agree the 3 is the number of participants and next time if I want to change the number of participants I can easily do it on this line ok so if you run it again it's the same basically does the same thing la ok any problems with loop so far remember we have seen how to do loops in 2 different ways one is using this loop using loop do end this one is basically loop forever and ever and ever until I meet some condition right then I break from the loop ok the second way of looping we encountered is loop a specific number of times so this number dot times do end do this stuff here however number of times ok so 2 different ways of looping any issue with loop so far ok I will talk a bit now I will go on to talk about another very important control flow structure called conditionals and you actually saw it already you see this if over here it's a conditional statement right if something do something ok so conditionals are another very important control flow structure so again create a new file please name it conditionals dot rb in the same directory in exercises ruby101 now create a variable ok this variable will store a boolean value so allow me to be a bit I don't know narcissistic if you will so if you are enjoying yourself you put awesome workshop la so this is one way of using an if statement so it's a bit cut off there's a second way we can do if if and drawing myself and the end puts awesome workshop so you will see both different syntaxes all the time ok so let's try running this program now it's ruby conditionals dot rb and of course because I assign enjoying myself to true then obviously it prints awesome workshop la you can feel it whatever value you want but ok why are there these two different ways of writing an if statement well it depends on what you use case so the first the first way the first thing is more concise it reads a bit more like English right but a lot of times you want to have multiple lines for that one condition so let's say I have another variable called numAndroidWorkshops so I presume all of you have enjoyed the previous workshop also now if I want to increment ok I'm losing this this funny plus equals thing here this basically says originally my numAndroidWorkshops variable has the value 1 now I want to increment that value by 1 meaning I look at my numAndroidWorkshops variable so after that value I add 1 to it and I put that resultant value back into the same variable so after this line after this line has executed your numAndroidWorkshops will be 2 so this plus equals thing means increment the value of numAndroidWorkshops and store it back in the same variable name so why will we use this second way of writing an if statement is because we can do multiple lines like this we can't do this with this form unless we repeat the condition again which is not very nice imagine you have a very very long block you're not going to write you're not going to copy this and paste do this if the bigger can no no yes no ok so try running this code ok wait hold on let me just make another small id so you can see that the value of the variable has changed so awesome workshop number write we do string interpolation again ok so this is what your coach law at the end I'm sorry my cloud line is lagging continue no so this is the problem of cloud id it's a bit laggy la the upstrip let me try that again ok sorry guys having some cloud line issues but I hope everything was saved and other things ok now let's try running this so go back to your terminal oh yes ok so I didn't save it yet so let's add this in ok save it now run the code ruby conditionals to rb so you should get the numandroid workshops number printed out la to screen ok now let's look at a slightly more complex program more complex thing so now we only have an if statement but what if we want to have multiple conditions so this is something called if else if else control flow structure ok so you can delete all this if you really don't want to delete it you can create a new file but I'm going to delete all of it now let's say x equals to 10 if x greater than 10 I'm going to put x is greater than 10 obviously else if notice how to spell it e l s i f x equal equal to 10 ok try to make the indentation align with the if puts x equals to 10 else puts x is less than 10 you guys understand the logic right it's pretty straight forward if x is greater than 10 I will tell you that x greater than 10 if x equal to 10 I will tell you that x equals to 10 now you notice I don't I put a else here instead of a else if x less than 10 right what's the reason for this it's because else basically says if the condition doesn't fit the first two then do that ok so it covers everything else so I don't have to specify an extra condition so how does this actually look like graphically it's a flow chart like this I don't want this to help you understand better how the logic flows through your diagram through your program but basically program starts we assign x equals to 10 and then we check is x greater than 10 if yes do this dan kemudian kita nampak. Jika tidak, kita periksa lagi. Ia X betul-betul 10? Jika tidak, periksa ini dan program N. Jika tidak, kita periksa lagi. Ia X. Dalam kode, kita menggunakan L. Jadi kita sebenarnya tidak mempunyai periksa ini. Jika kita menggunakan L, kita tidak mempunyai periksa ini. Dan kita baru saja menggunakan ini. Tapi jika kita menggunakan... Apa maksud saya? Jika kita mempunyai L, jika kita menggunakan L lebih daripada 10, kemudian ia akan menjadi apa yang anda lihat di sini. Tapi kerana kita menggunakan L, kita tidak sebenarnya memperiksa apa-apa lagi. Kita hanya... kita hanya membuat ini jika tiada apa-apa yang diperkenalkan. Maksudnya, kita memperiksa ke sini. Itu adalah perubahan dengan L dan L. L memperiksa kondisi. L bermaksud apa-apa kondisi yang tidak diperkenalkan, kita hanya membuat ini. Jadi di sini, program kita sebenarnya... You see this footage, it actually covers all the logical possibilities. X can only be greater than... Either be greater than 10 equals to 10 or less than 10. There's no other possible value of X unless... you found a very strange number. But our if-else-if tree may not always cover all the logical possibilities. So what do I mean? What if I do something like... If I do something like this, you notice the logical space covered by my if-else-if. It does not cover all the possibilities of X, right? X could be equal to 10 but it's not covered in my... in my branch. So what this means is that... basically it will check this, false, don't do anything. Check this, false, don't do anything. Then the program just ends already. Okay? So graphically... it's like that. You see the difference? Okay? Alright. Okay, now let's use our new knowledge of conditionals to write a better game. Because the game which I'm calling, it's pretty boring. You just keep rolling until you get 6. Now let's rewrite... our Rodeye thing. So I want you to... create a new file. Save it as Rodeye.rb. In the same folder, exercises.ruby101. Now take this... the inner loop part. Copy this part. And fix the indentation. Okay? You can use shift tab to unindend. Okay, so this is the original Rodeye game. Now I want... I want it to be such that... if you roll 6, you win. So the program will print you win to screen and it will break out of it. So... let's rewrite that with our new found knowledge of using if, else if. So get rid of the break line first. Now if... num equals... equal equal ah. Not just a single equal. Equal equal 6. Puts. You win. You can be as enthusiastic as you like. And then if you win, you stop rolling the dial. You break out of the loop. Okay? So this is actually the same logic we had just now. If you roll 6, you break out of the loop. But now in addition, you print out something. Now if... if I just do this. Let me run the program. Ruby... roll a die. dot rb. Okay? So... when you roll 6, the program will declare that you won. And then it will exit. Obviously this is a very boring game because you will always win. So let's make it more interesting. We have a lose condition also. So the win condition is you roll 6, right? So to introduce a lose condition, we should use what? Else? Else or else if? Let's say I want to say that you lose if you roll a 4. Else safe, right? Because what if we use else? That means if we roll anything other than 6, we will lose already. Not a very fun game also. We don't want to keep losing. Okay? But regardless of whether we win or lose, we want to break out of the loop. Okay? So the break must be there in both lines. So let's try to run a program again. Oh, I lost. That sucks. I lost again. Oh, yeah, I won. Okay? So just try to play your game for a few times. Why number 4 when you lose? Because I don't know like 4 is an unlucky number. Okay, anyone got any issues? Anyone got any questions about if else if? Cannot get the code to work? Anything? Okay. Now I will talk about another very... Okay, everything is important. Because everything is basic so everything is important. Now I will talk about an important thing called functions. So remember, I sort of refer... I thought about functions a bit just now. But you can think of functions as a bunch of code grouped together and given some kind of name so that you can re-execute that same bunch of code later on in different parts of your program. Okay, what do I mean? Let's say I have this row and die game, right? Now the program it does the loop once and that's it. What I'll be very good is if I can put this bunch of code put this into a function and then I can what we call call that function multiple times next time so I can play this game multiple times. You know what I mean? So it's like reusing this whole block of code. Okay, I'll explain more as I go along. So DEF stands for define. So DEF roll a die in then everything and finally an end at the bottom. So we just defined a function called roll a die. Right? So now I want you to try to run your Ruby program. Eh, what happened? Nothing happened, right? This is how you call a function. And remember when you call a function basically how Ruby reads this, right? It looks at this line and you define a function but it doesn't execute stuff here. Then after it finish just looking at your function it sees this line and you're like, oh, execute this function and then Ruby will jump back to here and execute all this. Okay? So when you define a function yet Ruby is not executing this stuff yet. Okay? So if you run your program now then your program will execute you're calling your function. So why don't I use function so I can reuse it later on? So I can actually call roll a die again. Right? And now I will play the game two times. Right? And what if like just now we wanted to play the game like 60 times or 3 times? This is kind of a bit more readable, right? You're just saying for 3 times play a game called roll a die. The people who read this code don't actually have to care what is in a roll a die. They don't know it's just a function that does some stuff. So it's what we call encapsulation. You are taking this bunch of code. You are encapsulating it in a function. You're naming it something descriptive. Right? And makes your code more readable. And it allows you to reuse the same function in different parts of your code later on. Okay? So if you run this program now you will see that sorry and you save. If you run this program now you will see that I'm playing the game 3 times. So you win, you win, you lose. Alright. Now let's make our game even better using functions. We're going to do something called refactoring. So refactoring is this very fancy term programmers used to say make your code better. Rewrite and modify your code. So that it still does the same thing but it reads better when other people read your code they have a better understanding of what it does when next time you want to come back to your code and modify it again because you have this better understanding your code is structured better you can more easily modify and improve on your code in the future. Okay? So how can I refactor this code make it look nicer? One thing that is really bugging me is this very ugly thing here. I mean, yeah, it works but you know this ugly whole if-else thing here I want to take it out into a function on its own and name that function something descriptive. So what is this part of the code doing? Basically it's checking for when the game ends. It's checking for the end game condition. So maybe I should take this out into a function called game end question mark. The question mark is part of the function name because it's like imagine how am I going to call this function? I'm going to check if game end then break. Okay? So just follow with me for now. Okay, fix indentation. Now I'm going to this is how I'm going to use the function. I'm going to say if game end. Does it read like English? Yes, right? If I read if I okay, I don't have to care what is inside here but I just read this. I know what this is supposed to do. I'm going to basically I know this function is checking for my end game condition somehow. All right? Now we need to modify the game end function a little bit. So instead of breaking inside the function because you can only break from a loop, right? Okay, so the question mark is just part in Ruby it's a convention to name your functions with a question mark behind if your function is returning a boolean value. So if your function is returning true or false put a question mark it's a convention. You don't have to follow it by just a convention because why it reads more like English, right? You can read this sentence like English break if game end, right? Okay? So we need to modify the game end function a bit because we cannot break inside the function, right? You can only break from a loop. But now we took the code out here. Remember we said our game end function should return a boolean value. So we want to return true, right? If you roll a 6 your game should end, right? So you should return true if you roll a 6. And even if you roll a 4 should you return true? Yes, right? Because you still want to end your game, right? Remember the value that game end returns is whether or not my game has ended, okay? So now I've refactored our program and it does but it's nicer now. It reads more easily. Like I said it reads almost just like English. So run it again. Sorry. Ah, yes. Okay. Another issue sorry, I forgot about this. But an issue is that notice that num, the variable num is assigned here. But somehow we're referring to num in our function. But our function actually doesn't know what this num is because it's a separate bunch of code already. So you have to find some way of passing in the num to this function. Okay? So we do that with something called arguments. So you have to put parentheses num over here. Okay? So when you call the function game end you need to pass in the number. Meaning when I do this function call here I need to pass in num. Okay? So what happens in this statement num is a sign sum value let's say 5 then you wrote 5. Break if game end you pass in the value 5 here basically. Right? And then game end takes the value 5 is stored in the variable num and then you check. Num equals to 6? Well, no. It's 5, right? Then you check. Num equals to 4? Well, no. Okay? So the game has not ended. Okay? So we are actually passing the value of num into this function through what we call an argument. So num is the argument that we pass to this function. Okay? All this terminology is confusing but basically when you call a function you need to pass in arguments. So for example when I call this row a die function it didn't take any arguments so I don't have to pass in anything. Okay? But a lot of times the functions you need to write they need to take some argument somehow to do some to process the values or to determine what to do. Right? So now if you run the program again it should work. Okay? But it's easier to modify this and make it more complex now that you've put everything into functions. Okay? Let's make it even better. Right? Now we have a... Now we are hard coding the value 6 and 4 inside here, right? We are saying that 06 win 04 you lose. But maybe I want to have different variations of this game where the win and the lose so how can I sort of abstract that out instead of hard coding 6 and 4 into it? So I can actually specify that my function going to take even more arguments. Let's say win number lose number or lose num or win num lose num type less. Okay? And I replace 6 with win num. Replace 4 with lose num. Now you notice if I try to run this now it's going to be an error because why? We say that our function should take num win num lose num. It should take 3 arguments but when we call the function we are only passing in 1. So the number of arguments when you define a function so when you define a function you're saying it should take 3 arguments but when you call it only pass in 1 that's obviously going to show an error. You must pass in the same number of arguments that you define your function to take. So we can just do this. Right? So we're saying that win num is 6 and lose num is 4. Right? And now it should work. Okay? And now if I want to change the win and lose conditions I can easily change it like that. I don't have to rewrite the whole if-else statement. So you see how functions have helped us make our code more readable and easier to modify. Okay. That's about all I'm going to talk about functions. Let me talk a little bit more about any questions about functions so far Alright, let me talk about these two very important data structures or data types called arrays and hashes. Okay? So Okay, open IRB again. Remember? Type IRB in our terminal. So arrays, right? Allow you to store sequence of values. So let's say remember I introduced the cast of characters today. Right? So how can I fetch these values from the array? It's something called indexing the array. So let's say I want to create a string which says your coach today is Jin C and my name, you know, my name the string is stored inside the cast the cast array. Right? So how can I fetch the value of my name from the array? So we do string interpolation again. Remember this hash and the curly braces. Then cast cast is the variable that stores our array. Square bracket zero square bracket. Right? So actually this is not the cast square bracket zero but it's the part that fetches that value from your array. Zero means fetch the value at this array index. What do I mean by index? That means the position that element is in the array. So you might think why is it zero? It shouldn't be one because that's the first element, right? Just by default and most programming convention is to start from zero. Right? So this is element zero. So if I want to fetch the second string, okay? Right? So this is how you fetch the values from the array. And you can actually also put a negative number. What does the negative number mean? It means you count from behind. So negative one means the last element. Negative two means the second last element. Negative three means the third last element. So if I put cast negative one you should get the last element of the array. Okay? And of course, arrays, you can actually store any kind of data in an array, right? So you can actually store arrays in arrays. Or arrays in arrays in arrays. You can nest however many arrays you want. So, for example, let's say you have an array that stores some points in Cartesian coordinates, right? A point has an X and Y coordinate and let's say that point has a certain color. So I want to store an array of points. So let's say my first point is at X position one, Y position two and as color red, for example. And then my second point is at X position three, Y position four and as color blue. So you notice, remember, how to create an array is using the square brackets, right? So I'm actually creating an array of arrays, right? So arrays within arrays. Okay? And how do I get the element inside the inner array? Because if I just do points zero, it's going to return me the first element of the outer array, which is this element, right? This itself is an array. But what if I want to get the value rate, right? Not this whole array. I want to get this value. So I can actually index twice, right? Okay? Everyone got it so far? This is how we index arrays. And another very important thing, if we store a sequence of values, usually you want to loop to do something with them. So for example, let's say, okay, go back to your row and die code, modify it a little. Now I want to pass in a player into my row and die function. And I want to modify it a little. I want to say player wrote this. So players, I intend it to be a string, so the name of the player. Now, okay, I can get rid of this. Let's create an array of players. Okay, you can name whatever you want. Just putting some of our assistant coaches over here. And now to loop over this array. So let me say I want every player in this array to play at the game. How do I loop over this array? Players dot each do player and so what this means is that the player's array, right, is this whole, this list of strings here. Now this thing that you pass in here is actually going to be the individual string. So first it will go to the first element. So now player have to value string C. Second element, player will have to value k. Third element, player will have to value Elisha and so on and so forth. So which means we can pass in this value into our row row or die function. So back in your terminal exit from IRB control D and let's try running row and die again. Row or die. I keep saying row and die because actually in my original planning I had a plan to do a bit more stuff but never mind. Okay. So if we run this program now you see that yes I am printing out the player's names instead of just you wrote something something. Okay. And what we call looping through the array or iterating through the array. Right. Going element by element. Okay. The next very important data structure is called hash. Right. Remember I said the hash is okay. Scrub. So again we said that row and die the function we take in an argument which is the player name. Okay. And then when we actually call the function later on we need to pass in the player name. So let me tell you that messages is going to be a hash something like this. Win. I want lose a lost. Right. So if I want to get the win message remember I said when you define a hash this special thing here is a symbol. Okay. So to access a symbol key I need to put the colon in front when I access it. Even though when I define the hash the colon is okay. It's a bit tricky thing about the syntax. Okay. So messages win will give me the win message. Okay. And messages lose it's going to give me the lose message. All right. So you guys understand this. So we're going to actually do this in our modify our game end a bit so that we can print the corresponding message lah. So instead of puts you in right. So follow along we need to pass in the messages hash to this function. So let me just hard coded here for now. Okay. And exit IRB if you were in IRB just now and let's try running this program lah. Okay. So you see now it replace we replaced it with the win message and lose message we define. Now so let's make it more interesting we want to customize the win a hash of hashes so you can an array of arrays you can also have a hash of hashes so how's it going to work? Okay. Instead of this array players array I'm going to make this a hash so can get rid of this curly braces right. Hash we need curly braces so for the key agency my I'm going to create another hash with the win and lose message I am a person of few words so I shall I just have a sad face and doggledog a sad face when I lose and doggledog when I win so let's say K you can feel this whatever you want you can put your own name whatever I'm just showing this example oh sorry okay so now I can actually iterate through my hash and I have to change to do a bit something a bit different okay so I have to say that because a hash array just has one item whereas a hash iterate through a way you just get one element each time whereas you iterate through a hash you're going to get the key and the value each iteration so you need to say that I want to assign the key to this variable and I want to assign the value to this variable so the value of this hash so that means let's say the first iteration I'm going to go to here player name the string string t and messages is going to be this hash okay so now I need to modify my row a die function so I need to pass in player name first okay let's rename it to make it nicer and now I pass I need to take in a messages argument also okay alright so I need to pass it in here and let me run the program okay so you can see that now I have a custom and lose message for each player alright do you think I should talk about OOP okay okay now I'm going to okay so I plan too much for this workshop but now I want to dive into a bit of the mini project the project that was supposed to end of this workshop so that you can get a taste of it there's actually source beer there was supposed to be another segment of the Ruby 101 but it's really a very big complex thing so if you're interested please go and read the notes now I want I want to work on this project where you're going to build a simple HTTP server anyone still need this? if not I'm going to show the slides okay everyone can I move on? scroll down okay I think most of you are more or less done let me move on okay so the next part of this workshop is going to be okay you guys was supposed to learn this as part of Ruby can go and google this if you're interested and read the notes I want to work on building a simple web server so like I said a server is basically just software that they also refer to the machine itself the hardware but it's also a software piece of software that what serves files right to your clients okay so to to understand how a server works and because you're going to be building web applications okay so like I said the web is based on a client server model right and the internet okay so the web is built on the internet the web is not the internet the internet is more things than just the web like for example there's email that's part of the internet but it's not the web the web is this whole system where you have web servers serving files to clients okay and that's how most people use internet these days but that is not all that is to the internet just to just to clarify the distinction so the internet the things that the thing that governs are the internet is basically a network of networks right right internet work okay so you have a lot of all these computers trying to talk to each other how do the computers know you know how do they communicate how are we going to govern this communication between computers so that's why we have something called protocols right it's a protocol it's a standard certain rules you have to follow so when your machines talk to each other over the internet they have to follow these rules so that they are intelligible to another computer right so the internet protocols is this whole suite which we call the TCP-IP suite so TCP stands for transmission control protocol IP stands for internet protocol so this TCP slash IP suite makes up the whole suite of protocols that govern called the internet as we know it today and traditionally we break up these protocols into different layers that means the architecture is such that different protocols tackle a different layer of the of networking so for example the first the very most the first layer is physical how your data is actually transmitted across your wires to ethernet cable or wifi that kind of thing that is the physical or link layer the most low level layer so this is just from let's say my computer wifi card to the access point that's the data is being transmitted over what we call the link and the physical layer so we have a bunch of protocols dealing with this first layer the second layer is internet work meaning so my computer and all your computers are connected to the access point in this NDC labs this is a local network we are all connected to the same local network but the internet is a network of networks so somehow this local network has to connect to another network out there so the second layer of protocols we call internet work protocols deal with how do computers within one network talk to a computer in a different network so if you go to work you have your own office network but you have to access the web server somehow on the internet so how does your computer connected to your office network know how to talk to a computer that's on a completely different network half way across the world right that's the internet work layer the third layer is called the transport layer so all this while we've been talking about one machine talking to another machine but it's not just about one machine talking to another machine on one machine you have many different processes you have your browser you have your email client you have many different processes trying to talk to other processes on other machines right so instead of just being machine to machine now we're talking about process to process okay so like your browser process it needs to fetch data from a server process on some machine and internet right and my single machine can run multiple processes okay so the transport layer is the layer that tries to figure out which process is trying to talk to which process so my internet work figure out okay this machine A is trying to talk to machine B but machine A is running processes 1, 2, 3 machine B is running processes 1, 2, 3 also how do I know which process process 1 on machine A wants to talk to on machine B okay so that's your transport layer and the final thing is the application layer so that is more like one layer above okay one layer above transport is more like dealing with how should the data be kind of like formatted when I send it across so actually something like HTTP that's in my application layer okay so there are examples ethernet wifi these are your data link layer protocols IP internet protocol is the you know IP address that's what the IP refers to right is the internet work layer protocol TCP transfer control protocol is on the transport layer and HTTP is on the application layer so these are not the only protocols that exist there are thousands of them okay these are just some of the few that you saw have a basic familiarity with when you talk about understanding the web and building web applications okay so what another concept I need you guys to understand at the transport layer we have this thing called sockets so let's say process one on machine A needs to talk to process one on machine B I have the IP address to distinguish machine A from machine B okay that's what IP addresses are for distinguishing two different machines identifying machines on different networks okay but how am I going to identify a specific process on the machine right that's where sockets come in so you have this thing called a port number right that tells it that hey this that identifies this particular process okay so what happens when you communicate between a client or server is that you open this you open this sockets to talk to each other okay and we're going to see how to do that in Ruby so to do that we have to learn how to use libraries so what are libraries libraries basically a bunch of code bunch of smart people wrote for you to use but you need to learn how to use them la so in there's generally two types of libraries in Ruby so first thing is something we call the standard library so this is the bunch of useful code that people have written that comes with a standard Ruby installation so you don't need to install some extra stuff to use it and there are other libraries that in Ruby we call them gems these are libraries that don't ship with your standard Ruby installation so you install them separately so something like rails is a jab it's not part of the standard library so to write our HTTP server we're going to use something in the Ruby standard library called socket so it's the same thing read the manual so Ruby has excellent documentation online see core STD so you can find a documentation of various core modules and standard library modules here so we're going to use a module called socket okay so you can see there's some explanation and then how to use the functions and the methods that they define in this library right what we're going to do is try to create a very simple HTTP server so they actually give an example the server might look like this first line you have to require the library because you need to in your program you're just writing in some file but Ruby needs to know that you want to use this code that is stored in another file on your computer stored in the standard library so you need to require that specific module from the standard library okay in this case the module is called socket right so this is what the first line does second line is to create a new server socket so that means basically this is saying that the server software has opened a socket on this port number 2000 and is waiting for clients to connect to it okay and then here we see our our good friend the loop so basically this server is just waiting for clients looping and waiting for clients to connect to it so at this point the server accepts the client connection and then the server sends this data back to the client so it sends the client hello sends the client time is whatever and then finally we close the socket connection okay so we're going to do something roughly like this so go to exercises simple server open the server.rb file this file is already there for you there's a lot of helpful comments already there to annotate what's going on okay so the first thing we need to do remember just now we said we need to require the module from the standard library okay so the first line is require socket okay by the way it can be double quotes it can be single quotes they're the same okay now we want to create the new server socket at a certain port number let's say 2000 okay how about 8 because it's a lucky number right now let me just output something so that we can see that our program is running if you run this in your terminal there's no output you don't have feedback that your program is running so let's say server is running on port 888 okay okay this is where the meet of our server is first we need to accept a client connection okay the server we're going to write now it's going to be again a hello world server so what this server is going to do it's going to wait for clients to connect to it then it's going to send hello world to the client right and then it's going to close the connection so very simple so first we need to accept the client connection sorry right and I want to read so when your client connects to the server right it's actually making a HGTP request and that request has some information okay and I want to get whatever information is stored in that request so I can do this by doing something like socket dot gets so this socket dot gets basically gets the first line of the HGTP request okay and I'm storing it in a variable called request so let me just put this on a terminal so you get to see what the request is so now we need to specify what's the response our server is going to give to the client so normally web servers normally the response is a HTML page for example but for now we're just going to return a normal string hello world just plain text data okay now finally remember the protocol that governs the client server communication is HGTP and HGTP specifies that when you send a request or response you must have certain metadata extra data other than just the response that you want to give so something called we have to have to send back to the client something called HGTP headers when the client makes HGTP request they also make a request with certain HGTP headers to specify let's say what kind of content it accepts that kind of thing server also needs to send back a set of HGTP headers for the client okay so the headers that we're going to have okay the first line this actually describes what protocol we're using so we're using HGTP version 1.1 the next thing is a number called the status code so I'm sure you're familiar when you go to a web page and you see 404 not found the 404 is actually the HGTP status code most of the time or you'll get a status code of 200 which means everything is fine so 200 here means everything okay right 200 okay and we need to according to the HGTP specification we need to end our one header line with this funny slash R slash N characters okay so slash R is carriage what we call carriage return slash N is new line okay the terminology for this is back in the days there was like a carriage return and a new line keys okay but basically HBRI says we should always put these two characters at end so I'm going to do that okay now I'm going to it's actually one whole long string so I'm going to concatenate the strings on separate lines the second header I'm going to have is content type header so this is saying that this is a server telling a client hey what I'm sending text HTML right but now you're going to just sending plain text so the content type should be text slash plain okay so again we need the slash R slash N the next header we're going to do is content length so this tells the client how long the byte size of my content so we're just going to do something like response so response is a string this dot byte size tells basically trying to get the number of bytes that this string takes up when you store this string on computer how many bytes does it take up and finally we tell the client that we're going to close this connection okay and then the specification says we need to put a new line after the after all our headers so we're going to print another slash R slash N and finally after we print our headers we print our new line then we send actual response right so we socket dot print response remember response here is hello world okay and finally we need to close the connection okay so now your server should be up and running so you can run this ya okay so remember the first thing require the socket library okay so I will just run through again require the socket library open your server socket on the port number AAA you put something so you can see your program running on your terminal okay then in your loop you must accept a client connection and then you get so your client is making HDB request your server right so you get the first line of that client request and then you because the response you can return whatever you want it's just that the HDB headers they have to be ended with slash R slash N as specified by the standard HDB standard okay so the response by itself is not part of the data oh ya oh so you're talking about this yes sorry yes we need this I'm sorry I thought you were talking about the hello ah ya okay so we print the HDB headers then we print a new line and then we print our actual response okay so let's try running this and remember it's actually an infinite loop we're not breaking out of the loop so your server is going to keep running so if you want to so it should be simple server simple HDB server right so that my server.rb is down there okay everyone is in the correct directory then we can run rubieserver.rb okay so our server is running on this port how do we test how do we get how do we test how do we get a client to connect to this server la so I need you to open because this server program is taking up this terminal I need you to open okay I'm just going to put it here and remember the good friend we saw just now to make HDTP request okay never mind but remember we saw the command line program called curl curl is used to make HDTP request remember you said curl.google.com we made a HDTP request to Google.com so now we're going to use curl to make a HDTP request to our server our own server okay so curl localhost so localhost is a special host name to say a server to identify a server running on your own on this local machine okay so port number 8 can you all see that you just made a HDTP request to your own server and your server return hello world and your client got the hello world back and you see your server we also print out the request line remember we print out we did this put request here so this is the request line that the client is sending okay so congratulations you wrote your first web server didn't think you can write your own web server before this i thought it's some very fensifu thing smart people build actually the basic thing is very simple okay can everyone get your client and your server running just give you guys about a minute or two so just so you know when you guys learn rails right and you have to run server command to start up your real server it's basically just htpserver something like this okay so for those who have got your client and server up and running i want you guys to try just a little bit instead of just doing curl localhost you can pass curl option verbose verbose meaning you print out extra stuff but what it's actually doing behind the scenes so you see curl print out all this stuff say it's trying to connect to this port trying to connect to localhost and this is the request htp request headers that is actually sending to your server okay and this is the htp headers that your server send back to your to curl and finally this is the response that your server send back okay anyone has any troubles can i move on now before which one the terminal so run your server to run your server do rubieserver.rb to use to make a htp request to your server curl localhost the port number and you should get the hallow world back ya you need two terminals because your server is going to be running so you need to run your client in another terminal okay let me just we only about ten minutes left so let me just move on to the bit of the next part so normally web servers they don't just serve plain text they don't just serve a hallow world and that's it web servers they will serve html file so we are going to try to write our server to serve a html file so if you see already in your directory you should have this html file called index.html it's a very simple file you can go and based on what you learn last week you can go and modify and whatever printify it omg sorry so let me just close a bit of all the windows okay so in in our simple index.html file for you already so I'm going to try to modify our server so that it serves this html file instead of just returning hallow world okay so the first thing we need to do is we need to read in the file in our program that's the first thing so to read in the file we actually need to know where the file is so we need to get what we call the file path right so under your put request part okay we're going to get get the file path right so path equals file bigf.join so file is another module in ruby remember just now we had random.ran so file like random is another module and it has certain functions that you can use so the file module exposes a function called join file.join file. directory name durname underscore underscore file so this file.durname underscore underscore file underscore underscore means that get me the directory name of this current file okay so what's the directory name it will be workspace exercises simple http server okay that's the directory name of that's the directory that this current file is located in so file.durname will get directory of where this current file is and then we need to say get the index html file right so file.join the directory name with the name of the actual file so the name of the file is index.html it is in this particular directory we get the directory name and join it with index.html to get the full path to the file okay so next we're going to change our response so we can get rid of this now we need to open the file okay so we're going to do file.open path so that means this will open the file read the open the file at this path right and we do something with the file okay and every do must be match with an so let's put an down here after socket.print response okay and then everything in here lah so we can do something with a file in this between the do and end here okay so what I'm going to do now we deleted our response variable so now I need to tell the client what's the size of my file so we have the file stored in the file variable over here okay so we can get the file size from file.size alright and instead of socket.print response we're going to do something we're going to stream the file over the socket it means we copy the contents of the file and send it over this socket to the client io input output right so this is the module that deals with all the input output functions so input output copy the stream copy my contents of my file stream it across the socket okay and with that you should be able to serve your HTML file right so if we try this again sorry are we sending the file 29 it's it's not you really line by lines you just imagine you reading this file in it comes in as a stream of bytes and you're sending these stream of bytes over the connection so this opens the file in your program so that you can refer to this what we call a file object right but we haven't actually read we haven't actually like read the file yet yeah we just have a a reference to the file you can think of it we haven't no no we just have a reference to the file we're not reading the file yet okay so this is where we actually get the contents of the file we copy it and we stream it over the socket over the connection to the client alright so with this we can try and run our server again so if your server is already running control C to quit and re-run it and now we try to curl hey, we got a HTML file okay so this is how you okay this is how you serve files from your server right so remember the first thing we did we got the path to the file then after that we open the file and we get a reference to the file object here so we're not reading from the file yet but afterwards we want to instead of so the file is going to be our response body so we copy the contents of the file and send it over the socket alright can you go to the last part that after we've done with this after we've done copy stream then how do we run this ah okay so to run it remember you had two terminals ah kill your server process so stop your server control C and then rubieserver.rb again and basically just do the same thing just now you curl localhost AA8 do it again okay so remember just now before we added the file stuff we got hello world now we actually get our HTML file right sorry ya but because you're using the cloud 9 machine right if you're actually running your server on your own local machine you can type it in your browser and you work so for example I can show you demonstrate on my own machine oh sorry so now I'm running a server on my local machine I can actually make a request from my browser just type in localhost AA8 okay but because you're running it on a cloud 9 machine you can't actually have a browser in your cloud 9 machine so it's a bit of a pity but just realise that your browser is also another client just like the curl command just making a htb client request to your server okay okay so we're kind of like time's up so at least you've managed to build a simple server to serve file so you can get so now you've actually got a taste of how actually those web servers actually run la actually these programs they're just looping forever and they're reading files they have some files stored on the disk they're reading these files and sending over to you alright so before we just end of let me just talk a little bit more about the different types of content that your server can serve so you can get a better understanding when you start doing rails because when you do rails you're not just going to serve simple HTML files anymore okay so there's a difference between what we call static content and dynamic content so imagine right your website has a product catalogue and there are thousands of products okay if you serve static content if you want and you want your users to be able to see a page with a product image and a name of the product right but you have a thousands of products if you just serve what we call static content you're going to have to serve you're going to have to store thousands of HTML pages with a name and image for each specific product so like something you have here let's say I have three products right in my server my computer I save I have three different HTML files with the corresponding the name and with the image and then when the client makes a request to my server I just serve these files directly I'm not changing these files anyway this is what we call static content I just serve the files directly but what you will see in rails is that you're actually going to have to deal with dynamic content so actually what the server stores is something like a HTML a template it doesn't actually store the actual product name and the actual product image for all the products it just stores this kind of template file which says hey in this H1 tag there should be the product name and then in this image tag it should be the product image and then your server processes these templates let's say it fetches the data from the database like what is the name for this particular product at this particular URL let's say you go to product slash camera so the server sees this URL I should fetch the camera image and then it processes this template and replaces the name and the image with the actual name and image replaces this stuff in your template with the actual name and image so now it's dynamic your server is actually processing that template file before serving it to the client so this way if you have thousands of products you don't have to store thousands of different HTML files you store one template the general layout is the same just display the product name and the image but you process that template and differently each time the client request a different product okay so when you guys start doing Rails you actually be dealing with HTML templates instead of just playing HTML okay ya and the rest is I unfortunately I don't have time to cover and actually rest is an acronym actually stands for something I wanted to cover but unfortunately I don't have time to cover so when you do Rails yes you will see it so thank you very much I hope you guys learn something we spoke today see this guy is very happy okay ya so again like I said the notes I provide for you is actually very comprehensive I may have made some typos here and there if you read them you may get a little confused but if you have the time and energy and you want to learn really please go and look at read through the notes and we actually go it covers a lot of this stuff in more detail okay photo ya Joel wants to take a photo