Hi
1) How can I call a Proc, if a method with the same name exists?
2) How can I "undefine" variable(name), so defined?(var) == nil?
thank you
Opti
Hi
1) How can I call a Proc, if a method with the same name exists?
2) How can I "undefine" variable(name), so defined?(var) == nil?
thank you
Opti
1.
def foo
puts “method”
end
foo = -> { puts(“proc") }
foo() #=> “method”
foo.call #=> “proc”
foo #=> “proc”
Obligatory: But don’t actually do this! Shadowing a method name with a local variable is only going to confuse people!
2. As far as I know, there’s no way to undefine a local (or global) variables.
There's remove_class_variable <Class: Module (Ruby 3.0.2), remove_instance_variable <Class: Object (Ruby 3.0.2); and remove_const <Class: Module (Ruby 3.0.2); methods, but no equivalent for local variables.
However, the common solution here would be to use scopes - i.e. if your variable should only exist inside a certain context (scope), then simply don’t define it outside of that scope:
[1, 2, 3].each do |x|
y = x * 2
puts y
end
# x and y are not defined here!
On 3 Aug 2021, at 16:55, Die Optimisten <inform@die-optimisten.net> wrote:
Hi
1) How can I call a Proc, if a method with the same name exists?
2) How can I "undefine" variable(name), so defined?(var) == nil?
thank you
Opti
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
`undef` is a ruby keyword.
On Aug 3, 2021, at 09:08, Tom Lord <lord.thom@gmail.com> wrote:
1.
def foo
puts “method”
endfoo = -> { puts(“proc") }
foo() #=> “method”
foo.call #=> “proc”
foo #=> “proc”Obligatory: But don’t actually do this! Shadowing a method name with a local variable is only going to confuse people!
2. As far as I know, there’s no way to undefine a local (or global) variables.
There's remove_class_variable, remove_instance_variable and remove_const methods, but no equivalent for local variables.
However, the common solution here would be to use scopes - i.e. if your variable should only exist inside a certain context (scope), then simply don’t define it outside of that scope:
[1, 2, 3].each do |x|
y = x * 2
puts y
end
# x and y are not defined here!On 3 Aug 2021, at 16:55, Die Optimisten <inform@die-optimisten.net> wrote:
Hi
1) How can I call a Proc, if a method with the same name exists?
2) How can I "undefine" variable(name), so defined?(var) == nil?
thank you
Opti
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>