background preloader

Ruby Regular Expressions

Ruby Regular Expressions
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern. A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r as follows: Syntax: /pattern//pattern/im # option can be specified%r!/usr/local! # general delimited regular expression Example: #! This will produce the following result: Line1 contains Cats Regular-expression modifiers: Regular expression literals may include an optional modifier to control various aspects of matching. Like string literals delimited with %Q, Ruby allows you to begin your regular expressions with %r followed by a delimiter of your choice. # Following matches a single slash character, no escape required%r|/| # Flag characters are allowed with this syntax, too%r[</(.*)>]i Regular-expression patterns: Except for control characters, (+ ? Following table lists the regular expression syntax that is available in Ruby. #!

Ruby Z Wikibooks, biblioteki wolnych podręczników. Ruby to interpretowany, w pełni obiektowy język programowania stworzony przez Yukihiro Matsumoto (pseudonim Matz). W języku angielskim ruby oznacza rubin. Od roku 2003 lawinowo zdobywa nowych zwolenników, głównie za sprawą popularnego frameworku do tworzenia aplikacji webowych o nazwie Ruby on Rails, tworzonego przez grupę programistów pod kierownictwem Davida Heinemeiera Hanssona. W roku 2005 według statystyk sklepu Amazon dwie najpopularniejsze książki na temat Rubiego i Ruby on Rails były najlepiej sprzedawanymi pozycjami z kategorii Programowanie. Punktem wyjścia dla niniejszego podręcznika stała się książka Marka Slagella "Ruby's User Guide" udostępniona na licencji GNU Free Documentation License. Spis treści[edytuj] Podstawy[edytuj] Programowanie zorientowane obiektowo[edytuj] Licencja[edytuj] Licencja Zobacz też[edytuj] Linki zewnętrzne[edytuj]

[Akita Responde] Começando com Ruby on Rails Agora, para começar a entender a teoria, existem alguns materiais que você pode usar: Se estiver muito no começo de programação em geral, leia o livro Aprenda a Programar, do Chris Pine, traduzido em português. Se quiser aprender a linguagem Ruby, leia o livro O Guia (Comovente) de Ruby do Why, também traduzido em português. Os seguintes livros são interessantes para Ruby: The Ruby Way, do Hal Fulton, é um dos melhores livros para aprender Ruby a fundo. Aliás, um cuidado importante: se você veio de outra linguagem, vai querer programar Ruby do jeito Java, ou do jeito PHP, mas assim você está fazendo errado. Para aprender Ruby on Rails, o framework Web, veja estas referências: O site oficial tem o Rails Guides com uma documentação completa de tudo que você precisa para começar no Ruby on Rails. Não recomendo comprar livros muitos avançados de Rails, porque o framework está evoluindo o tempo todo e os livros impressos de Rails tendem a ficar obsoletos muito rápido.

Ruby for Newbies: Regular Expressions Ruby is a one of the most popular languages used on the web. We’ve started a new Session here on Nettuts+ that will introduce you to Ruby, as well as the great frameworks and tools that go along with Ruby development. In this lesson, we’ll look at using regular expression in Ruby. If you’re familiar with regular expressions, you’ll be glad to know that most of the syntax for writing the actual regular expressions is very similar to what you know from PHP, JavaScript, or [your language here]. If you’re not familiar with regular expressions, you’ll want to check out our Regex tutorials here on Nettuts+ to get up to speed. Just like everything else in Ruby, regular expressions are regular objects: they’re instances of the Regexp class. To start, the simplest way to use a regexp is to apply it to a string and see if there’s a match. Both of these examples match, and so we’re going to get a MatchData instance back (we’ll look at MatchData objects soon). So, let’s execute this: subsub!

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. Ruby can be used as a fully object oriented language, in which case you'd create classes and objects to accomplish everything. This is the simplest possible Ruby program, hello.rb. Let's count to 10... Retry

21 Ruby Tricks You Should Be Using In Your Own Code Writing for Ruby Inside, I get to see a lot of Ruby code. Most is good, but sometimes we forget some of Ruby's shortcuts and tricks and reinvent the wheel instead. In this post I present 21 different Ruby "tricks," from those that most experienced developers use every day to the more obscure. Whatever your level, a refresh may help you the next time you encounter certain coding scenarios. Note to beginners: If you're still learning Ruby, check out my Beginning Ruby book. 2009 Update: This post was written in early 2008 and looking back on it, there are a couple of tricks that I wouldn't recommend anymore - or to which extra warnings need to be added. 1 - Extract regular expression matches quickly A typical way to extract data from text using a regular expression is to use the match method. email = "Fred Bloggs <fred@bloggs.com>"email.match(/<(.*?) Ultimately, using the String#[] approach is cleaner though it might seem more "magic" to you. x = 'this is a test' x[/[aeiou].+? puts x == 10 ?

Ruby Regular Expressions: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial Regular expressions, though cryptic, is a powerful tool for working with text. Ruby has this feature built-in. It's used for pattern-matching and text processing. Many people find regular expressions difficult to use, difficult to read, un-maintainable, and ultimately counterproductive. A regular expression is simply a way of specifying a pattern of characters to be matched in a string. You could write a pattern that matches a string containing the text Pune or the text Ruby using the following regular expression: The forward slashes delimit the pattern, which consists of the two things we are matching, separated by a pipe character (|). The simplest way to find out whether there's a match between a pattern and a string is with the match method. m1 = /Ruby/.match("The future is Ruby") puts m1.class m2 = "The future is Ruby" =~ /Ruby/ puts m2 m1 = /Ruby/.match("The future is Ruby") puts m1.class # it returns MatchData m2 = "The future is Ruby" =~ /Ruby/ puts m2 # it returns 14

Język programowania Ruby Ruby Procs And Lambdas (And The Difference Between Them) As you know, I am a big believer in knowing the basics. I find it is especially valuable to go back to the fundamentals of whatever skill you're trying to master periodically, as you gain more experience. Somehow you're always able to extract something valuable from the experience due to the extra insight you've acquired along the way. Recently I've been looking more closely at some Ruby concepts that I may have glossed over when I was learning the language initially, so I will likely be writing about this in some of my upcoming posts. What's So Good About Procs? You know how everything in Ruby is an object, well, as it turns out that's not quite true. Procs And Lambdas In Detail There are three ways to create a Proc object in Ruby. Using Proc.new. Those are all the different ways to get a Proc object in Ruby (either a proc or a lambda). Procs Are First Class (Functions That Is) alan@alan-ubuntu-vm:~/tmp$ ruby a.rb String are equal? alan@alan-ubuntu-vm:~/tmp$ ruby a.rb Procs are equal?

Regex Runner: a game to teach regular expressions to kids Last December, I wrote a column suggesting that games would be a great way to teach kids Regular Expressions, a part of the world of programming that surfaces in tons of non-expert applications, like word processors. Now, Sal writes, "For my Computer Games course project, I used your article regarding reg-exp to try to make an educational game, there's already SO many academic papers and books on this area it's overwhelming. Rather than read up and design something based on researched principles, I thought instead about what game I was playing most on my phone and I decided those "always running" type games (Temple Run/Canabalt etc) are super cool. Using that mechanic as a template, I shoehorned in some letters and symbols and played around with a randomly generated grid and a couple of sprites. Regex Runner

Ruby on Rails Guides 19 Rails Tricks Most Rails Coders Don’t Know New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast. A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Please note that this post is over four years old - it's from 2006! When looking at my own Rails code and that of the community as a whole, I often see places where certain Rails techniques could have been used, but weren't. Benchmark logic in your controller actions - It's really easy. User.benchmark("adding and deleting 1000 users") do 1000.times do User.create(:name => 'something') x = User.find_by_name('something') x.destroy endend Of course, your code would be a lot better ;-) The regular SQL logs are not shown when within the benchmark sections. You can learn a little more here.

Related: