0

hi i am very much a beginner.

i think i understand how the attr_accessor works (below). and the "setter" is the name=(name) method. and i know that that method is equivalent to the assignment: name = "john". because "=" is a method that accepts an argument and assigns that argument to whatever object calls it. (though i don't understand how "name" could be considered an object as it is being assigned to an object)

so my question is: how can you assign a variable calling a method as a method name? It feels like I'm missing something..

class Person
  def name
    @name
  end

  def name=(name)
    @name = name
  end
end
Zach Smith
  • 8,458
  • 13
  • 59
  • 133
  • It's unclear what you're asking. Are you just asking how to use your `name=` method? – user229044 Jan 05 '14 at 09:34
  • thank you for the reply. am i right in thinking that name= is actually a variable "name" calling the method "=" with a "string" argument. how is that allowed as a method name itself? – Zach Smith Jan 05 '14 at 09:38
  • Err, no, the method itself is called `name=`. This is how Ruby denotes setter methods. – user229044 Jan 05 '14 at 09:39
  • These might be helpful: http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby http://stackoverflow.com/questions/12142193/ruby-difference-between-instance-and-local-variables-in-ruby – jcm Jan 05 '14 at 11:11
  • thanks the first link was really helpful. – Zach Smith Jan 05 '14 at 11:23

1 Answers1

1

so my question is: how can you assign a variable calling a method as a method name? It feels like I'm missing something..

You don't. In this code

def name=(name)
  @name = name
end

name= isn't a variable name calling a method =. The name of the method is name=.

Edit:

In the above code snippet the def paired with a terminating end constitutes a method definition.

def method_name(param1, param2)
  # method body
end

On the same line as def there can only be the method name, optional parentheses, and the param list. By definition having a "variable calling a method" in that line would be illegal. So in your code name= is the method name.

jcm
  • 1,781
  • 1
  • 15
  • 27
  • so why if i write this code " object.name = "tim" would it end up calling that method? – Zach Smith Jan 05 '14 at 09:58
  • It's just syntactic sugar. The space before the equal sign is ignored and the method `name=` is called. – jcm Jan 05 '14 at 10:11
  • i understand that. but i still don't get why "name=" would call a method with a name "name=" instead of trying to call the method "=" on name. which wouldn't work because of scope. so why is it when i type object.name.=("tim") is that giving me the result of typing object.name=("tim"). I'm going to try and write it out clearer in another box – Zach Smith Jan 05 '14 at 10:18
  • sorry i have tried something else. i see that object.name.=("tim") doesn't work. however 5.+(5) does work. are "=" and "+" not the same in that regard? – Zach Smith Jan 05 '14 at 10:29
  • No, in `object.name="time"` note that there isn't a dot between `name` and `=`. That's because the method is `name=`. In `5.+(1)`, you are calling the method `+` on `5`, passing the argument `1`. – jcm Jan 05 '14 at 10:45
  • so by defining the "setter" method: ***** def name=(name) @name = name end ******** you are re-defining the method "name=" to assign "@name" instead of "name"? – Zach Smith Jan 05 '14 at 10:56
  • It isn't a redefinition since it hasn't been defined before. (You don't have an `attr_accessor` call). Even if you had an `attr_accessor` and hadn't defined your own `name=` method, the assignment would still be to `@name`, not `name`. – jcm Jan 05 '14 at 11:10
  • thank you for your time. last question (i hope - just to confirm). if i just assign a local variable. sound = "bang". is that a main.sound=("bang") method? if so, where is that method being defined? or how is that assignment working? – Zach Smith Jan 05 '14 at 11:56
  • Hmm... good question. I'm not sure if assignment is an implicit method call to the `self` object. Exactly what the `self` object is depends on the scope. – jcm Jan 05 '14 at 12:15
  • After doing some reading I don't think that `=` is a method call. – jcm Jan 05 '14 at 12:28
  • ahhhhh. then i still don't know what that little = sign is actually doing – Zach Smith Jan 05 '14 at 13:20
  • ive reopened the question at http://stackoverflow.com/questions/20934375/how-does-the-assignment-symbol-work-ruby – Zach Smith Jan 05 '14 at 13:55