background preloader

Ruby

Facebook Twitter

Pry - an IRB alternative and runtime developer console. Pakyow: Punch Packing Web Framework for Ruby. Programming Ruby 1.9. About this Book 944 pages Published: Release: P5.0 (2012-10-16) ISBN: 978-1-93435-608-1 Would you like to go from first idea to working code much, much faster? Do you currently spend more time satisfying the compiler instead of your clients or end users? Are you frustrated with demanding languages that seem to get in your way, instead of getting the work done? Are you using Rails, and want to dig deeper into the underlying Ruby language? If so, then we’ve got a language and book for you!

Ruby is a fully object-oriented language, much like the classic object-oriented language, Smalltalk. The combination of the power of a pure object-oriented language with the convenience of a scripting language makes Ruby a favorite tool of intelligent, forward-thinking programmers. The Pickaxe contains four major sections: An acclaimed tutorial on using Ruby. What You Need This book assumes you have a basic understanding of object-oriented programming. About the Author Upcoming Author Events. Bbatsov/ruby-style-guide - GitHub. Variable/optional parameter methods, symbols and ruby « The Plan A. First if you want only for optional parameter in a method, just see the next two examples : def foo(bar=:hello) puts bar end foo #=> hello foo bye #=> bye The second example : def add_book(title=>"no title") arg={:, :author=>"anonymous", :language=>"esperanto"} arg.merge!

P_argirb(main):020:1> end add_book :title=>"El Qujote", :author=>"Miguel De Cervantes", :language=>"spanish" => {:language=>"spanish", :title=>"El Qujote", :author=>"Miguel De Cervantes"} add_book :title=>"El Qujote", :author=>"Miguel De Cervantes" => {:language=>"esperanto", :title=>"El Qujote", :author=>"Miguel De Cervantes"} add_book :title=>"El Qujote", :author=>"Miguel De Cervantes" :foo=>:Bar => {:language=>"esperanto", :title=>"El Qujote", :author=>"Miguel De Cervantes"} Sometimes is usefull to have an optional/variable parameters method, .

For example we want to add a book to our database, But we don’t want to write every single attribute, is the attribute is not present then is initialize with a default value. 5 things you can do with a Ruby array in one line (PLUS A FREE BONUS!!) « collect{thoughts} March 23, 2007 Coming from a Java background, I’ve found arrays to be very “rigid”. And by rigid I mean they sort of suck. However, Ruby makes arrays flexible and a blast to work with. Let’s show by example rather than have me ranting about it. First, we assume we have an array with 100 random integers between 0 and 99. I would show you how to do this but typing the append symbol breaks wordpress (grr). 1. Puts my_array.inject(0){|sum,item| sum + item} 2. My_array.map{|item| item*2 } 3. My_array.find_all{|item| item % 3 == 0 } 4. My_array.find_all{|item| item % 3 == 0 }.inject(0){|sum,item| sum + item } 5. My_array.sortmy_array.sort_by{|item| item*-1} Bonus!

My_array.find_all{|item| item =~ /\d{3}-\d{3}-\d{4}/ } Stay tuned, more Ruby goodness to come! Don’t be shy, check out everything you can do with Ruby’s Array Like this: Like Loading... A Ruby HOWTO: Writing A Method That Uses Code Blocks | Archives | codablog | Coda Hale. A Ruby HOWTO: Writing A Method That Uses Code Blocks One of the first things about Ruby which absolutely delighted me was its implementation of code blocks. They take a potentially ugly construct–anonymous delegates–and turn them into a readable, time-saving structure: chunky_bacon = %w(moo hoo ha ha) chunky_bacon.each { |cb| puts "--> #{cb}" } So how can we use this wonderful little bit of functionality in our own code?

Wait… why are code blocks cool? Okay, maybe there’re a few skeptics out there who are thinking to themselves “Gosh, Coda… this all sounds super-swell, but I’m not just not convinced that the classes I write could use code blocks.” Transactions: Do you have something which needs to be done atomically? Got it? Yay! If you’re anything like me, you immediately jumped to the RDoc info for the Ruby Core, and took a gander at the listing for Array#each: array.each {|item| block } → array That’s not helpful. Example One: A Basic Code Block Just that simple. Kernel#block_given? Hmm. Ruby on Rails Screencasts. RubyTree - A General Purpose Tree Data Structure for Ruby.

Ruby Programming/Syntax/Method Calls. A method in Ruby is a set of expressions that returns a value. With methods, one can organize their code into subroutines that can be easily invoked from other areas of their program. Other languages sometimes refer to this as a function. A method may be defined as a part of a class or separately. Method Calls[edit] Methods are called using the following syntax: method_name(parameter1, parameter2,…) If the method has no parameters the parentheses can usually be omitted as in the following: If you don't have code that needs to use method result immediately, Ruby allows to specify parameters omitting parentheses: results = method_name parameter1, parameter2 # calling method, not using parentheses # You need to use parentheses if you want to work with the result immediately.# e.g., if a method returns an array and we want to reverse element order: results = method_name(parameter1, parameter2).reverse Method Definitions[edit] Example: def output_something(value) puts value end Return Values[edit]

A Wealth Of Ruby Loops And Iterators. I remember when I first started looking at Ruby, I’d be browsing some code and see yet another way of looping/iterating over stuff. Whenever that would happen I would think, “…Ruby sure has a lot of different ways to iterate over things”, but I also remember wishing that someone would just put all the different ways to loop and iterate over stuff together so that you don’t have to discover them in a piecemeal fashion. Most resources I’ve seen tend to gloss over this a little bit by introducing the while loop and the each iterator and quickly moving on to more interesting things, expecting you to discover the rest on your own (nothing wrong with that by the way).

As you may have guessed from my recent posts on Ruby method arguments (and more advanced method arguments), the Ruby case statement and others, I am kind-of discovering Ruby in my own way and looking more closely at things that I find of interest. Ways To Loop Over Stuff In Ruby Unconditional Looping With Loop The While Loop. It’s Time To HTTParty! When you’re developing with Ruby, it’s pretty inevitable that at some point you come across an HTTP API that doesn’t have a gem available. While writing Hopefully Sunny I ran into just that situation with World Weather Online’s weather API. It’s often really tempting to go searching for a different API to use instead, but it’s actually not so hard to just write your own wrapper library.

Let’s take a look at one of my favorite libraries for working with HTTP APIs, HTTParty, and in the process we’ll figure out how to write our own simple API wrapper libraries. Getting Set Up You’re using Bundler, right? If you’re not using Bundler, you can run gem install httparty to get HTTParty installed. Really Basic Integration At its core, HTTParty encapsulates HTTP. The URL that we’re accessing above is from the RubyGems API, and will return a bit of JSON representing the versions of the httparty gem, as an array of objects containing information about each version.

Wrapping The Entity In A Class. The Sleepless Geek. Software: Ruby In Steel Developer - Overview. The only professional Ruby development tool for Visual Studio, Ruby In Steel Developer 2 has powerful analytical IntelliSense, the fast ‘Cylon’ debugger, syntax-sensitive editing and comprehensive development support for Ruby and Rails.

Go to: Ruby In Steel Download Page Here we provide a brief overview of just a few of the major features of Ruby In Steel... Debugging Ruby In Steel’s ultra-fast ‘Cylon’ debugger provides... Speed The ultra-fast ‘Cylon’ debugger Ruby and Rails debugging is notoriously slow. Breakpoints and Tracing Step through your code Step into/step-out/step-over code in Ruby and Rails applications. Hover and Drill-down Drill-down debugging Drill-down debugging lets you look inside objects and expand arrays and hashes in the docked debugging windows, in the code editor or even right inside the interactive Ruby console! Docked debug windows Conditional Breakpoints Add conditions in Ruby code Break when a condition (a test of one or more values) is met.

Other debugging features: Watch. Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby. Forum: wxRuby. File [ruby-doc.org] Absolute_path(file_name [, dir_string] ) → abs_file_name click to toggle source Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point.

If the given pathname starts with a “~” it is NOT expanded, it is treated as a normal directory name. File.absolute_path("~oracle/bin") VALUE rb_file_s_absolute_path(int argc, VALUE *argv) { VALUE fname, dname; if (argc == 1) { return rb_file_absolute_path(argv[0], Qnil); } rb_scan_args(argc, argv, "11", &fname, &dname); return rb_file_absolute_path(fname, dname); } atime(file_name) → time click to toggle source Returns the last access time for the named file as a Time object). file_name can be an IO object. File.atime("testfile") static VALUE rb_file_s_atime(VALUE klass, VALUE fname) { struct stat st; if (rb_stat(fname, &st) < 0) { FilePathValue(fname); rb_sys_fail_path(fname); } return stat_atime(&st); } exist?

WxRuby Documentation: Class Reference. This is the class reference for WxRuby. The classes listed below are those currently available in WxRuby2. This documentation was seeded by converting the WxWidgets C++ documentation, so it has some shortcomings. A small number of methods have inaccurate descriptions of the parameters they accept Some of the example code is still in C++ There are links to C++ tutorials and topic overviews that are not available – many are not relevant to wxRuby.

The following tutorials contain helpful overviews specific to the wxRuby API : wxRuby introduction : wxRuby syntax conventions window styles overview : window styles and wxRuby constants. Outline wxRuby includes a large number of classes. Others are helper classes used in handling data and managing all aspects of user interaction with applications: See also the list of wxRuby module functions . Window Any element drawn on the screen in wxRuby is a instance of Window or one if its subclasses.

Frames top Controls Miscellaneous windows Common dialogs Events. Getting Started with the wxRuby GUI Toolkit. Many readers have asked "What is the best GUI toolkit? ". There is no single answer to this question, and I won't try to lobby for my GUI of choice. Use whatever works best for you and your users. I use the wxRuby library. It's cross-platform, provides a native look-and-feel, and is easy to install (via a gem) and distribute with your application (via RubyScript2Exe).

Further features can be found in the wxRuby wiki here. wxRuby is the Ruby interface to wxWidgets, a very stable and widely-used widget toolkit. Installing wxRuby is simple using RubyGems: gem install wxruby Though you can use a GUI designer, creating most forms "by hand" is probably simpler than you suspect. Let's create a simple form with a label, text box, combo box, and button. Require 'wx'include Wx We create a new class which inherits from the Wx::Frame class and includes an initialize method: class MyFrame < Frame def initialize() super(nil, -1, 'My Frame Title') endend Parent: The window parent.

ID: The window identifier. Community, open source ruby on rails development : thoughtbot. Concurrency is a Myth in Ruby.

Rails 3

Ruby on Rails Tutorial: Learn Rails by Example | Ruby on Rails 3 Tutorial 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.