background preloader

Ruby

Facebook Twitter

Hackety Hack! Ruby Tutorial. Ruby Basic Tutorial. Troubleshooters.Com, Code Corner and Ruby Revival Present Ruby Basic Tutorial Copyright (C) 2005 by Steve Litt Note: All materials in Ruby Revival are provided AS IS. By reading the materials in Ruby Revival you are agreeing to assume all risks involved in the use of the materials, and you are agreeing to absolve the authors, owners, and anyone else involved with Python Patrol of any responsibility for the outcome of any use of these materials, even in the case of errors and/or omissions in the materials.

If you do not agree to this, you must not read these materials. To the 99.9% of you honest readers who take responsibility for your own actions, I'm truly sorry it is necessary to subject all readers to the above disclaimer. CONTENTS This is a Ruby tutorial for one not knowing Ruby. Therefore, we use many constructs and styles that, while familiar to programmers and intuitive to beginners, are not optimal for Ruby. This is the simplest possible Ruby program, hello.rb. While Loops Retry x x. Programming Ruby: The Pragmatic Programmer's Guide.

Ruby in Twenty Minutes. What if we want to say “Hello” a lot without getting our fingers all tired? We need to define a method! Irb(main):010:0> def hiirb(main):011:1> puts "Hello World! "irb(main):012:1> end=> :hi The code def hi starts the definition of the method. It tells Ruby that we’re defining a method, that its name is hi. The Brief, Repetitive Lives of a Method Now let’s try running that method a few times: irb(main):013:0> hiHello World! Well, that was easy. What if we want to say hello to one person, and not the whole world? Irb(main):015:0> def hi(name)irb(main):016:1> puts "Hello #{name}! " So it works… but let’s take a second to see what’s going on here.

Holding Spots in a String What’s the #{name} bit? Irb(main):019:0> def hi(name = "World")irb(main):020:1> puts "Hello #{name.capitalize}! " A couple of other tricks to spot here. Evolving Into a Greeter What if we want a real greeter around, one that remembers your name and welcomes you and treats you always with respect. The new keyword here is class. Ruby, Ruby on Rails, and _why: The disappearance of one of the world’s most beloved computer programmers. Illustration by Charlie Powell. In March 2009, Golan Levin, the director of Carnegie Mellon University’s interdisciplinary STUDIO for Creative Inquiry, invited an enigmatic and famed computer programmer known to the virtual world only as “Why the Lucky Stiff” or “_why”—no, not a typo—to speak at a CMU conference called Art && Code—also not a typo—an event where artsy nerds and nerdy artists gather to talk shop.

_why came to Pittsburgh and presented his latest project to a room full of a student programmers and artists. He was scruffily handsome, seemingly in his early- to mid-30s, with shaggy brown hair falling in his eyes and a constant half-smile. He looked like a member of an indie band—he actually was in an indie band—or the leader of an experimental improv troupe.

At this symposium, he wore a pair of oversize sunglasses and a tidy sports coat with a red pocket square, a silly riff on a stuffy professor’s outfit. Why’s Poignant Guide to Ruby Related Story: Where's _why? Why? That’s it. Ruby on Rails Tutorial: Learn Rails by Example book and screencasts by Michael Hartl. Michael Hartl Contents Foreword My former company (CD Baby) was one of the first to loudly switch to Ruby on Rails, and then even more loudly switch back to PHP (Google me to read about the drama). This book by Michael Hartl came so highly recommended that I had to try it, and the Ruby on Rails Tutorial is what I used to switch back to Rails again. Though I’ve worked my way through many Rails books, this is the one that finally made me “get” it. The linear narrative is such a great format. Enjoy! Derek Sivers (sivers.org) Founder, CD Baby Acknowledgments The Ruby on Rails Tutorial owes a lot to my previous Rails book, RailsSpace, and hence to my coauthor Aurelius Prochazka.

I’d like to acknowledge a long list of Rubyists who have taught and inspired me over the years: David Heinemeier Hansson, Yehuda Katz, Carl Lerche, Jeremy Kemper, Xavier Noria, Ryan Bates, Geoffrey Grosenbach, Peter Cooper, Matt Aimonetti, Gregg Pollack, Wayne E. About the author Copyright and license 1.1 Introduction.