# About variable name and method name

**URL:** <https://www.ruby-forum.com/t/about-variable-name-and-method-name/181978>\
**Category:** Ruby\
**Created:** [January 21, 2010, 3:44am UTC](https://www.ruby-forum.com/t/about-variable-name-and-method-name/181978 "2010-01-21T03:44:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 21, 2010, 3:44am UTC](https://www.ruby-forum.com/t/about-variable-name-and-method-name/181978/1 "2010-01-21T03:44:34Z")

</div>

class Array  
def atoh  
hash = Hash.new  
if size % 2 != 0  
raise “size must be odd”  
end

```
 0.step(size-1,2) do |c| hash[self[c]] = self[c+1] end
 hash

```

end  
end

For “size” in the code above, we know it’s a self.size method actually.  
But how ruby differ it’s a variable or an instance method?

Thanks.

---

<div class="post-metadata">

**Author:** ![rubynut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e8cbc/32.png) [@rubynut](https://www.ruby-forum.com/u/rubynut)\
**Post date:** [January 21, 2010, 9:43am UTC](https://www.ruby-forum.com/t/about-variable-name-and-method-name/181978/2 "2010-01-21T09:43:54Z")

</div>

2010/1/21 Ruby N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end  
> You are aware that there is Hash[] already, are you?

irb(main):004:0\> a=%w{foo bar baz buz}  
=\> [“foo”, “bar”, “baz”, “buz”]  
irb(main):005:0\> Hash[\*a]  
=\> {“foo”=\>“bar”, “baz”=\>“buz”}

Btw, you can shorten your method by using #each\_slice. I would do it  
like this:

irb(main):008:0\> module Enumerable  
irb(main):009:1\> def to\_hash  
irb(main):010:2\> h={}  
irb(main):011:2\> each\_slice(2) {|k,v| raise “size must be odd” if  
v.nil?;h[k]=v}  
irb(main):012:2\> h  
irb(main):013:2\> end  
irb(main):014:1\> end  
=\> nil  
irb(main):015:0\> a.to\_hash  
=\> {“foo”=\>“bar”, “baz”=\>“buz”}  
irb(main):016:0\> a[0…-1].to\_hash  
RuntimeError: size must be odd  
from (irb):11:in `block in to_hash' from (irb):11:in `each\_slice’  
from (irb):11:in `to_hash' from (irb):16 from /opt/bin/irb19:12:in `’  
irb(main):017:0\>

Disadvantage: it is not as safe as your solution because it will  
detect an error even for Arrays with even number of elements if any  
value is nil.

Advantage: it works for all Enumerables.

Synthesis:

module Enumerable  
def to\_hash  
h = {}  
key = flag = nil

```
each do |val|
  if flag
    h[key] = val
  else
    key = val
  end
  flag = !flag
end

raise 'odd!' if flag
h

```

end  
end

> For “size” in the code above, we know it’s a self.size method actually.  
> But how ruby differ it’s a variable or an instance method?

As soon as Ruby has seen an assignment it assumes a local variable. Try  
this:

ruby -e ‘def f;123;end;def g;p f;f=0;p f;end;g’

More details can be found here:  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UO)

Kind regards

robert
