Ruby Basic Tutorial - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Ruby and Ruby on Rails (https://www.supportforums.net/forumdisplay.php?fid=55) +---- Thread: Ruby Basic Tutorial (/showthread.php?tid=5262) Pages:
1
2
|
Ruby Basic Tutorial - DarkByte - 03-13-2010 Ruby Basic Tutorial Hello World This is the simplest possible Ruby program, hello.rb. As you'd expect, it prints "Hello World" on the screen. Be sure to set it executable. Code: #!/usr/bin/ruby Although this program works as expected, it goes against the philosophy of Ruby because it's not object oriented. But as a proof of concept that Ruby's working on your computer, it's just fine. Besides print, there's also a puts keyword. The difference is that puts automatically inserts a newline at the end of the string being printed, whereas print does not. In other words, puts is more convenient, but print is necessary if separate statements print to the same line. Througout this tutorial we'll use both print and puts. Loops Let's count to 10... Code: #!/usr/bin/ruby The elipses (...) indicate the range through which to loop. The for is terminated by an end. You don't need braces for a loop. Whew! The following is the output: Code: [slitt@mydesk slitt]$ ./loop.rb Now lets list the presidents backwards by calculating the array's subscript as the array's length minus the counter, minus one. Ugly, but it gets the job done: Code: #!/usr/bin/ruby The preceding program produces the following output: Code: [slitt@mydesk slitt]$ ./hello.rb Ruby has a much nicer way of iterating backwards through a list: Negative subscripts. The following iterates backward through the array, using the fact that array[-1] is the last item, array[-2] is the second to last, etc: Code: #!/usr/bin/ruby If you're familiar with C, Pascal or Perl, you're probably dissappointed you couldn't just use presidents.length...0. Backwards iteration doesn't work in Ruby -- it must iterate up. Iterators and Blocks Another way to loop through an array is to use an iterator (in red in the following code) and a block (in blue in the following code: Code: #!/usr/bin/ruby In the preceding code, the block argument (prez) contains the current array element, and everything else until the closing brace contains code to operate on the block argument. The block argument is always enclosed in vertical lines (pipe symbols). The following is the output of the preceding code: Code: [slitt@mydesk slitt]$ ./hello.rb The block needn't be on one line: Code: #!/usr/bin/ruby As shown in the previous examples, you can define the block by enclosing it in curly braces. You can also define it by enclosing it in a do and an end, where the do replaces the opening brace, and the end replaces the closing brace: Code: #!/usr/bin/ruby Personally, I greatly prefer the do/end syntax for multiline blocks, because as a Perl/C/C++ guy I have a very different perception of braces than their limited use in Ruby, and also because of all the brace placement religious wars I've endured (I'm a Whitesmith type guy myself). However, on short single line blocks, using the braces saves valuable line space. From what I understand, the methods are interchangeable in features and performance, with one small exception... Speaking of performance, if you declare the block argument outside the block (in other words, make it a local variable), performance improves because Ruby needn't recreate a variable every iteration. HOWEVER, the loop messes with the value of the variable, so it's best to use a specific variable for that purpose, and do not use it for other purposes within the subroutine. Here's an example of using a local variable as a block argument: Code: #!/usr/bin/ruby If you use a local variable for a block argument, do so only in loops with huge numbers of iterations, and use only variables that are specifically intended to serve as block arguuments and nothing else. I pulled this crap from here , so don't flame! RE: Ruby Basic Tutorial - uber1337 - 03-13-2010 Great tutorial, thanks for the share. No need to flame, you can pull it from other sites, just be sure to cite your sources. If you don't give credit where it is due you will find your stay on this forum to be relatively short. RE: Ruby Basic Tutorial - DarkByte - 03-13-2010 What can I say , I'm happy that you didn't flame. RE: Ruby Basic Tutorial - Wolskie - 03-13-2010 Very nice, I understood all of it clearly. RE: Ruby Basic Tutorial - Awesome - 04-06-2010 Thanks! It cleared up a few things for me. Good tutorial dude, keep it up RE: Ruby Basic Tutorial - Kewlz - 04-07-2010 so in every program We should start With Code: #!/usr/bin/ruby hmm? RE: Ruby Basic Tutorial - uber1337 - 04-07-2010 (04-07-2010, 09:36 AM)Mr.Kewl Wrote: so in every program We should If ruby is anything like python than no, your right, you should use: Code: #!/usr/bin/env ruby RE: Ruby Basic Tutorial - Wolskie - 04-07-2010 (04-07-2010, 05:03 PM)uber1337 Wrote: <SNIP> Yes that right RE: Ruby Basic Tutorial - Unspoken - 04-22-2010 Great TUT 5/5 Great Job RE: Ruby Basic Tutorial - JesusOfSuburbia - 04-26-2010 Great TuT dude.Very usefull. Well done |