1、rails gems
cd /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record
#这个路径因你的ruby安装路径不同而不同
ls
aggregations.rb dynamic_finder_match.rb schema_dumper.rb
association_preload.rb dynamic_scope_match.rb schema.rb
associations fixtures.rb serialization.rb
associations.rb i18n_interpolation_deprecation.rb serializers
attribute_methods.rb locale session_store.rb
autosave_association.rb locking test_case.rb
base.rb migration.rb timestamp.rb
batches.rb named_scope.rb transactions.rb
calculations.rb nested_attributes.rb validations.rb
callbacks.rb observer.rb version.rb
connection_adapters query_cache.rb
dirty.rb reflection.rb
利用 active_record/ fixtures.rb提供的api创建加载数据到数据库的任务 Rails.root/Rakefile:
namespace :db do
desc "Load seed fixtures into the database."
task :seed => :environment do
require 'active_record/fixtures' # import active_record/ fixtures.rb
Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do |file|
Fixtures .create_fixtures('db/fixtures', File.basename(file, '.*'))
end
end
end
2、ruby libraries
cd /home/simon/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8
ls
abbrev.rb erb.rb mathn.rb profiler.rb soap
base64.rb eregex.rb matrix.rb pstore.rb sync.rb
benchmark.rb expect.rb md5.rb racc tempfile.rb
bigdecimal fileutils.rb mkmf.rb rational.rb test
cgi finalize.rb monitor.rb rdoc thread.rb
cgi-lib.rb find.rb mutex_m.rb readbytes.rb thwait.rb
cgi.rb forwardable.rb net resolv.rb timeout.rb
complex.rb ftools.rb observer.rb resolv-replace.rb time.rb
csv.rb
generator.rb open3.rb rexml tmpdir.rb
date getoptlong.rb openssl rinda tracer.rb
date2.rb getopts.rb openssl.rb rss tsort.rb
date.rb gserver.rb open-uri.rb rss.rb un.rb
debug.rb i686-linux optparse rubyunit.rb uri
delegate.rb importenv.rb optparse.rb runit uri.rb
digest io ostruct.rb scanf.rb weakref.rb
digest.rb ipaddr.rb parsearg.rb securerandom.rb webrick
dl irb parsedate.rb set.rb webrick.rb
drb irb.rb pathname.rb sha1.rb wsdl
drb.rb jcode.rb ping.rb shell xmlrpc
e2mmap.rb kconv.rb pp.rb shell.rb xsd
English.rb logger.rb prettyprint.rb shellwords.rb yaml
Env.rb mailread.rb profile.rb singleton.rb yaml.rb
class CardsController < ApplicationController
def batch_create
require 'csv'
parse_line = CSV::Reader.parse(params[:cards_text])
parse_line.each do |row|
Card.create(:term => row[0].to_s.strip, :definition => row[1].to_s.strip, :collection_id => params[:collection_id], :user_id => current_user.id)
end
redirect_to collection_cards_path(params[:collection_id])
end
end
class Card < ActiveRecord::Base
def self.parse_csv(csv_file_path, collection_id, user_id)
require 'csv'
CSV::Reader.parse(File.open(csv_file_path, "r"))
do |line|
c = Card.find_or_create_by_term_and_collection_id(line[0], collection_id)
c.update_attributes(:definition => line[1], :user_id => user_id)
end
end
end
这里只是举几个例子,要合理利用ruby built in libraries。如果你要用ruby来实现多线程,mutex_m.rb 或 monitor.rb 可以直接拿来用
查看rubygems源码,有许多思想值得借鉴
/home/simon/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/site_ruby/1.8/
#cd rubygems/
#ls
builder.rb gemcutter_utilities.rb security.rb
command_manager.rb gem_openssl.rb server.rb
command.rb gem_path_searcher.rb source_index.rb
commands gem_runner.rb source_info_cache_entry.rb
config_file.rb indexer.rb source_info_cache.rb
custom_require.rb installer.rb spec_fetcher.rb
defaults.rb install_update_options.rb specification.rb
dependency_installer.rb local_remote_options.rb test_utilities.rb
dependency_list.rb old_format.rb text.rb
dependency.rb package uninstaller.rb
doc_manager.rb package.rb user_interaction.rb
errors.rb package_task.rb validator.rb
exceptions.rb platform.rb version_option.rb
ext remote_fetcher.rb version.rb
ext.rb requirement.rb
format.rb require_paths_builder.rb