Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Kernel#local_variables entries with block local parameters

Tags:

ruby

I am using this version of Ruby on Arch Linux. I also tried the first code snippet in ruby 1.9, which had the same results.

ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]

uname -a
Linux ryantm0j132 3.12.7-2-ARCH #1 SMP PREEMPT Sun Jan 12 13:09:09 CET 2014 x86_64 GNU/Linux

These three snippets below are separate programs.

When I use block local variables that shadow a variable the local_variables array contains 3 entries:

a = 1
puts local_variables.inspect #=> [:a]
proc { |;a|
  puts local_variables.inspect #=> [:a,:a,:a]
}.call

If I don't shadow, anything it contains 1 entry:

puts local_variables.inspect #=> []
proc { |;b|
  puts local_variables.inspect #=> [:b]
}.call

Another example of the block local variable not shadowing anything:

a = 1
puts local_variables.inspect #=> [:a]
proc { |;b|
  puts local_variables.inspect #=> [:b,:a]
}.call

Is there some reason for these extra entries in the first case? Is it a bug in ruby?

like image 638
ryantm Avatar asked Jan 29 '14 06:01

ryantm


People also ask

What is multi kernel svm?

Multiple kernel learning refers to a set of machine learning methods that use a predefined set of kernels and learn an optimal linear or non-linear combination of kernels as part of the algorithm.

What is a kernel in ML?

In machine learning, a “kernel” is usually used to refer to the kernel trick, a method of using a linear classifier to solve a non-linear problem. It entails transforming linearly inseparable data like (Fig. 3) to linearly separable ones (Fig. 2).


1 Answers

It looks like I finally got why there are three of them. That’s out of my competence to decide whether this is a bug.

Let’s take a look at the bindings:

b1 = binding
a = 1
puts proc { |b2=binding; a| 
  a = 3
  "T: #{b1}, B: #{b2}, L: #{binding}\n" + 
  "TV: #{b1.eval('a')}, BV: #{b2.eval('a')}, LV: #{binding.eval('a')}" 
}.call
# ⇒ T: #<Binding:0x0000000294ef88>,
# ⇒ B: #<Binding:0x0000000294de58>,
# ⇒ L: #<Binding:0x0000000294dd68>
# ⇒ T: 1, B: 3, L: 3

It seems there are three Binding objects, each having the local variable name added to the list if and only it was shadowed. Binding b2, though it is a separate instance, has affected by a = 3 setting.

Probably it was made to simplify the local_variables discounting.

like image 80
Aleksei Matiushkin Avatar answered Nov 15 '22 19:11

Aleksei Matiushkin