background preloader

Bbatsov/ruby-style-guide - GitHub

Bbatsov/ruby-style-guide - GitHub

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? 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. This is the reference manual for Ruby, including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9, 1.9.2, and 1.9.3¹). What You Need This book assumes you have a basic understanding of object-oriented programming. About the Author Upcoming Author Events

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! 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. add_book :title=>"El quijote", :author=> "Miguel de Cervantes" add_book :title=>"El quijote", :author=> "Miguel de Cervantes", :tag=>"novel" add_book :title=>"El quijote" add_book :title=>"hogehoge", :language=>"Japanese" # => raise ArgumentError Asking in the ruby list, Hidetoshi NAGAI, teach me how to use symbols, variable parameter methods (varargs), to create this clean example. In a next post i explain ho to to improve the optinal parameter using meta-programming. Like this:

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. 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? Well, break out a text editor and a terminal window, because it’s time to write some Ruby! 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

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

Pry - an IRB alternative and runtime developer console

Related: