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!

Kimmo Lehto
  • 5,910
  • 1
  • 23
  • 32
Luiz Melo
  • 37
  • 1
  • 6
  • 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 [mcve] really *is* minimal. Are you absolutely certain that your problem cannot possibly be replicated in less than 25 lines? – Jörg W Mittag Jan 02 '19 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 : https://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 02 '19 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 02 '19 at 20:52

1 Answers1

4

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
Kimmo Lehto
  • 5,910
  • 1
  • 23
  • 32