Rails console is a great way to execute scripts, but what if the script is not located on your folder dir or you need to move to a folder with different permissions. The Ruby Dir Class is perfect for moving around the file system to find what you need. $ rails console Loading production environment (Rails 2.3.17) >> Dir.pwd => "/usr/home/rwdev/deployer" >> Dir.chdir("/home/brett") => 0 >> Dir.chdir("../") => 0 >> Dir.entries("brett") => [".", "..", ".cshrc", ".rhosts", ".profile", ".bash_history", ".shrc", ".login", ".mailrc", ".login_conf", ".mail_aliases"] >> Dir.chdir("brett") => 0 >> Dir.pwd => "/usr/home/brett" >> Dir.glob("*") => [] These commands provide a good way to move around the file system and execute commands from the console. :)

Collecting media for all reasons is a common thing we do. I like to keep my directory structure clean and organized on my machine. Today I was faced with an issue of downloading a bunch of educational media, so I needed 100 folders to organize my downloads. Creating 100 directories takes a stupid long time manually so I wrote a quick script to solve the issue. 1. Create the file $ vim create_files_with_ruby.rb 2. Write this script #!/usr/local/bin/ruby -w   require ‘fileutils’   dir = Dir.pwd if dir != "/Users/<username>/Movies/RubyTapas" Dir.chdir("/Users/<username>/Movies/RubyTapas") dir = Dir.pwd end   (9..100).each do |i| if i == 71 puts "nothing here" elsif i.to_s.length > 2 FileUtils.mkpath "100" elsif i.to_s.length > 1 f_name = "0" +[…]

Yesterday I exampled a quick setup of a class in Ruby. Today I will use the same class but in the Rails context. I will also use the ‘delegate’ method to reduce code. To see the original code click here. Here is the refactor in Ruby on Rails with delegate.   User = Struct.new(:email, :name)   class Course < Struct.new(:instructor)   delegate :name, :email, :to => :instructor   end   >> u = User.new("brett@brettu.com", "Brett")   >> c = Course.new(u)     >> c.instructor.name => "Brett"   >> c.instructor.email => "brett@brettu.com" Pretty cool. With ActiveRecord associations on actual models you can use the this delegate create method links between associations.