nums = -1 .. 42 nums.min nums.max (0...32).max # note need parens nums.include?(20) nums.include?(90)
b.join(', ')
a.sort a a.sort! a
ruby -r debug gpa.rbThis starts program in debug mode
# a to-do item class Task def initialize(descr, duration) @descr = descr @duration = duration end # increase duration by a factor def scale(factor) @duration *= factor end def descr return @descr end def duration return @duration end end
class Task def to_s return @descr + " for " + @duration.to_s + " hr" end end
class DayLongTask < Task def initialize(descr) super(descr, 8.0) end def to_s super + " (all day)" end end
class Task attr_reader :descr, :duration attr_writer :duration def initialize(descr, duration) @descr = descr @duration = duration end # increase duration by a factor def scale(factor) @duration *= factor end def to_s return @descr + " for " + @duration.to_s + " hr" end end
attr_accessor :durationThis implies both a reader and a writer.
foreach (ItemType item: someArray) { item.print(); }
messed_up = [42, 'why?', ['avogadro', 6.02214086e23], nil]
nums = [-23, 44, 17, 2] nums.each { |x| puts x * x * x }
def do_twice yield yield end do_twice { puts "echo" }
def doit_again(n) for i in 1..n yield end end doit_again(10) { puts "bye" }
def doit_again(n) i = 1 while i <= n yield i i += 1 end end doit_again(5) { |x| puts x**x }
toself = proc { |x| x ** x } # proc signals don't eval block doit_again(8, &toself) # & specifies this param is the block (must be last)
(1..5).each { puts "hello" }
(1..5).each { |count| puts count }
words = ['fee', 'fi', 'fo', 'fum'] words.each { |it| puts it } words.each { |it| puts it.reverse }
words.find { |x| x.length == 2 }
words.map { |w| w + "-" + w.upcase } words words.map! { |w| w + "-" + w.upcase } words
words.select { |x| x.length > 5 }
words.map do |w| tmp = w.downcase puts "Lower case word: #{tmp}" endbut curly braces are better in some editors (you get matching) and are good enough for this course.
class Task attr_reader :descr, :duration attr_writer :duration def initialize(descr, duration) @descr = descr @duration = duration end # increase duration by a factor def scale(factor) @duration *= factor end def to_s return @descr + " for " + @duration.to_s + " hr" end end
color_of = { 'apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange', 'pear' => ['green', 'brown', 'yellow'], 'grape' => ['green', 'purple'] }
a = { 0 => 'tic', 1 => 'tac', 2 => 'toe' }
require 'set' xs = Set.new xs << 9 << 5 xs.include? 42
def add_scaled(a, b, c) a * c + b * c end add_scaled(3, 4, 5) add_scaled('x', 'y', 3)
require 'matrix' transform_a = Matrix[[0.5, 0.25, 1.0], [0.1, 0.1, 0.1], [0.9, 0.9, 0.9]] transform_b = Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]] add_scaled(transform_a, transform_b, 2.0)
// Java double ave(int[] nums) { ... } ... int x = (int)ave(xs); // reduced checking
5.class 5.class.class # also: Fixnum.class 5.class.class.class
Fixnum.instance_methods Fixnum.instance_methods.sort String.instance_methods.sort Fixnum.instance_methods false # not including superclasses
Class.methods
load 'revised-gpa.rb'to provide convert_to_numbers to other code
require 'open-uri' # use open-uri library home = URI.open("http://www.google.com") 5.times { puts home.gets }
gem install rails
ruby generate.rb 1.5 3.5 0.25
ruby generate.rb 1.5 3.5 0.25 | ruby sum.rb
ruby generate.rb -3
ruby generate.rb -3 >results.txt