background preloader

Ruby

Facebook Twitter

RubyMonk - Ruby Primer - Iteration. RVM: Ruby Version Manager - RVM Examples. $ rvm help # Documentation Index RVM version $ rvm -v rvm 1.11.7 (stable) by Wayne E. Seguin Use a particular Ruby. $ rvm use 1.8.6 Equivalently (because of RVM's defaults): $ rvm 1.8.6 List Ruby interpreters available for installation $ rvm list known # MRI Rubies [ruby-]1.8.6[-p399] [ruby-]1.8.6-head [ruby-]1.8.7[-p330] [ruby-]1.8.7-head [ruby-]1.9.1-p243 [ruby-]1.9.1[-p378] [ruby-]1.9.1-p429 [ruby-]1.9.1-head [ruby-]1.9.2-preview1 [ruby-]1.9.2-preview3 [ruby-]1.9.2-rc1 [ruby-]1.9.2-rc2 [ruby-]1.9.2[-p136] [ruby-]1.9.2-head ruby-head # GoRuby goruby # JRuby jruby-1.2.0 jruby-1.3.1 jruby-1.4.0 ... List Ruby interpreters you've already installed Ruby information for the current shell Install JRuby (default version is 1.3.1), but do not use it right now. $ rvm install jruby Use Ruby 1.9.1 $ rvm use 1.9.1 Equivalent to "rvm use 1.9.1", due to defaults $ rvm 1.9.1 Use Ruby 1.8.6 Switch to gems directory for current ruby $ rvm gemdir /Users/wayne/.gem/ruby/1.9.1 Switch to the system gems directory $ rvm reset.

Bootstrap 3: Change Stacking Order | A Geek and His Blog. Changing the stacking order of columns in Twitter’s Bootstrap 3 is actually easier than you may think. Excited yet? Calm down. Below is an example situation where you may want to change the stacking order. We have a nice 2 column row that has text on the left and an image on the right.

This is a common setup that many websites use. Both the text and image each take up 6 grid columns (so they’re equal spacing). This works because when there’s enough space, they float into their original positions at first. Incoming search terms: bootstrap push pull examplesbootstrap grid orderbootstrap change column ordercol-md-push-*pull and push classes in bootstrapbootstrap stack colbootstrap stacking orderbootstrap rows pushchanging order of elements in bootstrap3bootstrap reverse stacking in small screen. Building Awesome Rails APIs: Part 1. After teaching an Advanced Rails class last week, I realized that we use a lot of patterns internally to build great APIs in Rails that many people don’t know about.

We didn’t invent most of them, but we use them with great success, so we need to start sharing. Namespace your API A really easy way to keep your API code clean is to namespace it. Give it its own controllers and routes. We start by adding this in our routes, assuming Person is the resource (model) we’re working with: namespace :api do resources :peopleend This now gives us the standard routes for people, but namespaced as /api/people. Class Api::PeopleController < ApplicationController end This is great on its own, but we can even fix that ugly Api by using Rails’ built-in inflections. ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'API'end This is a small change, but now our controller can use the API module, which feels a bit better. class API::PeopleController < ApplicationController Versioning. Best In Place TEST APP. Go back to USERS Click to edit Try the features of Best In Place: Try giving wrong email values or too short address, inputs to see server errors.

Click on newsletter to change a boolean value Click on country to change the value in a collection of values Use the external handler to change the value of the name Try making changes inside inputs or textareas and then press the ESC key to recover the old value Try creating HTML only in the description textarea, where the option :sanitize is set to false, in the others the input is sanitized More information on github or bernatfarrero.com.

#296 Mercury Editor. Mercury allows you to edit a section of HTML directly in the web browser through a WYSIWYG editor. Here I show how to integrate Mercury into a Rails app and save the changes back to the database. Download: source codeProject Files in Zip (88.9 KB)mp4Full Size H.264 Video (32.8 MB)m4vSmaller H.264 Video (15.4 MB)webmFull Size VP8 Video (16.6 MB)ogvFull Size Theora Video (39.7 MB) Gsub (String) Replacing with "\" and match If you’re trying to place a “" in front of your matches, you’ll quickly see that it is a pain in the ass to add the quoting in the replacement string. Here’s an example: v = "Foo Bar! " v.gsub(/\W/, '\0') v.gsub(/\W/, '\\0') v.gsub(/\W/, '\\\0') v.gsub(/\W/, '\\\\0') v.gsub(/\W/, '\\\\\0') v.gsub(/\W/) { |m| "\\#{m}" } Surely, there must be an easier way to do this.

#390 Turbolinks. Turbolinks can make your Rails app feel faster by using JavaScript to replace the page content when clicking a link. It will be default in new Rails 4.0 applications, but here I show how to use it in Rails 3 and mention some of the gotchas. Download: source codeProject Files in Zip (63.9 KB)mp4Full Size H.264 Video (21.5 MB)m4vSmaller H.264 Video (9.41 MB)webmFull Size VP8 Video (10.9 MB)ogvFull Size Theora Video (22.9 MB) Introducing Turbolinks for Rails 4.0 - GeekMonkey. David Heinemeier Hansson, creator of Ruby on Rails, recently announced on Twitter that a feature called Turbolinks will ship as default-on in the Gemfile of Ruby on Rails 4.0. Turbolinks is already powering the mobile website of Basecamp. Someone at Hacker News posted a link to the Turbolinks repository on Github and titled it: Turbolinks for Rails (like pjax) So what is PJAX and what does Turbolinks do differently?

HTML5 introduced several new APIs to JavaScript, one being the History interface. pushState allows JavaScript to store arbitrary data into the session history, combined with a title and an optional URL. The back() and forward() methods then allow you to navigate through the pushed session history. This way pushState can be used to store the navigation history of the current page and dynamically switch backwards on forwards through different states without reloading the whole page. PJAX is a portmanteau of the terms pushState and AJAX. As so often the answer is: It depends. Kossnocorp/jquery.turbolinks. Jquery - Character countdown like on twitter. HTML5 for the Mobile Web: Canvas. Next up in our HTML5 for Mobile Web series is the canvas element. Canvas is particularly interesting since it facilitates the use of graphics without the need for any plugins or other technologies other than JavaScript and CSS. Quite a few new elements were introduced in HTML5, although few have received as much attention as canvas.

Several elements have been removed too, including some of the most infamous ones such as big, font, and frameset. For a full reference of new and removed HTML5 elements, please see this page In this article, we'll take a look at how to setup a canvas suitable for use on a mobile device, and then we'll look at how we can handle images, text, drawing arc and curves, and various transformations that can be performed. We'll also embed several live examples for good measure.

We pull some of these ideas together into our final example, an HTML5 Meme Generator, which we broke out into a separate article. Setting up your canvas Images. How to Setup a Production Server for Rails 4 // Rob McLarty. Choose a Service Provider I already had an account with Media Temple, so I thought that keeping all my cloud junk together and staying with MT would make things easier to manage, for me personally. I'm not getting paid to endorse MT in any way.

Without my existing relationship I could have just as easily gone with something like Rackspace, Linode, or even Heroku. I was previously using MT's DV server which is a pre-installed setup that comes with a custom CentOS and Plex configuration. But I noticed that when using this setup for clients, each time I ordered a new one, the configurations changed and I had to figure out how to solve problems I had already dealt with when I wanted to put together the same small Rails/Passenger production system I've used for all my apps.

I was looking for something I could repeat on-demand with minimal unknowns. So for my production server (the one running this blog) I chose to go with Media Temple's VE server. Install an Operating System passwd bill visudo. Prologue | HTML5 UP. Pricing. Introduction to Database Design (on Rails): Part II. In a previous post, I explained the fundamentals of database design. This guide will cover the second half of that topic: how to make the database work with Rails. Before reading this you should have a good understanding of what a database is and how to organize one. Rails is a framework that sits on top of the programming language Ruby. The framework speeds up web development by filling in code that you'd otherwise write from scratch on every new project.

A component of this is ActiveRecord, a subset of Rails that acts as the bridge between your database and your Ruby code. Here are two examples of a Ruby class: A class is a just a blueprint for a given object. We represent relationships between tables like this: The top diagram should be familiar to you; it' s a simple foreign key/primary key database relationship. Hint: it's often difficult to decide which class gets the :hasmany and which gets the :belongsto. This works for all types of relationships. …I can do the following: A Guide to Active Record Associations.

1 Why Associations? Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Class Customer < ActiveRecord::Base end class Order < ActiveRecord::Base end Now, suppose we wanted to add a new order for an existing customer. @order = Order.create(:order_date => Time.now, :customer_id => @customer.id) Or consider deleting a customer, and ensuring that all of its orders get deleted as well: @orders = Order.find_by_customer_id(@customer.id)@orders.each do |order| order.destroy end@customer.destroy With Active Record associations, we can streamline these — and other — operations by declaratively telling Rails that there is a connection between the two models.

Class Customer < ActiveRecord::Base has_many :orders, :dependent => :destroyend class Order < ActiveRecord::Base belongs_to :customerend. Ruby on Rails Tutorial Sample App. Ruby Primer - Loops in Ruby. Loops are programming constructs that help you repeat an action an arbitrary number of times. The methods Array#each, Array#select etc. are the most frequently used loops since the primary use of loops is to iterate over or transform a collection, something that we'll learn in the chapter on "Arrays in Ruby.

" Here we will cover two basic looping constructs you can use to solve most other looping requirements that may come up. Infinite Loops Infinite loops keep running till you explicitly ask them to stop. Loop do puts "this line will be executed for an infinite amount of time" end The example above does not have a termination condition and hence will run till the process is stopped. Now write an infinite loop where the monk will meditate till he achieves Nirvana. Run a block of code N times Say N is 5, let us imagine how it might look. 5.times do # do the stuff that needs to be done end Well, we imagined it right. Here is a task for you to test your newly learned looping skills.

Learn Web Development with the Ruby on Rails Tutorial | Beginning. 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. Scaffolding. Need reasons to love Bootstrap? Look no further. By nerds, for nerds. Built at Twitter by @mdo and @fat, Bootstrap utilizes LESS CSS, is compiled via Node, and is managed through GitHub to help nerds do awesome stuff on the web. Made for everyone. Bootstrap was made to not only look and behave great in the latest desktop browsers (as well as IE7!) Learn Ruby with the Neo Ruby Koans. Learn Web Development with the Ruby on Rails Tutorial | Filling In The Layout. Ruby in Twenty Minutes. Ruby Primer - Modules as Namespaces.