Skip to content

Instantly share code, notes, and snippets.

@osyo-manga
Last active November 6, 2019 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osyo-manga/f332ba1f31dbc3a437acd4d86d7986dc to your computer and use it in GitHub Desktop.
Save osyo-manga/f332ba1f31dbc3a437acd4d86d7986dc to your computer and use it in GitHub Desktop.

Example 1. Defined local variable name _1

# Example 1-1
# Warning : defined local variable
_1 = 42
# Example 1-2
# No warning : defined method name
def _1
  42
end
# Example 1-3
# No warning : defined parameter name
def hgoe(_1)
end
# Example 1-4
# In block
proc {
  # Warning: `_1' is used as Numbered parameter
  _1 = 42
}

# Example 1-5
# In block and used Numbered parameter
proc {
  _1
  # No warning
  _1 = 42
}

Example 2. Refer to local variable _1 in block

_1 = :local_variable

# Example 2-1
# No warning
x = _1
p x
# => :local_variable

# Example 2-2
# No warning
p proc { |i| _1 }.call 3
# => :local_variable

# Example 2-3
# No warning
p proc { _1 }.call 3
# => :local_variable

# Example 2-4
p proc {
  _1
  proc {
    # No warning
    _1
  }.call
}.call 42
# => :local_variable

Example 3. Refer to method _1 in block

def _1(a = nil)
  :"method#{a}"
end

# Example 3-1
# No warning
x = _1
p x
# => :method

# Example 3-2
# No warning
x = _1 42
p x
# => :method42

# Example 3-3
# No warning
x = _1()
p x
# => :method

# Example 3-4
# Error
p proc { |i| _1 }.call 42

# Example 3-5
# No warning
p proc { |i| _1() }.call 42
# => :method

# Example 3-6
# No warning
p proc { |i| _1 i }.call 42
# => :method42

# Example 3-7
# No warning
p proc { |i| self._1 }.call 42
# => :method

# Example 3-8
# No warning
p proc { _1 }.call 42
# => 42

# Example 3-9
# No warning
p proc { _1 _2 }.call 42, 3
# => method3

# Example 3-10
# No warning
p proc { _1() }.call 42
# => :method

# Example 3-11
# No warning
p proc { self._1 }.call 42
# => :method

Example 4. Defined local variable _1 and method _1

def _1(a = nil)
  :"method#{a}"
end

_1 = :local_variable

# Example 4-1
# No warning
p proc { |i| _1 }.call 3
# => :local_variable

# Example 4-2
# No warning
p proc { |i| _1() }.call 3
# => :method

# Example 4-3
# No warning
p proc { |i| _1 42 }.call 3
# => :method42

# Example 4-4
# No warning
p proc { _1 }.call 3
# => :local_variable

# Example 4-5
# No warning
p proc { _1() }.call 3
# => :method

# Example 4-6
# No warning
p proc { _1 42 }.call 3
# => :method42

Example 5. Used eval("_1")

  • eval ("_1") refers to local variables in preference
    • The same goes for binding.local_variable_get(:_1)
  • If local variable _1 becomes Error, solve it?
  • eval("@1") is Syntax Error
# Example 5-1
# Error: numbered parameter outside block (SyntaxError)
p proc { eval("_1") }.call 42
# Example 5-2
# No warning
p proc { _1; eval("_1") }.call 42
# => 42
_1 = :local_variable

# Example 5-3
# No warning
p eval("_1")
# => :local_variable

# Example 5-4
# No warning
p proc { eval("_1") }.call 42
# => :local_variable

# Example 5-5
# No warning
p proc { _1; eval("_1") }.call 42
# => :local_variable
# Example 5-6
p proc {
  _1
  # Error: numbered parameter is already used in
  #        outer block here
  proc { _1 }.call 3
}.call 42
# Example 5-7
p proc {
  _1
  proc {
    # No warning
    eval("_1")
  }.call 3
}.call 42
# => 42
# Example 5-8
_1 = :local_variable
p proc {
  _1
  proc {
    # No warning
    eval("_1")
  }.call 3
}.call 42
# => :local_variable
def _1; :method; end

# Example 5-9
# No warning
p _1
# => :method

# Example 5-10
# Error: numbered parameter outside block (SyntaxError)
p eval("_1")

# Example 5-11
# No warning
p eval("_1()")
# => :method

# Example 5-12
# Error: numbered parameter outside block (SyntaxError)
p proc { eval("_1") }.call 42

# Example 5-13
# No warning
p proc { _1; eval("_1") }.call 42
# => 42

Example 6. Used default arguments

# Example 6-1
# OK
p proc { |_1|
  -> (a = _1) { a }.call
}.call 42
# => 42
# Example 6-2
# OK
_1 = :local_variable
p proc {
  -> (a = _1) { a }.call
}.call 42
# => :local_variable
# OK
def _1; :method; end

p proc {
  _1
  eval("_1")
}.call 42
# => 42
# Example 6-3
# NG
p proc {
  # Error: ordinary parameter is defined
  -> (a = _1) { a }.call
}.call 42
# Example 6-4
# OK
p proc { |_1|
  -> (a = _1) { a }.call
}.call 42
# => 42
# Example 6-5
# OK
_1 = :local_variable
p proc {
  -> (a = _1) { a }.call
}.call 42
# => :local_variable
# Example 6-6
# NG
p proc {
  # Error: ordinary parameter is defined
  -> (a = _1) { a }.call
}.call 42

Example 7. Refrer to _1 in binding.irb

# Example 7-1
proc {
  _1
  # Referencing _1 returns 42
  binding.irb
}.call 42
# Example 7-2
_1 = :local_variable
proc {
  _1
  # Referencing _1 returns :local_variable
  binding.irb
}.call 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment