Project

General

Profile

Actions

Feature #10571

closed

Keyword arguments with local variable

Feature #10571: Keyword arguments with local variable

Added by gogotanaka (Kazuki Tanaka) almost 11 years ago. Updated almost 11 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:66716]

Description

It's kind of curiosity. As far as I searched, I couldn't get sensible reason.
Please give me information if you have free time.

class MyClass
  var = 'test'
  def my_method(kwd_var: var)
    p kwd_var
  end
end

MyClass.new.my_method
#=> NameError: undefined local variable or method `var' for #<MyClass:0x007f91520a15c8>

I've expected above code is same as below code.

class MyClass
  def my_method(kwd_var: 'test')
    p kwd_var
  end
end

MyClass.new.my_method
#=> "test"

Take your time. Thanks.

Updated by gogotanaka (Kazuki Tanaka) almost 11 years ago Actions #1 [ruby-core:66717]

Ops.. not only Keyword arguments also other arguments with default value has same behavior.

I suppose there must be sensible reason for that, I need to look for more.. sorry for noisey then.

Updated by matz (Yukihiro Matsumoto) almost 11 years ago Actions #2 [ruby-core:66718]

var is a local variable in the class scope, which cannot be accessed from the method. a method has independent scope.

Matz.

Updated by matz (Yukihiro Matsumoto) almost 11 years ago Actions #3 [ruby-core:66719]

  • Status changed from Open to Closed

Updated by nobu (Nobuyoshi Nakada) almost 11 years ago Actions #4 [ruby-core:66722]

You can define a method shares the scope with the outside by define_method.

Updated by gogotanaka (Kazuki Tanaka) almost 11 years ago Actions #5 [ruby-core:66723]

@Yukihiro Matsumoto san
Thank you so much. It does make sense, I need to more deepen understanding your language.

@Nobuyoshi Nakada san
Thank you for your information. I think define_method, moreover block is really really awesome.

Sorry for the noise.

Actions

Also available in: PDF Atom