Advanced Ruby

本文节选自ORUG的Tyler Hunt的《Advanced Ruby》

Syntax Sugar
if not version.empty?
  return version.gsub('_', '.')
end

unless version.empty?
  return version.gsub('_', '.')
end

return if version.valid?

return if not version.valid?

return unless version.valid?

if person.xy?
  gender = 'M'
else
  gender = 'F'
end

gender = person.xy? ? 'M' : 'F'

for item in items
  puts item.to_s
end

items.each do |item|
  puts item.to_s
end

for i in 1..3
  quantity = gets.chomp.to_i

  next if quantity.zero?
  redo if quantity > LIMIT
  retry if quantity == RESTART
  break if quantity == FINISHED

  quantities[i] = quantity
end

(1..5).collect { |i| i * i }
# [1, 4, 9, 16, 25]
(1..5).detect { |i| i % 2 == 0 }
# 2
(1..5).select { |i| i % 2 == 0 }
# [2, 4]
(1..5).reject { |i| i % 2 == 0 }
# [1, 3, 5]
(1..5).inject { |sum, n| sum + n }
# 15


Alternative Syntax
puts '"Hello World!"\n'
  # "Hello World!"\n
puts "\"Hello World!\"\n"
  # "Hello World!"
puts %q("Hello World!"\n)
  # "Hello World!"\n
puts %Q("Hello World!"\n)
  # "Hello World!"
puts %!"Hello World\!"\n!
  # "Hello World!"

exec('ls *.rb')
system('ls *.rb')
`ls *.rb`
%x(ls *.rb)

system('echo *')
system('echo', '*')

/[ \t]+$/i

%r([ \t]+$)i

Regexp.new(
  "[ \t]+$",
  Regexp::IGNORECASE
)

["one", "two", "three"]

%w(one two three)

1..5
Range.new(1, 5)
  # (1..5).to_a == [1, 2, 3, 4, 5]

1...5
Range.new(1, 5, true)
  # (1...5).to_a == [1, 2, 3, 4]


Variables
$global_variable

@instance_variable

@@class_variable

CONSTANT

local_variable


Blocks & Closures
with_block a, b {
  ...
}
# with_block(a, b { ... })

with_block a, b do
  ...
end
# with_block(a, b) { ... }

puts begin
  t = Time.new
  t.to_s
end

def capitalizer(value)
  yield value.capitalize
end

capitalizer('ruby') do |language|
  puts language
end

def capitalizer(value)
  value = value.capitalize

  if block_given?
    yield value
  else
    value
  end
end

def capitalizer(value)
  yield value.capitalize
end

def capitalizer(value, &block)
  yield value.capitalize
end

proc_adder = Proc.new {
  return i + i
}

lambda_adder = lambda {
  return i + i
}

visits = 0

visit = lambda { |i| visits += i }

visit.call 3


Exceptions
begin
  # code
rescue AnError
  # handle exception
rescue AnotherError
  # handle exception
else
  # only if there were no exceptions
ensure
  # always, regardless of exceptions
end

def connect
  @handle = Connection.new
rescue
  raise ConnectionError
end


Classes
class Person
  attr_accessor :name

  def initialize(first, last)
    name = first + ' ' + last
  end
end

person = Person.new('Alex', 'Chin')

class Person
  attr_accessor :name

  class Address
    attr_accessor :address, :zip
  end
end

address = Person::Address.new

class Person
  attr_accessor :name
end

class Student < Person
  attr_accessor :gpa
end

student = Student.new
student.name = 'Alex Chin'


Modules
module Demographics
  attr_accessor :dob

  def age
    (Date.today - dob).to_i / 365
  end
end

class Person
  include Demographics
end

person = Person.new
person.extend Demographics

module Demographics
  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def years_since(date)
      (Date.today - date).to_i / 365
    end
  end
end

你可能感兴趣的:(F#,Ruby)