Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between alias foo bar and just foo = bar

Tags:

ruby

While learning ruby with tutorial on rubylearning i got to 'using alias' part of it. I cant understand what is the difference between using alias like in example:

def oldmtd  
  "old method"  
end  
alias newmtd oldmtd  
def oldmtd  
  "old improved method"  
end  
puts oldmtd  
puts newmtd

with output

old improved method
old method

and just assigning a new variable to this function, like:

def oldmtd  
  "old method"  
end  
newmtd = oldmtd  
def oldmtd  
  "old improved method"  
end  
puts oldmtd  
puts newmtd

with the same output:

old improved method
old method

Please, tell me what is actual difference and when it is correct to use 'alias'?

like image 777
Павел Кайгородов Avatar asked Jan 26 '23 14:01

Павел Кайгородов


1 Answers

With newmtd = oldmtd you are not assigning a new variable to a function; you are assigning a variable to the result of a function, that is, a string. In Python terms: newmtd = oldmtd()

like image 89
steenslag Avatar answered Jan 31 '23 10:01

steenslag