Ruby
From Sigmon
see Rails also
Contents |
True Object Oriented terminologies
- everythings an object
- metaclasses
- closures
- interators
- ubiquious heterogenous collections
More Ruby
Debugging
require 'ruby-debug'
call this in code smoewhere to enter debugger..
debugger
more commands:
show list var class var const var global var instance var local
Learning Ruby
- Left off at "Control Structures" here: http://www.rubycentral.com/book/intro.html
- http://www.zenspider.com/Languages/Ruby/QuickRef.html#17
- Class and Library Reference, http://www.rubycentral.com/book/builtins.html
shell commands
- 'irb' - interactive ruby
- 'ri' - ruby object/documententation search tool
books
- currently reading, http://www.rubycentral.com/pickaxe/tut_containers.html
ruby keywords / methods to remember
- private, protected, public
- caller
- class
- module
- self
- method_missing
- delete_method
- undef_method
- alias
- alias_method
- lambda
- respond_to? instance method
- kind_of? instance method
- instance_of? instance method
- is_a?
- super
- binding
- instance_eval
- class_eval
- module_eval
- block_given?
- yield
- attr
- attr_reader
- attr_writer
- attr_accessor
- throw/catch
- raise/rescue
- local_variables
- singleton_methods
- eval note eval is 2.3x slower than send or call
- Module#method_added
- Kernel::singleton_method_added
- Class#inherited
- Module#extend_object
- set_trace_func proc { |event, file, line, id, binding, classname| printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname }
- trace_var
- global_variables
interesting classes to read help on
Read http://www.rubycentral.com/pickaxe/builtins.html for a complete list
- Kernel
- Class
- Module
- Object
- ObjectSpace
- Enumerable
- IO
- Math
- File
- Dir
- Regexp
- Process
explore
Object.methods Object.instance_methods Object.private_methods Object.public_methods Object.protected_methods Object.private_instance_methods Object.public_instance_methods Object.protected_instance_methods Object.singleton_methods
data type classes
- Symbol
- String
- Integer
- Float
- Fixnum
- Bignum
- Array
- Hash
- TrueClass/FalseClass
- NilClass
core ruby
sig@sig-dom0:/usr/lib/ruby/1.8$ ls abbrev.rb cmdparse dl forwardable.rb lib ncurses.rb pp.rb redcloth set.rb thwait.rb xmlrpc augeas.rb cmdparse2.rb drb ftools.rb log4r net prettyprint.rb redcloth.rb sha1.rb timeout.rb xmlsimple.rb base64.rb complex.rb drb.rb generator.rb log4r.rb observer.rb profile.rb resolv.rb shell time.rb xsd benchmark.rb csv.rb e2mmap.rb getoptlong.rb logger.rb open3.rb profiler.rb resolv-replace.rb shell.rb tmpdir.rb yaml bigdecimal daemonize.rb English.rb getopts.rb mailread.rb openssl pstore.rb rexml shellwords.rb tracer.rb yaml.rb binding_of_caller.rb daemons Env.rb gserver.rb mathn.rb openssl.rb puppet rinda singleton.rb tsort.rb blankslate.rb daemons.rb erb.rb i486-linux matrix.rb open-uri.rb puppet.rb rss soap ubygems.rb breakpoint_client.rb date eregex.rb importenv.rb md5.rb optparse racc rss.rb sqlite3 un.rb breakpoint.rb date2.rb expect.rb io mkmf.rb optparse.rb rake rubygems sqlite3.rb uri builder date.rb facter ipaddr.rb mocha ostruct.rb rake.rb rubygems.rb stubba.rb uri.rb builder.rb debug.rb facter.rb irb mocha.rb parsearg.rb rational.rb rubyunit.rb sync.rb weakref.rb cgi delegate.rb fileutils.rb irb.rb mocha_standalone.rb parsedate.rb rbconfig runit tempfile.rb webrick cgi-lib.rb digest finalize.rb jcode.rb monitor.rb pathname.rb rdoc scanf.rb test webrick.rb cgi.rb digest.rb find.rb kconv.rb mutex_m.rb ping.rb readbytes.rb securerandom.rb thread.rb wsdl
Exploring objects
irb(main):001:0> s = "This is a string" => "This is a string" irb(main):002:0> s.methods => ["methods", "instance_eval", "%", "rindex", "map", "<<", "split", "any?", "dup", "sort", "strip", "size", "instance_variables", "downcase", "min", "gsub!", "count", "include?", "succ!", "instance_of?", "extend", "downcase!", "intern", "squeeze!", "eql?", "*", "next", "find_all", "each", "rstrip!", "each_line", "+", "id", "sub", "slice!", "hash", "singleton_methods", "tr", "replace", "inject", "reverse", "taint", "sort_by", "unpack", "lstrip", "frozen?", "instance_variable_get", "capitalize", "max", "chop!", "method", "kind_of?", "capitalize!", "scan", "select", "to_a", "display", "each_byte", "type", "casecmp", "gsub", "protected_methods", "empty?", "to_str", "partition", "tr_s", "tr!", "match", "grep", "rstrip", "to_sym", "instance_variable_set", "next!", "swapcase", "chomp!", "is_a?", "swapcase!", "ljust", "respond_to?", "between?", "reject", "to_s", "upto", "hex", "sum", "class", "object_id", "reverse!", "chop", "<=>", "insert", "<", "tainted?", "private_methods", "==", "delete", "dump", "===", "__id__", "member?", "tr_s!", ">", "concat", "nil?", "untaint", "succ", "find", "strip!", "each_with_index", ">=", "to_i", "rjust", "<=", "send", "index", "collect", "inspect", "slice", "oct", "all?", "clone", "length", "entries", "chomp", "=~", "public_methods", "upcase", "sub!", "squeeze", "__send__", "upcase!", "crypt", "delete!", "equal?", "freeze", "detect", "zip", "[]", "lstrip!", "center", "[]=", "to_f"] irb(main):004:0> s.methods.grep /each/ => ["each", "each_line", "each_byte", "each_with_index"]
Common Tasks
- a string, s = "foobar"
- a hash h = {}, h[a] = 1
- a constant is all caps
- get a objects methods with puts obj.methods
- inspect a object with puts obj.inspect
- call obj.class to see the class it is
- load a yaml file with YAML.load() or .load_file
- ARGV is ARGV minus $0
- require 'pp' for printing complex objects