Eloquent Ruby

It’s easy to write correct Ruby code, but to gain the fluency needed to write great Ruby code, you must go beyond syntax and absorb the “Ruby way” of thinking and problem solving. In Eloquent Ruby, Russ Olsen helps you write Ruby like true Rubyists do–so you can leverage its immense, surprising power.

Olsen draws on years of experience internalizing the Ruby culture and teaching Ruby to other programmers. He guides you to the “Ah Ha!” moments when it suddenly becomes clear why Ruby works the way it does, and how you can take advantage of this language’s elegance and expressiveness.

Eloquent Ruby starts small, answering tactical questions focused on a single statement, method, test, or bug. You’ll learn how to write code that actually looks like Ruby (not Java or C#); why Ruby has so many control structures; how to use strings, expressions, and symbols; and what dynamic typing is really good for.

Next, the book addresses bigger questions related to building methods and classes. You’ll discover why Ruby classes contain so many tiny methods, when to use operator overloading, and when to avoid it. Olsen explains how to write Ruby code that writes its own code–and why you’ll want to. He concludes with powerful project-level features and techniques ranging from gems to Domain Specific Languages.

A part of the renowned Addison-Wesley Professional Ruby Series, Eloquent Ruby will help you “put on your Ruby-colored glasses” and get results that make you a true believer.

 

 

Proxy für open-uri verwenden

In vielen Umgebungen ist der direkte Zugriff auf z.B. Web-Inhalte nur über einen Proxy möglich. In unserem heutigen Script-Tip zeigen wir euch wie ihr für open-uri einen Proxy setzen könnt.

[ruby]
require ‚open-uri‘

url = ‚http://www.example.com/‘
proxy_adresse = ‚http://123.456.789.10‘
proxy_port = ‚8888‘

seite = open(url, :proxy => (proxy_adresse + ":" + proxy_port))

[/ruby]

Parameter an send Methode übergeben

Wenn der direkte Aufruf eine Methode nicht möglich ist oder auch häufig bei der Meta-Programmierung, kann in Ruby die send-Methode verwendet werden. Diese Methode wird von Ruby selber benutz um Methoden in Klassen aufzurufen.

Da Methoden auch Parameter erwarten können, wollen wir euch in unserem heutigen Script-Tip zeigen wie das geht.

[ruby]

class MeineKlasse
def hallo(*args)
"Hallo " + args.join(‚ ‚)
end
end

mk = MeineKlasse.new

mk.send :hallo, "ihr", "lieben", "Leser"

#=> "Hallo ihr lieben Leser"

[/ruby]

Zeitgesteuerte Programmausführung ohne cron

Hin und wieder ist nötig Scripte/Programme zu einem bestimmten Zeitpunkt auszuführen. Auf unixoiden System wird in der Regel cron oder at dafür benutzt. Bei Windows kommt hierfür häufig die Services zum Einsatz. Wenn all dies nicht möglich ist oder man lieber bei Ruby bleiben will, kann unseren heutigen Script-Tip dafür benutzen.

Bei dieser Lösung wird lediglich Ruby (und selbstverständlich das auszuführende Programm) benötigt.

[ruby]

#!/usr/bin/ruby

def background_run_at(time)
fork do
sleep(1) until Time.now >= time
yield
end
end

heute = Time.now
mittag = Time.local(heute.year, heute.month, heute.day, 12, 0, 0)
raise Exception, "Es ist bereits mittag" if mittag < Time.now

background_run_at(mittag) { puts "Am mittag ausgeführt!" }

[/ruby]

Ruby im Webbrowser (wieder verfügbar)

Viele kennen die irb Ruby Konsole von „Why The Lucky Stiff“. Die Konsole lief im Browser und jeder konnte so seine ersten Ruby-Geh-Versuche im Browser testen bevor man sich den Interpreter installiert hat. Nach dem misteriösen Verschwinden von Why war eine zeitlang unklar was mit seinen Projekten passieren würde.

Inzwischen hat sich die Lage „entspannt“ und die Projekte haben ein anderes Zuhause auf Github gefunden.

Auch die irb-Browser-Konsole ist wieder online und jeder kann sie unter folgender Adresse benutzen: http://tryruby.sophrinix.com/

MySQL DB in UTF8 konvertieren

In der Regel sollte man das Encoding für seine Datenbanken auf UTF8 stellen um später keine bösen Überraschungen zu erleben. Falls beim Entwicklungstart diese Einstellung vergessen wurde oder wenn man sich später zu UTF8 „bekennt“ kann man folgendes Ruby-Script benutzen um die Datenbank zu konvertieren.

Ein Wort der Warnung: Wie immer sollte man IMMER ein Backup anlegen bevor man seine Daten in großem Stil verändert –> Ihr seid gewarnt!

Hier nun das Script:
Es ruft im Grunde die bekannten Shellscript-Befehle (iconv, mysqldump, etc.) auf um die Konvertierung durchzuführen.

[ruby wraplines=“true“]

class ConvertDbToUtf8

def self.up

db_name = ‚DATENBANK-NAME‘
db_user = ‚DATENBAK-BENUTZER‘
db_pass = ‚DATENBANK-PASSWORT‘
db_host = ‚DATENBANK-HOST‘

latin1_mysql_dump = ‚latin1_mysql_dump.sql‘
utf8_mysql_dump = ‚utf8_mwsql_dump.sql‘

print "DB-Dump holen…. "
system "mysqldump –user=#{db_user} –password=’#{db_pass}‘ –host=#{db_host} –add-drop-table –default-character-set=latin1 –insert-ignore –skip-set-charset #{db_name} > #{latin1_mysql_dump}"
puts "…fertig"

print "Konvertierung nach UTF8 starten… "
system "iconv -f ISO-8859-1 -t UTF-8 #{latin1_mysql_dump} | sed ’s/latin1/utf8/‘ > #{utf8_mysql_dump}"
puts "…fertig"

print "Datenbank neu anlegen"
system "mysql –user=#{db_user} –password=’#{db_pass}‘ –host=#{db_host} –execute="DROP DATABASE #{db_name};""
system "mysql –user=#{db_user} –password=’#{db_pass}‘ –host=#{db_host} –execute="CREATE DATABASE #{db_name} CHARACTER SET utf8 COLLATE utf8_unicode_ci;""
puts "…fertig"

print "Konvertierten UTF8 Dump einspielen…"
system "mysql –user=#{db_user} –password=’#{db_pass}‘ –host=#{db_host} –default-character-set=utf8 #{db_name} < #{utf8_mysql_dump}"
puts "…fertig"

puts " Konvertierung abgeschlossen "
end

def self.down
raise "Konvertierung fehlgeschlagen"
end

end

[/ruby]

Practical Ruby Projects: Ideas for the Eclectic Programmer

Want to take Ruby to the limit? Looking for new, powerful, and creative ideas that will take Ruby beyond Rails and web programming? If you’re comfortable with Ruby, you’ll be itching to go further–apply Practical Ruby Projects: Fresh Ideas with Ruby and become a master of advanced Ruby techniques.

Rubyist Topher Cyll brings several imaginative projects to this book, ranging from making generative music, animations, and turn-based games to implementing simulations, algorithms, and even an implementation of Lisp!

Art, music, theory, and games–this book has it all. Best of all, it’s all done with Ruby.

Each chapter, in addition to making you say “Cool–I hadn’t thought of that before,” looks at solving tricky development problems, enforces best practices, and encourages creative thinking. You’ll be building your own exciting, imaginative Ruby Projects in no time.

  • Create imaginative and innovative Ruby programming projects.
  • Learn how to solve tricky development problems, be guided by best practices, and be inspired to think creatively.
  • Don’t waste time on the basics–it’s assumed you know the fundamentals of Ruby already.

What you’ll learn

  • How to set up Ruby on Windows, Mac OSX, and Linux
  • How to create UIs with RubyCocoa
  • How to create a Lisp implementation in Ruby
  • How to create generative music and turn-based adventure games in Ruby
  • How to model things and create algorithms in Ruby
  • Several more Ruby best practices and programming techniques that will save you hours of programming time

Who is this book for?

This book is for anyone who has mastered the basics of Ruby and wants to learn advanced Ruby techniques.

About the Apress Practical Series

The Practical series from Apress is your best choice for getting the job done, period. From professional to expert, this series lets you apply project-motivated templates (or frameworks) step by step in a very direct, practical, and efficient manner toward current real-world projects that may be sitting on your desk. So whatever your career goal, Apress can be your trusted guide to take you where you want to go on your IT career empowerment path.

About the Author

Topher Cyll is a software engineer and writer living in Cambridge, Massachusetts. He received his bachelor’s degree in computer science from Williams College and works for a small Boston-area startup.

In reverse alphabetical order, he finds programming languages, music, Free Software, education, bioengineering, and beer terribly exciting.

Topher loves Ruby not only for the language itself, but also the light-hearted and intellectually curious community that surrounds it.

  • Taschenbuch: 328 Seiten
  • Verlag: (2007-12-10 )
  • Preis: 24.44 €
  • Sprache: english
  • ISBN-10: 159059911X
  • Autor(en): Topher Cyll
  • kaufen

Professional Ruby Series, CD-ROM Mongrel, Routing, RESTful, Plugins, and Rubyisms

Rails Routing focuses on the extensive and powerful routing subsystem in Rails–the suite of techniques for manipulating the process by which incoming requests to a Rails application can be matched with, or mapped to, specific actions. Rails Refactoring to Resources. If you’re new to Representational State Transfer (REST), this Short Cut will help you decide which parts of the REST paradigm you want to introduce to your application. If you’re a developer with more RESTful experience, it will introduce you to some refactorings that will give your application a cleaner, leaner code base. Rubyisms in Rails is an examination of how the style of Ruby informs the design of Rails.

In particular, it looks at a few specific examples of how Rails‘ internal code is implemented in Ruby to instruct about Ruby’s design principles. Rails Plugins teaches you what plugins are, how they work, and why they’re useful. Discover how to find and install plugins using the provided script, then explore the world of plugin development, including common plugin idioms, testing, and version control. Finally, learn how to share your own plugins. Mongrel introduces the fast, versatile Ruby Web server.If you build or manage Web applications, this will be a useful reference as you set up and use Mongrel in your development and production environments.

Also teaches how to extend Mongrel.

  • Taschenbuch: 192 Seiten
  • Verlag: (2007-08-05 )
  • Preis: 25.59 €
  • Sprache: english
  • ISBN-10: 0132417995
  • Autor(en): Dave Thomas, David A. Black
  • kaufen

Design Patterns in Ruby (Addison-Wesley Professional Ruby)

Praise for Design Patterns in Ruby "Design Patterns in Ruby documents smart ways to resolve many problems that Ruby developers commonly encounter. Russ Olsen has done a great job of selecting classic patterns and augmenting these with newer patterns that have special relevance for Ruby. He clearly explains each idea, making a wealth of experience available to Ruby developers for their own daily work." -Steve Metsker, Managing Consultant with Dominion Digital, Inc. "This book provides a great demonstration of the key ‚Gang of Four‘ design patterns without resorting to overly technical explanations. Written in a precise, yet almost informal style, this book covers enough ground that even those without prior exposure to design patterns will soon feel confident applying them using Ruby. Olsen has done a great job to make a book about a classically ‚dry‘ subject into such an engaging and even occasionally humorous read." -Peter Cooper "This book renewed my interest in understanding patterns after a decade of good intentions.

Russ picked the most useful patterns for Ruby and introduced them in a straightforward and logical manner, going beyond the GoF’s patterns.This book has improved my use of Ruby, and encouraged me to blow off the dust covering the GoF book." -Mike Stok "Design Patterns in Ruby is a great way for programmers from statically typed objectoriented languages to learn how design patterns appear in a more dynamic, flexible language like Ruby." -Rob Sanheim, Ruby Ninja, Relevance Most design pattern books are based on C++ and Java. But Ruby is different-and the language’s unique qualities make design patterns easier to implement and use. In this book, Russ Olsen demonstrates how to combine Ruby’s power and elegance with patterns, and write more sophisticated, effective software with far fewer lines of code. After reviewing the history, concepts, and goals of design patterns, Olsen offers a quick tour of the Ruby language-enough to allow any experienced software developer to immediately utilize patterns with Ruby.

The book especially calls attention to Ruby features that simplify the use of patterns, including dynamic typing, code closures, and "mixins" for easier code reuse.Fourteen of the classic "Gang of Four" patterns are considered from the Ruby point of view, explaining what problems each pattern solves, discussing whether traditional implementations make sense in the Ruby environment, and introducing Ruby-specific improvements. You’ll discover opportunities to implement patterns in just one or two lines of code, instead of the endlessly repeated boilerplate that conventional languages often require. Design Patterns in Ruby also identifies innovative new patterns that have emerged from the Ruby community. These include ways to create custom objects with metaprogramming, as well as the ambitious Rails-based "Convention Over Configuration" pattern, designed to help integrate entire applications and frameworks. Engaging, practical, and accessible, Design Patterns in Ruby will help you build better software while making your Ruby programming experience more rewarding.

  • Taschenbuch: 384 Seiten
  • Verlag: (2007-12-21 )
  • Preis: 37.06 €
  • Sprache: english
  • ISBN-10: 0321490452
  • Autor(en): Russ Olsen
  • kaufen

Flexible Rails: Flex 3 on Rails 2

Flexible Rails is a unique, application-based guide for using Ruby on Rails and Adobe Flex to build rich Internet applications (RIAs). It is not an exhaustive Ruby on Rails or a Flex reference. Instead, it is an extensive tutorial in which the reader builds multiple iterations of an interesting RIA using Flex and Rails together.

Author Peter Armstrong walks readers through over a dozen iterations in which the sample application – pomodo – is variously built, refactored, debugged, sliced, diced, and otherwise explored from every conceivable angle with respect to Ruby on Rails and Adobe Flex. The book unfolds both the application and the Flex-on-Rails approach side-by-side. If you’re doing any work on Flex and Ruby, I’d highly recommend getting a copy of Flexible Rails. – Mike Potter, Adobe Developer Relations For those of you who want a little more richness in your application interfaces, and Ajax just isn’t cutting it, Flexible Rails is exactly what you’ve been waiting for. – Steven Baker, creator of RSpec, the BDD framework for Ruby.

  • Taschenbuch: 592 Seiten
  • Verlag: (2008-01-25 )
  • Preis: 33.99 €
  • Sprache: english
  • ISBN-10: 1933988509
  • Autor(en): Peter Armstrong
  • kaufen