`
hideto
  • 浏览: 2683650 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Advanced Ruby

    博客分类:
  • 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
分享到:
评论
1 楼 blackanger 2007-08-11  
ORUG的Tyler Hunt的《Advanced Ruby》
原文在哪?我到ORUG没有找到

相关推荐

    Head First Ruby.pdf

    You'll enter at Ruby's language basics and work through progressively advanced Ruby features such as classes, inheritance, and blocks. As your Ruby skills grow, you'll tackle deep topics such as ...

    Beginning.Ruby.From.Novice.to.Professional.3rd.Edition.1484212797

    Chapter 11: Advanced Ruby Features Chapter 12: Tying It Together: Developing a Larger Ruby Application Part 3: Ruby Online Chapter 13: Two Web Application Approaches: Rails and Sinatra Chapter 14: ...

    Rails Crash Course(No Starch, 2014)

    In Part II, you'll take your skills to the next level as you build a social networking app with more advanced Ruby tools, such as modules and metaprogramming, and advanced data modeling techniques ...

    The Ruby Programming Language

    - **Advanced Rails**:针对高级Ruby on Rails开发者的深入指南。 - **Rails Cookbook**:涵盖Ruby on Rails框架中的各种常见问题解决方案。 - **Ruby Pocket Reference**:提供Ruby语言核心特性的快速参考手册。 - ...

    Ruby-LockboxRuby和Rails的文件加密

    1. **加密算法支持**:Lockbox支持多种加密算法,如AES(Advanced Encryption Standard),RSA,ECB(Electronic Codebook),CBC(Cipher Block Chaining)等。AES是目前最常用的对称加密算法,而RSA则是一种非对称...

    Ruby-Sneakers一个基于RubyRabbitMQ的快速后台处理框架

    RabbitMQ是一种流行的开源消息代理,它实现了Advanced Message Queuing Protocol (AMQP)标准。它允许应用程序之间通过创建、发送、接收和存储消息来通信,这种解耦的方式使得系统更加健壮和灵活。 Sneakers的核心...

    Metaprogramming Ruby 2(Pragmatic,2014)

    Dig under the surface and explore Ruby's most advanced feature: a collection of techniques and tricks known as metaprogramming. In this book, you'll learn metaprogramming as an essential component of ...

    Enterprise Integration with Ruby

    1. **消息传递**:书中可能详细讲解了如何使用Ruby来构建消息传递系统,如通过AMQP(Advanced Message Queuing Protocol)或者RabbitMQ等中间件实现异步通信。消息队列在系统解耦和提高可扩展性方面扮演着重要角色。...

    the_ruby_programming_language原版

    O'Reilly旗下的资源还包括了《Ruby Cookbook》、《Learning Ruby》、《Advanced Rails》、《Rails Cookbook》和《Ruby on Rails: Up and Running》等,这些资源对于想要在Ruby领域深入学习的开发者来说,都是宝贵的...

    The Ruby Programming Language PDF

    5. 其他资源:文档提到了O'Reilly提供的其他相关资源,包括《Ruby Cookbook》、《Learning Ruby》、《Advanced Rails》、《Rails Cookbook》和《Ruby Pocket Reference》等。这些资源涵盖了从基础到高级的Ruby编程...

    Ruby-服务器优化的Ruby发行版通过APTYUM实现更少内存更快速易于安装和安全补丁

    "Ruby-服务器优化的Ruby发行版通过APTYUM实现更少内存更快速易于安装和安全补丁"这个主题就聚焦于这样的发行版,特别是如何通过APT(Advanced Package Tool)和YUM(Yellowdog Updater, Modified)这两个包管理器来...

    Advanced Rails

    "Advanced Rails" 涵盖了Rails开发中的高级主题和技术,是Ruby on Rails学习进阶的重要资源,尤其适合已经对基础Rails有一定了解的开发者。 在Web开发领域,Rails以其高效、简洁的代码和“约定优于配置”的哲学吸引...

    The Ruby Programming Language 2008 .pdf

    - **Advanced Rails**:专注于Ruby on Rails框架的高级用法和技术细节。 - **Rails Cookbook**:类似于Ruby Cookbook,但专门针对Rails框架。 - **Ruby Pocket Reference**:一本便携式的参考手册,涵盖了Ruby语言的...

    Eric is a full featured Python and Ruby editor and IDE,

    Eric is a full featured Python and Ruby editor and IDE, written in python. It is based on the cross platform Qt gui toolkit, integrating the highly flexible Scintilla editor control. It is designed to...

    Ruby Pocket Reference

    ### Ruby Pocket Reference Key Points **Title and Description Overview:** The "Ruby Pocket Reference" by Michael Fitzgerald is a concise and practical guide designed to serve as a quick reference for...

    ruby cookbook

    Beginners and advanced Rubyists alike will learn how to program with: &lt;br&gt; &lt;br&gt;Strings and numbers &lt;br&gt;Arrays and hashes &lt;br&gt;Classes, modules, and namespaces &lt;br&gt;Reflection and ...

Global site tag (gtag.js) - Google Analytics