@users.sort! { |a,b| a.name.downcase <=> b.name.downcase } Since strings are comparable, this will sort user objects alphabetically by name. To sort on login_count; @users.sort! { |a,b| a.login_count <=> b.login_count }
Posts Tagged ‘ruby’
Ruby: Sorting an array of objects by an attribute
Posted in Ruby on Rails, tagged ruby, Ruby on Rails on September 28, 2011 | Leave a Comment »
Ruby – Usage of SELF in modules and libraries
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | Leave a Comment »
Following example gives the best undertanding of it. Also refer; http://www.arthurneil.com/ruby-on-rails/difference-include-extend-ruby module Foo def a puts “a: I am a #{self.class.name}” end def Foo.b puts “b: I am a #{self.class.name}” end def self.c puts “c: I am a #{self.class.name}” end end class Bar include Foo def try_it a Foo.b # Bar.b undefined Foo.c # Bar.c [...]
Difference between include and extend in Ruby
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | 1 Comment »
include : mixes in specified module methods as instance methods in the target class extend : mixes in specified module methods as class methods in the target class module ReusableModule def module_method puts “Module Method: Hi there!” end end class ClassThatIncludes include ReusableModule end class ClassThatExtends extend ReusableModule end puts “Include” ClassThatIncludes.new.module_method # “Module Method: [...]
Nested classes in Ruby
Posted in Ruby on Rails, tagged ruby, Ruby programming on September 25, 2011 | Leave a Comment »
We can create classes inside another classes in Ruby. In Ruby, classes defined are stored as constants with same name as class. That means any classes (say B,C) defined inside another class (A) would act like constants of that outer class ( as A::B, A::C). class A def test_method “I AM INSIDE CLASS A” end [...]
Removing extra whitespaces from string or params [Ruby on Rails]
Posted in Ruby on Rails, tagged lessons, ruby, Ruby on Rails on July 10, 2009 | Leave a Comment »
” Arthur Neil “.squeeze(” “).strip To find empty characters/space in a filed; use if params[:description].squeeze(” “).strip.length == 0 [handle it here] end
(uninitialized constant DBI::TypeUtil) (DBI::InterfaceError)
Posted in Ruby on Rails, tagged bug fixes, ruby on February 26, 2009 | 1 Comment »
use; require ‘rubygems’ gem ‘dbi’ before the line; require “dbi”
undefined method `gem’ for main:Object (NoMethodError)
Posted in Ruby on Rails, tagged bug fixes, ruby on February 26, 2009 | Leave a Comment »
Try using; require ‘rubygems’ in the firt line. That should fix your problem.