background preloader

Delightful lessons for dedicated programmers

Delightful lessons for dedicated programmers
Related:  Ruby Tuts:ruby

RubyIdioms - tokland - Ruby conventions and idioms - My personal repository for code and documentation This document shows some Ruby idioms, conventions and patterns, most of them accepted by the community, some of them personal. This is a work in progress, so feel free to email me or add a comment if you have any suggestion (tokland AT gmail.com). General formatting Choose a formatting style with readability as first goal, and stick to it. At the same time, break the rules when necessary ("a foolish consistency is the hobgoblin of little minds"). Novice programmers tend to overlook the importance of how the code looks, but as it's been extensively remarked, source code is meant for other people to read and understand, and only incidentally for machines to execute. Indentation Use 2 spaces, no tabs, and never mix spaces and tabs. Whitespaces Put an space after a comma: def method(x, y) [x, y]end No spaces after or before a (, [ or { (when it's a hash): array = [1, 2, 3] hash = {:a => 1, :b => 2} def method(arg1, arg2, arg3) # method bodyend But use spaces to let the blocks breathe: Naming Blank lines

Ruby QuickRef Table of Contents Language General Tips These are tips I’ve given over and over and over and over… Use 2 space indent, no tabs. See for more. General Syntax Rules Comments start with a pound/sharp (#) character and go to EOL. Reserved Words alias and BEGIN begin break case class def defined? Types Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes. Numbers 1231_234123.451.2e-30xffff 0b01011 0377 ? Strings In all of the %() cases below, you may use any matching characters or any single character for delimiters. %[], %!! 'no interpolation'"#{interpolation}, and backslashes\n"%q(no interpolation)%Q(interpolation and backslashes)%(interpolation and backslashes)`echo command interpretation with interpolation and backslashes`%x(echo command interpretation with interpolation and backslashes) Backslashes: Here Docs: Encodings: Waaaay too much to cover here. Symbols Internalized String. Ranges 1..101...10'a'..' Regexen "r"

Learn Web Development with the Ruby on Rails Tutorial 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

Best tips and practices from Ruby experts As you work with Ruby a lot and develop applications, you will start using more and more components, like fast RESTful APIs or any other frameworks. In that situation you need to think about separating responsibilities and business logic. For example, if you are using Ruby on Rails, you should start using ActiveRecord and keep all rake tasks outside the Rails. Let’s now imagine you want to create a new restful API using Grape, but you don’t want to end up replicating models. What can you do? One way to do this is to move your models and core tasks to a Gem so it can be decoupled and used anywhere. You can use Bundler to initiate a new Gem with the command: bundle gem core. ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── core.gemspec └── lib ├── core │ └── version.rb └── core.rb These files contain most of the business logic, and we need to move them to a separate Gem so they can be used outside Rails applications: That’s it.

bbatsov/ruby-style-guide · GitHub Ruby/2013-01-22-book-problems.md at master · Duke-PL-Course/Ruby Ruby Programming Ruby is an interpreted, object-oriented programming language. Its creator, Yukihiro Matsumoto, a.k.a “Matz,” released it to the public in 1995. Its history is covered here. Its many features are listed here. The book is currently broken down into several sections and is intended to be read sequentially. Getting started will show how to install and get started with Ruby in your environment. Table of Contents[edit] Getting started[edit] Overview Installing Ruby Ruby editors Notation conventions Interactive Ruby Mailing List FAQ Basic Ruby[edit] Hello world Strings Alternate quotes Here documents Encoding Introduction to objects Ruby basics Data types — numbers, strings, hashes and arrays Writing methods Classes and objects Exceptions Ruby Semantic reference[edit] See also some rdoc documentation on the various keywords. Built in Classes[edit] This is a list of classes that are available to you by default in Ruby. Available Standard Library Modules[edit] Other Libraries[edit] Database Interface Modules Unit testing

Ryan Angilly — Dynamically adding class methods in Ruby Even though there is technically no such thing as a class method in Ruby, I’m going to call them that for the sake of clarity. When I say class method, I mean something like this: class A class << self def yo "wassup" end end end A.yo #=> "wassup" Every now and then, you may want to dynamically generate these things. Thanks to instance_eval and define_method, dynamically defining methods in Ruby is trivial, but these operate in what someone coming from Java or C++ would call the instance context. Take the following example: class A class << self def create_method(name) define_method(name) { puts "Nice! The define_method creates a method that is only accessible on instances of the class, not a class method. class A class << self def create_method(name) self.class.instance_eval do define_method(name) { puts "Nice! Great success! class A class << self def create_method(name) self.class.instance_eval do define_method(name) { puts "Nice! What? "1".class.mine # prints out "Nice! Yeah. Simple.

bbatsov/ruby-style-guide Ruby Quiz - MUD Client (#45) MUD Client (#45) Sy has been searching for a Ruby scriptable MUD client via Ruby Talk and so far, there hasn't been many helpful answers posted. Let's generate some. This week's Ruby Quiz is to create a basic MUD client that must be scriptable in the Ruby programming language. That's pretty vague, so let me see if I can answer the questions I'm sure at least some of you still have. What is a MUD? MUD stands for Multi-User Dungeon/Dimension, depending on who you ask. > lookSacred Grove You are standing in the legendary resting place of The Dagger in The Stone.Many have tried to free the mystical blade before, but none were worthy. You can see the Castle of Evil to the west. What's here: The Dagger in The Stone >look daggerThe all-powerful blade begs to be stolen! >get daggerYou take the dagger. >equip daggerYou are now the most dangerous warrior in the kingdom! >westThe Gates of Castle of Evil A very big, very black, very evil castle. What's here: Grog, Castle Guardian The MUD Connector The MUD FAQ

The Bastards Book of Ruby Technical notes, my online memory: Ruby Time and DateTime: parse a string, compute the difference, simple right? Time in ruby is a confusing maze of suckiness. The Ruby Time class doesn't include a method to convert a string into a Time object (like the C standard 'strptime'), despite having a Time.strftime function. What the? It turns out that Time.parse and Time.strptime do exist, but confusingly they aren't in the core class, but in a separate library you have to require that overrides the class. WTF? irb(main):001:0> Time.parse NoMethodError: undefined method `parse' for Time:Class from (irb):1 irb(main):002:0> require 'time' => true irb(main):003:0> Time.parse ArgumentError: wrong number of arguments (0 for 1) from (irb):3:in `parse' from (irb):3 So it seems that the best way to read a time from a file and compare it to the current time is the following, which gives you an answer in seconds: irb(main):010:0> a=Time.parse('2011-01-10 12:34:00 UTC') => Mon Jan 10 12:34:00 UTC 2011 irb(main):012:0> b=Time.now.utc => Fri Dec 09 20:47:32 UTC 2011 irb(main):013:0> b-a => 28800812.788154

Related: