Mac OS X 配置指南

Mac OS X 配置指南

  • Docs
  • Blog

notes/ruby

ruby

"2147483648" should a special number in ruby which leads to error.

Ruby 里的 Array#map里的 return

参见 - https://stackoverflow.com/questions/39350042/return-in-ruby-arraymap

引用:


Sergio's answer is very good, but it's worth pointing out that there is a keyword that works the way you wanted return to work: next.


Ruby 里的 Hash(Dictionary)

# you can do hash like this
grades = {"A" => 100, "B" => 90}

# or this
options = {:size => 10, :font-family => "arial"}

# or this
options = {size:10, font-family:"arial"}

# can be asceess as 
options[size] # 10
# A hash can also be created by new method
grades = Hash.new

if you want to add default value then you can use

grades = Hash.new(0)

or by using default= method

grades.default = 0

更进阶的使用请参考

  • http://docs.ruby-lang.org/en/2.0.0/Hash.html

Notes

  • scope 在 C 里面,'{}' 的内部可以看到外部的变量。但是在 ruby 里面,scope 之间是严格划分开的,参见下面的例子,local_variables 之间是严格分开的。

    • scope.rb:
v1 = 1

class myClass
  v2 = 2 
  local_variables # => [:v2]
  def my_method
    v3 = 3
    local_variables
  end
end

obj = Myclass.new
obj.my_method # => [:v3]
local_variables # => [:v1]

TIPS: 在《Meteprogramming Ruby》一书里面,有一个 Bug 就是这个机制造成的。

Spaceship Operators

<=>

a <=> b :=
  if a < b then return -1
  if a = b then return  0
  if a > b then return  1
  if a and b are not comparable then return nil
  • 参见 http://stackoverflow.com/questions/827649/what-is-the-ruby-spaceship-operator

Lambda, Block 和 Proc 的区别与联系

http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/

Quiz

  • Closure,block 以及 scope 的联系和区别是什么?

  • method_missing() 在 Ruby 里面有什么奇技淫巧?

  • 在 ruby 里实现一个 C# 里的 using 关键字(参见《元编程 Quiz:Ruby# 》)。

  • Flat Scope 是什么意思?Scope Gate 以及 Shared Scope 呢,它们分别是什么意思?

  • Dynamic Dispatch 是什么意思?

  • define_method 的用法? class.new()的用法? 如何使用二者以共享变量?

  • instance_eval()有什么使用技巧?

  • block 和 proc 有什么联系和区别?

  • 说一下hignline这个 gem 是怎么使用的呢?

  • callable object 一共有哪些?他们的联系和区别是什么?

  • instance_eval() 和 class_eval() 的使用有什么不同?

  • attr_accessor 的作用原理是什么?

Cheatsheat

if __name__ == '__main__' 在 Ruby 里是什么

if __FILE__ == $0
    foo()
    bar()
end

Ruby 里的取整

.round 四舍五入

.floor 向下取整

.ceil 向上取整

除法。在 ruby 里是向下取整。而在 C 里是向零取整。

参见:http://otnth.blogspot.hk/2009/05/negative-division-in-ruby.html

Implement a priority queue in Ruby

http://www.brianstorti.com/implementing-a-priority-queue-in-ruby/

Heap Datastructure in Ruby

https://www.sitepoint.com/heap-data-structure-ruby/

在代码里取得 Ruby 的版本号

"#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"

# or

RUBY_DESCRIPTION

参见:http://stackoverflow.com/questions/11819525/programmatically-getting-full-ruby-version

Print Format Cheatsheat

http://alvinalexander.com/programming/printf-format-cheat-sheet

Attribute Accessor

attr_accessor 的意思和用法

https://metova.com/a-beginners-guide-to-ruby-getters-and-setters/

gem install pg 错误

https://stackoverflow.com/questions/33557487/error-error-installing-pg-error-failed-to-build-gem-native-extension?rq=1

$ brew install postgres
Last updated on 2019-1-8 by wild-flame
Docs
Mac OS 配置指南 | Mac OS Setup Guide乱七八糟的笔记
Projects
Leetcode solutionsJekyll simple猪瘟疫情可视化
More
BlogGitHub
Copyright © 2019 wild-flame