background preloader

Ruby QuickRef

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"

Ruby on Rails Guides Programmation Ruby/Types standards Un livre de Wikilivres. << Retour au sommaire Nous allons voir ici tous les types que nous pouvons considérer comme "standards", dans le sens où nous les retrouvons dans la plupart des langages, et que ceux-ci sont directement intégrés à l'interpréteur (built-in). Néanmoins il ne faut pas perdre de vue qu'il s'agit en réalité d'objets. Pour rappel les méthodes dont le nom se termine par ! Chaîne de caractères[modifier | modifier le wikicode] En ruby les chaînes de caractères sont représentées par l'objet "String". Créer une chaîne de caractères[modifier | modifier le wikicode] En ruby il existe une multitude de manière de créer une chaîne de caractères, par exemple en créant une instance de l'objet String : maChaine = String.new("Une chaîne de caractères") Mais le moyen le plus courant de créer une chaîne est de la placer entre simple quote (') ou entre double quote ("). Dans une chaîne de caractère \ à une valeur particulière, elle permet de placer un caractères d'échappements. %q! "Ho!

Ruby From Other Languages When you first look at some Ruby code, it will likely remind you of other programming languages you’ve used. This is on purpose. Much of the syntax is familiar to users of Perl, Python, and Java (among other languages), so if you’ve used those, learning Ruby will be a piece of cake. This document contains two major sections. The first attempts to be a rapid-fire summary of what you can expect to see when going from language X to Ruby. The second section tackles the major language features and how they might compare to what you’re already familiar with. What to Expect: Language X to Ruby Important Language Features And Some Gotchas Here are some pointers and hints on major Ruby features you’ll see while learning Ruby. Iteration Two Ruby features that are a bit unlike what you may have seen before, and which take some getting used to, are “blocks” and iterators. some_list.each do |this_item| # We're inside the block. # deal with this_item.end Everything has a value Everything is an Object

Rubular: a Ruby regular expression editor and tester Getting Started with Nokogiri | Engine Yard Ruby on Rails Blog We're decided to mix up the Engine Yard blog a little and invite some community members to contribute guest posts. This one (our first!) is from [Aaron Patterson]( -- a long-time member of the Ruby community, and the creator of Nokogiri. He hacks with the developers of Seattle.rb, and travels the world to speak about Nokogiri and other Ruby topics at industry conferences and events. Nokogiri is a library for dealing with XML and HTML documents. Getting Nokogiri installedBasic document parsingBasic data extraction Hopefully by the end of this article you will also be able to use and enjoy Nokogiri on a day to day basis too! Installation Nokogiri is actually a wrapper around Daniel Veillard's excellent HTML/XML parsing library written, libxml2. I recommend installing libxml2 on OS X from macports. To install libxml2 from macports: $ sudo port install libxml2 libxslt Then to install nokogiri: $ sudo gem install nokogiri And that should be it! Linux On Fedora: On Ubuntu:

RVM: Ruby Version Manager - RVM Ruby Version Manager - Documentation Ruby Inside: The Ruby Blog How to parse CSV data with Ruby - Ruby - csv, parse, ruby, fastercsv, ccsv, csvscan, excelsior Ruby alternatives for parsing CSV files Ruby String#split (slow) Built-in CSV (ok, recommended) ccsv (fast & recommended if you have control over CSV format) CSVScan (fast & recommended if you have control over CSV format) Excelsior (fast & recommended if you have control over CSV format) CSV library benchmarks can be found here and here Parsing with plain Ruby filename = 'data.csv' file = File.new(filename, 'r') file.each_line("\n") do |row| columns = row.split(",") break if file.lineno > 10end This option has several problems… Parsing with the built-in CSV library require 'csv' CSV.open('data.csv', 'r', ';') do |row| puts row end require 'csv' CSV.foreach("changes.csv", quote_char: '"', col_sep: ';', row_sep: :auto, headers: true) do |row| puts row[0] puts row['xxx'] end Parsing with the ccsv library ccsv is hosted on GitHub. require 'rubygems'require 'ccsv' Ccsv.foreach(file) do |values| puts values[0] end Parsing with the CSVScan library CSVScan can be downloaded from here.

AWK-ward Ruby This essay was to be published as a companion piece to The Shell Hater's Handbook, an introductory talk on UNIX shell programming for Ruby hackers given at GoGaRuCo 2010. Alas, the post-conference wrap up magazine will not be published this year and so I'm making the essay available here instead. Ruby, like most successful languages, was assembled from pieces of things that came before it: Smalltalk's consistent object system, Perl's practical syntax, UNIX's sensibilities. There's something less obvious but perhaps more essential that Ruby borrowed: the very concept of blatant, unashamed borrowing. When I started designing Perl, I explicitly set out to deconstruct all the computer languages I knew and recombine or reconstruct them in a different way, because there were many things I liked about other languages, and many things I disliked. Or did it? If these features didn't originate with Perl, as Wall seems to imply, then where did they come from? cat /etc/passwd | awk -F: '{ print $1 }'

Related: