---
title: "Ruby的一些疑问"
author: "biyeah"
date: "2012-01-26T01:01:29Z"
tags: []
categories: ["编程语言"]
slug: "ruby"
draft: false
lastmod: "2012-02-05T10:44:01Z"
id: 1374785
url: "https://biyeah.iteye.com/blog/1374785"
---

1、网点看到一断程序，

```ruby
def m1(a)
	puts 'invoke m1'
	puts a
end

def self.m1(a)
	puts 'invoke self.m1'
	a = 20
	super
end

m1 10
```


运行结果如下：
invoke self.m1
invoke m1
20

为何先运行self.m1，这里为何可以使用super?还弄不明白，先记下来。
 2、下面的代码在rails源代码中看到的，不明白&:to_s的写法。

```ruby
(1..10).map(&:to_s)
```


3、
def who
  person = "Matz"
  yield("rocks")
  # p city #=>undefined local variable or method `city'
end
 
person = "Matsumoto"
 
who do |y|
  puts("#{person}, #{y} the world") # => Matsumoto, rocks the world
  city = "Tokyo"
  person = 'new'
end

puts person #=> new

4、＃nokogiri css.rb源码
require 'nokogiri/css/xpath_visitor'
x = $-w  ＃<--表示什么意思?
$-w = false
require 'nokogiri/css/parser'
$-w = x

5、
class H < Hash
    # Gets or sets keys in the hash.
    #
    #   @cookies.my_favorite = :macadamian
    #   @cookies.my_favorite
    #   => :macadamian
    #
    def method_missing(m,*a)
        m.to_s=~/=$/?self=a:a==?self:super
    end
    undef id, type if ?? == 63 #这里的??与63表示什么意思？
  end