Well I have survived day 1 of Ruby in Seven Languages in Seven Days by Bruce Tate. Now I just need to figure out how to do everything I want to do in this blog software.
Finding the Ruby documentation was an easy Google search:
http://www.ruby-doc.org
And the free online version of Programming Ruby: The Pragmatic Programmer's Guide:
http://www.ruby-doc.org/docs/ProgrammingRuby/
I am running Ubuntu so I already have ruby installed. Then the question became what version of ruby do I have installed? The answer should have been obvious:
ray@stingray:~/tmp$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
Let's try to find a substitution method the hard way (i.e. without using the documentation):
irb(main):001:0> 'a string'.methods
=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :byteslice, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :<<, :prepend, :crypt, :intern, :to_sym, :ord, :include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr_s, :delete, :squeeze, :count, :tr!, :tr_s!, :delete!, :squeeze!, :each_line, :each_byte, :each_char, :each_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force_encoding, :valid_encoding?, :ascii_only?, :unpack, :encode, :encode!, :to_r, :to_c, :>, :>=, :<, :<=, :between?, :nil?, :!~, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main):002:0> 'a string'.sub
ArgumentError: wrong number of arguments (0 for 1..2)
from (irb):2:in `sub'
from (irb):2
from /usr/bin/irb:12:in `<main>'
irb(main):003:0> 'a string'.sub('a')
ArgumentError: wrong number of arguments (1 for 1..2)
from (irb):3:in `sub'
from (irb):3
from /usr/bin/irb:12:in `<main>'
irb(main):004:0> 'a string'.sub('a', 'b')
=> "b string"
irb(main):005:0>
OK, I'm a little confused by the arguments message (1 for 1..2) but it worked out.
The next interesting exercise is print your name ten times. I'm going to cheat and use the sneak peek at some Ruby code in section 2.1. Once I got the range syntax down it was no problem:
irb(main):011:0> (1..10).each {|x| puts 'your name'}
Running a ruby program from a file is easier in linux than Windows. (This probably works on a Mac as well.) Put the following at the beginning of the file:
#!/usr/bin/env ruby
Note: env is a nice little program that allows you not to need to know where ruby is installed on the particular machine. Although you do need to get the path to env right so maybe it doesn't buy you too much.
Then make the file executable with
chmod a+x
and you can run it right from the command line:
chmod a+x sentence.rb
./sentence.rb
OK, on to day two...
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.