ebook img

ILove Ruby PDF

2015·2.3 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview ILove Ruby

I Love Ruby 2015 Beta 1 I Love Ruby 2015 Beta Contents I love Ruby....................................................4 Loops...........................................................40 Copyright.......................................................5 downto....................................................41 I....................................................................6 times.......................................................41 Kannan Doss.................................................6 upto.........................................................42 Iain McNulty.................................................7 step..........................................................43 Getting this book...........................................8 while.......................................................44 Getting example programs.......................8 until.........................................................45 The books source......................................8 break.......................................................46 Installing Ruby..............................................9 next.........................................................46 Installing Ruby on Debian flavor redo.........................................................47 GNU/Linux...............................................9 loop.........................................................47 Installing Ruby Natively (Debian flavor Arrays..........................................................50 Gnu Linux).............................................10 More on Array........................................53 Installing on Windows and Mac.............10 Set operations.........................................55 Installing IDE.........................................10 Hashes and Symbols...................................57 Online Resources.........................................11 Default values in Hash...........................58 Ruby Website..........................................11 Looping hashes.......................................58 Ruby Forum............................................11 More way of hash creation.....................59 Twitter.....................................................11 Using symbols........................................60 Getting Started............................................13 Ranges.........................................................64 Interactive Ruby.....................................13 Ranges used in case .. when...................65 Doing some Math...................................13 Checking Intervals..................................66 Space doesn't matter..........................16 Using triple dots …................................67 Decimals............................................16 Functions.....................................................68 Variables.................................................16 Argument Passing...................................69 Naming Convention...........................18 Default Argument...................................70 The underscore – a special variable...18 Returning Values....................................71 Constants................................................19 Recursive function..................................72 Strings.....................................................20 Variable number of arguments................74 String Functions.................................21 Hashes to functions................................75 Escape sequence................................22 Variable Scope.............................................77 Using Text Editor...................................23 Global Variables.....................................79 Printing Something.................................24 Classes & Objects.......................................83 Getting Input...........................................25 Creating a Square...................................83 Comments...............................................26 Functions in Class..................................84 Comparison and Logic................................28 Initializers or Constructors.....................85 Logical Operators...................................28 Private Methods......................................87 true != “true”......................................30 Class variables and methods...................88 if..............................................................30 Inheritance..............................................91 if else......................................................30 Overriding Methods...............................92 elsif.........................................................31 The super function..................................95 unless......................................................32 Extending class.......................................96 unless else...............................................33 Reflection...............................................97 case when...............................................34 Encapsulation.......................................101 ? :............................................................36 Polymorphism......................................103 Assigning logic statement to variables...37 Class Constants.....................................104 2 I Love Ruby 2015 Beta Struct and OpenStruct...............................107 Deadlocks.............................................170 Rdoc...........................................................111 Creating multiple threads.....................172 Reading Ruby Documentation..............111 Thread Exception.................................175 Creating Documentation.......................112 Thread Class Methods..........................177 Modules and Mixins..................................115 Thread Instance Methods.....................178 Calling functions without include.........117 Exception Handling...................................180 Classes in modules...............................121 Exception and Threads.........................183 Mixins...................................................122 Raising Exceptions...............................184 Shebang.....................................................126 Regular Expressions..................................186 Date and Time...........................................127 Creating a empty regexp.......................186 Days between two days........................129 Detecting Patterns.................................186 How many days have you lived?..........130 Things to remember..............................187 Files...........................................................133 The dot..................................................188 Storing output into files........................133 Character classes..................................188 Taking file as input...............................133 Scanning...............................................190 File copy – a kind of.............................134 Captures................................................192 Displaying a file...................................135 MatchData class...................................195 Reading file line by line.......................136 Anchors and Assertions........................196 Open and new – the difference.............137 Anchors............................................196 Defining our own line endings.............138 Assertions........................................197 Reading byte by byte............................139 Ignoring Cases......................................199 Reading single character at a time........140 Ignore Spaces.......................................199 Renaming files......................................141 Dynamic Regexp..................................200 Finding out position in a file................141 Gems.........................................................202 Writing into files...................................143 Searching a gem...................................202 Appending content into files................145 Installing gem.......................................203 Storing objects into files.......................146 Viewing Documentation.......................204 Pstore...............................................146 Using gem.............................................204 YAML..............................................150 The Gemfile..........................................205 Proc, Lambdas and Blocks........................153 Creating a gem......................................207 Passing parameters...............................154 Uninstalling a Gem...............................209 Passing Proc to methods.......................154 Meta Programming....................................210 Returning Proc from function...............155 Send......................................................210 Proc and Arrays....................................156 Method Missing....................................211 Lambda.................................................157 Define Method......................................214 Passing Argument to Lambda...............157 Final Word.................................................216 Proc and Lambdas with Functions.......158 For 2015 Edition...................................216 The second difference...........................159 For 2014 Edition...................................216 Lambda and Arrays..............................160 For 2013 Edition...................................216 Blocks and Functions...........................160 For 2012 Edition...................................216 Multi Threading........................................163 For 2010 Edition...................................217 Scope of thread variables.....................166 Underscore................................................219 Thread Exclusion..................................167 An important Math Discovery..................220 3 I Love Ruby 2015 Beta I love Ruby Ruby is an easy to learn programming language, it was invented by a guy named Matz in Japan. Ruby is a free1 software and can be used by any one for zero cost. Ruby's popularity was initially confined to Japan, later it slowly trickled out to rest of the world. Things changed with the emergence of Ruby on Rails2 which is a popular web-development framework written with Ruby. I was thrilled when I started to program in Ruby. One of my first application was a student ranking software for my mom who was a teacher. I was able to write the console based application in just 32 lines. This opened my eyes and made me realize the power of Ruby. The language was simple, easy to learn and nearly perfect. Currently I am an professional Ruby on Rails programmer working in TechArtUs3. This book is written for GNU/Linux (Debian distro) users, thats because I think GNU/Linux will conquer desktops of programmers in near future. Almost all who have Debian GNU/Linux based distro should feel at home while trying to learn Ruby using this book. If you are using other operating systems like Solaris, OSX or Windows please contact your Operating System help channels to learn how to install or get started with Ruby. You can also visit http://ruby-lang.org to learn how get started with Ruby. 1 Free here does not mean zero cost. Visit http://fsf.org to know more 2 http://rubyonrails.org 3 http://techartus.com 4 I Love Ruby 2015 Beta Copyright Copyright (c) 2009 - 2013 Bigbang to Infinite, Karthikeyan A K Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found in http://www.gnu.org/copyleft/fdl.html 5 I Love Ruby 2015 Beta I Hi, I am Karthikeyan A K, the author of this book. I came across Ruby and found that the language was dazzling. I thought why not write a book for self study and so was created this book. Currently I am an web developer, I use Ruby on Rails due to its great innovative features. You can contact me at [email protected] or tweet to @karthik_ak This book is no match to other Ruby best sellers, this is just an attempt to have a personalized Ruby study material. Kannan Doss I am very much grateful Kannan Doss who once worked in Mind As Lab as a apprentice web developer and was also a Ruby on Rails geek. I thank him for putting sincere efforts to improve the quality of this book. He has proof read the entire book, spotted mistakes he could find, cataloged them and corrected them. Without him this book wont be as good quality as it is now. I am not saying that this book is superb, but his efforts truly played a decisive role to make this book what it is. You can contact Kannan at [email protected] 6 I Love Ruby 2015 Beta Iain McNulty I don't know who Iain is, he proof read some of the pages in this book very well. He is an Irishman and possibly loves freedom of information. I am very grateful that he has contributed to this book and hope he will contribute more. This is his facebook page https://www.facebook.com/iain.mcnulty.3 7 I Love Ruby 2015 Beta Getting this book This book is hosted at http://goo.gl/1qrMUN . All announcements about this book are available on facebook in this URL: https://www.facebook.com/pages/I-love-ruby/172269549451705 and you do not need to be a member of facebook.com to access this page. Getting example programs The example programs in this book are hosted in Github in the following URL: https://github.com/mindaslab/ilrx . I realize many who read this book are starters, who have just plunged into programming. Hence you can download all examples by clicking this link: https://github.com/mindaslab/ilrx/zipball/master The books source Get the book's source here https://www.dropbox.com/s/oj7ky96iftwh4xv/I%20Love%20Ruby %202015.odt 8 I Love Ruby 2015 Beta Installing Ruby Installing Ruby on Debian flavor GNU/Linux You need to install something called RVM (ruby version manager) which will manage multiple ruby versions. Why? It's because Ruby's version changes so fast.. Before you had 1.8, now 1.9 and soon Ruby 2 will be out. Apart from just using Ruby alone, you will also use it for other stuff like web development with packages such as Sinatra and Ruby on RailsTM . You might need to change from one version to other without uninstalling and reinstalling ruby again and again. RVM manages this for you. With simple commands we can switch between Ruby versions easily. Installing RVM : OK, to install RVM, you need to have curl (a program that can download things). To get curl, just type $ sudo apt­get install curl Now install RVM using the following command $ \curl ­sSL https://get.rvm.io | bash ­s stable ­­ruby Once done give these commands into terminal. These will tell Ubuntu GNU / Linux where to find the ruby interpreter. $ echo '[[ ­s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*' >> ~/.bashrc $ source ~/.bashrc Once this is done, you may need to restart your computer. Open the terminal and type the following: $ ruby ­v It will spit an output something like this: ruby 2.1.0p0 (2013­12­25 revision 44422) [x86_64­linux]  Then all is OK! 9 I Love Ruby 2015 Beta Installing Ruby Natively (Debian flavor Gnu Linux) rvm is a version manager fr ruby. You can install many versions of Ruby interpreter like ruby 1.8.7 , ruby 1.9.3, ruby 2.0.0 and so on, on a same system and switch between them seamlessly. For more information visit http://rvm.io But some wish to install Ruby supported natively on their Linux platform. For that all you need to do is type the following thing in your terminal (leave out the initial $ sign, as it symbolizes the prompt $ sudo apt­get install ruby The above thing will install Ruby on your system. Installing on Windows and Mac I don't use Windows or Mac, so please ask respective help channels on how to install Ruby in them. Installing IDE You need a good IDE (Integrated development environment) to get started with Ruby. I recommend simple and light weight IDE Geany4. To install the IDE in Ubuntu just type (without that $): $ sudo apt­get install geany If the system asks for administrator password, provide it. Click the dash button and type in geany. Click on the geany icon, you will get it :-) 4 Windows users goto http://www.geany.org/ to download the installer 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.