Ruby alias alias_method confusion












1















I have this piece of code here:



class MyFile
attr_accessor :byte_content

alias_method :read, :byte_content
alias_method :write, :byte_content=
end


On the class MyFile there is an alias_method, but I do not understand how this method is working. Does it change the way the getter and setter for :byte_content are called?



Also, what is the difference between this method and just using alias? When should I one over the other?



Thanks in advance for your time!










share|improve this question




















  • 2





    It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

    – Jörg W Mittag
    Jan 2 at 12:32













  • Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

    – Luiz Melo
    Jan 2 at 13:17













  • Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

    – Cary Swoveland
    Jan 2 at 20:52
















1















I have this piece of code here:



class MyFile
attr_accessor :byte_content

alias_method :read, :byte_content
alias_method :write, :byte_content=
end


On the class MyFile there is an alias_method, but I do not understand how this method is working. Does it change the way the getter and setter for :byte_content are called?



Also, what is the difference between this method and just using alias? When should I one over the other?



Thanks in advance for your time!










share|improve this question




















  • 2





    It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

    – Jörg W Mittag
    Jan 2 at 12:32













  • Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

    – Luiz Melo
    Jan 2 at 13:17













  • Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

    – Cary Swoveland
    Jan 2 at 20:52














1












1








1








I have this piece of code here:



class MyFile
attr_accessor :byte_content

alias_method :read, :byte_content
alias_method :write, :byte_content=
end


On the class MyFile there is an alias_method, but I do not understand how this method is working. Does it change the way the getter and setter for :byte_content are called?



Also, what is the difference between this method and just using alias? When should I one over the other?



Thanks in advance for your time!










share|improve this question
















I have this piece of code here:



class MyFile
attr_accessor :byte_content

alias_method :read, :byte_content
alias_method :write, :byte_content=
end


On the class MyFile there is an alias_method, but I do not understand how this method is working. Does it change the way the getter and setter for :byte_content are called?



Also, what is the difference between this method and just using alias? When should I one over the other?



Thanks in advance for your time!







ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 12:47









Kimmo Lehto

4,20311329




4,20311329










asked Jan 2 at 12:20









Luiz MeloLuiz Melo

2216




2216








  • 2





    It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

    – Jörg W Mittag
    Jan 2 at 12:32













  • Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

    – Luiz Melo
    Jan 2 at 13:17













  • Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

    – Cary Swoveland
    Jan 2 at 20:52














  • 2





    It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

    – Jörg W Mittag
    Jan 2 at 12:32













  • Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

    – Luiz Melo
    Jan 2 at 13:17













  • Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

    – Cary Swoveland
    Jan 2 at 20:52








2




2





It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

– Jörg W Mittag
Jan 2 at 12:32







It would be helpful, if you could explain precisely what is unclear to you about the documentation of Module#alias_method. That way, the Ruby developers can improve the documentation so that future developers don't have the same problems you have. Also, it saves time and effort for people answering your question, so that they don't repeat things you already know, or repeat explanations that you don't understand. And lastly, it would help if you can check that your Minimal, Complete, and Verifiable example really is minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines?

– Jörg W Mittag
Jan 2 at 12:32















Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

– Luiz Melo
Jan 2 at 13:17







Thank you for your input, I will certainly keep this in mind in the future. I got confused about when to use one or the other regarding lexical scope, which I did not see in the documentation (there is only one example and it is not clear at all). I found this blog post about the topic and found it to be really helpful : blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html My intention is to help other beginners (like myself) that face the same doubt in the future.

– Luiz Melo
Jan 2 at 13:17















Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

– Cary Swoveland
Jan 2 at 20:52





Google "Ruby alais vs. alais method" and you will get plenty of hits that will tell you all you need to know about that, and much more.

– Cary Swoveland
Jan 2 at 20:52












1 Answer
1






active

oldest

votes


















3














This:



attr_accessor :byte_content


Essentially does:



def byte_content
@byte_content
end

def byte_content=(value)
@byte_content = value
end


Then



alias_method :read, :byte_content
alias_method :write, :byte_content=


will create copies of the byte_content and byte_content= methods with read and write as the new names . If you modify the byte_content or byte_content= methods after the aliasing, the read and write will remain unchanged.



So, the intention of the alias_method in the example appears to be to give the user a more "File-like" interface. Without the method aliases, you would use:



file = MyFile.new
file.byte_content = "abcd"
puts file.byte_content


With the aliases, the user can use:



file = MyFile.new
file.write("abcd")
file.read


Which is quite similar to the methods of the standard library File class,



file = File.open('/tmp/foo.txt', 'w')
file.write('hello')
file.rewind
puts file.read


And this makes it possible to use instances of MyFile in place of real File instances in some very simple use-cases, also known as Duck-typing.




alias vs alias_method



The differences of alias and alias_method are described in the answers to this question, most imporantly:




  • you can override alias_method but not alias


  • alias_method is used with symbols as the method names, making it useful for dynamically created methods






share|improve this answer


























  • The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

    – vise
    Jan 2 at 13:03






  • 1





    @vise Updated the answer to mention the copying.

    – Kimmo Lehto
    Jan 2 at 13:10











  • That was extremely helpful. Thank you!

    – Luiz Melo
    Jan 2 at 13:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006295%2fruby-alias-alias-method-confusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














This:



attr_accessor :byte_content


Essentially does:



def byte_content
@byte_content
end

def byte_content=(value)
@byte_content = value
end


Then



alias_method :read, :byte_content
alias_method :write, :byte_content=


will create copies of the byte_content and byte_content= methods with read and write as the new names . If you modify the byte_content or byte_content= methods after the aliasing, the read and write will remain unchanged.



So, the intention of the alias_method in the example appears to be to give the user a more "File-like" interface. Without the method aliases, you would use:



file = MyFile.new
file.byte_content = "abcd"
puts file.byte_content


With the aliases, the user can use:



file = MyFile.new
file.write("abcd")
file.read


Which is quite similar to the methods of the standard library File class,



file = File.open('/tmp/foo.txt', 'w')
file.write('hello')
file.rewind
puts file.read


And this makes it possible to use instances of MyFile in place of real File instances in some very simple use-cases, also known as Duck-typing.




alias vs alias_method



The differences of alias and alias_method are described in the answers to this question, most imporantly:




  • you can override alias_method but not alias


  • alias_method is used with symbols as the method names, making it useful for dynamically created methods






share|improve this answer


























  • The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

    – vise
    Jan 2 at 13:03






  • 1





    @vise Updated the answer to mention the copying.

    – Kimmo Lehto
    Jan 2 at 13:10











  • That was extremely helpful. Thank you!

    – Luiz Melo
    Jan 2 at 13:20
















3














This:



attr_accessor :byte_content


Essentially does:



def byte_content
@byte_content
end

def byte_content=(value)
@byte_content = value
end


Then



alias_method :read, :byte_content
alias_method :write, :byte_content=


will create copies of the byte_content and byte_content= methods with read and write as the new names . If you modify the byte_content or byte_content= methods after the aliasing, the read and write will remain unchanged.



So, the intention of the alias_method in the example appears to be to give the user a more "File-like" interface. Without the method aliases, you would use:



file = MyFile.new
file.byte_content = "abcd"
puts file.byte_content


With the aliases, the user can use:



file = MyFile.new
file.write("abcd")
file.read


Which is quite similar to the methods of the standard library File class,



file = File.open('/tmp/foo.txt', 'w')
file.write('hello')
file.rewind
puts file.read


And this makes it possible to use instances of MyFile in place of real File instances in some very simple use-cases, also known as Duck-typing.




alias vs alias_method



The differences of alias and alias_method are described in the answers to this question, most imporantly:




  • you can override alias_method but not alias


  • alias_method is used with symbols as the method names, making it useful for dynamically created methods






share|improve this answer


























  • The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

    – vise
    Jan 2 at 13:03






  • 1





    @vise Updated the answer to mention the copying.

    – Kimmo Lehto
    Jan 2 at 13:10











  • That was extremely helpful. Thank you!

    – Luiz Melo
    Jan 2 at 13:20














3












3








3







This:



attr_accessor :byte_content


Essentially does:



def byte_content
@byte_content
end

def byte_content=(value)
@byte_content = value
end


Then



alias_method :read, :byte_content
alias_method :write, :byte_content=


will create copies of the byte_content and byte_content= methods with read and write as the new names . If you modify the byte_content or byte_content= methods after the aliasing, the read and write will remain unchanged.



So, the intention of the alias_method in the example appears to be to give the user a more "File-like" interface. Without the method aliases, you would use:



file = MyFile.new
file.byte_content = "abcd"
puts file.byte_content


With the aliases, the user can use:



file = MyFile.new
file.write("abcd")
file.read


Which is quite similar to the methods of the standard library File class,



file = File.open('/tmp/foo.txt', 'w')
file.write('hello')
file.rewind
puts file.read


And this makes it possible to use instances of MyFile in place of real File instances in some very simple use-cases, also known as Duck-typing.




alias vs alias_method



The differences of alias and alias_method are described in the answers to this question, most imporantly:




  • you can override alias_method but not alias


  • alias_method is used with symbols as the method names, making it useful for dynamically created methods






share|improve this answer















This:



attr_accessor :byte_content


Essentially does:



def byte_content
@byte_content
end

def byte_content=(value)
@byte_content = value
end


Then



alias_method :read, :byte_content
alias_method :write, :byte_content=


will create copies of the byte_content and byte_content= methods with read and write as the new names . If you modify the byte_content or byte_content= methods after the aliasing, the read and write will remain unchanged.



So, the intention of the alias_method in the example appears to be to give the user a more "File-like" interface. Without the method aliases, you would use:



file = MyFile.new
file.byte_content = "abcd"
puts file.byte_content


With the aliases, the user can use:



file = MyFile.new
file.write("abcd")
file.read


Which is quite similar to the methods of the standard library File class,



file = File.open('/tmp/foo.txt', 'w')
file.write('hello')
file.rewind
puts file.read


And this makes it possible to use instances of MyFile in place of real File instances in some very simple use-cases, also known as Duck-typing.




alias vs alias_method



The differences of alias and alias_method are described in the answers to this question, most imporantly:




  • you can override alias_method but not alias


  • alias_method is used with symbols as the method names, making it useful for dynamically created methods







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 13:23

























answered Jan 2 at 12:39









Kimmo LehtoKimmo Lehto

4,20311329




4,20311329













  • The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

    – vise
    Jan 2 at 13:03






  • 1





    @vise Updated the answer to mention the copying.

    – Kimmo Lehto
    Jan 2 at 13:10











  • That was extremely helpful. Thank you!

    – Luiz Melo
    Jan 2 at 13:20



















  • The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

    – vise
    Jan 2 at 13:03






  • 1





    @vise Updated the answer to mention the copying.

    – Kimmo Lehto
    Jan 2 at 13:10











  • That was extremely helpful. Thank you!

    – Luiz Melo
    Jan 2 at 13:20

















The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

– vise
Jan 2 at 13:03





The reason that alias_method can be overwritten is because it creates a copy of the method. In your example of alias_method :read, :byte_content the content of the read method should be @byte_content.

– vise
Jan 2 at 13:03




1




1





@vise Updated the answer to mention the copying.

– Kimmo Lehto
Jan 2 at 13:10





@vise Updated the answer to mention the copying.

– Kimmo Lehto
Jan 2 at 13:10













That was extremely helpful. Thank you!

– Luiz Melo
Jan 2 at 13:20





That was extremely helpful. Thank you!

– Luiz Melo
Jan 2 at 13:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006295%2fruby-alias-alias-method-confusion%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas