# More on the fundamentals

**URL:** <https://www.ruby-forum.com/t/more-on-the-fundamentals/51570>\
**Category:** Ruby\
**Created:** [January 3, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570 "2006-01-03T15:53:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 3, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/1 "2006-01-03T15:53:18Z")

</div>

Please have a look at the comments below to see that I’m understanding  
things correctly;

#!/usr/bin/ruby  
#Tue Jan 3 14:04:28 GMT 2006  
class Greeter  
# a new class cllaed Greeter  
def initialize(name)  
# creating a new method called name  
@name = name  
# we define a new @instance variable called name  
end  
def say(phrase)  
# creating a new method called phrase…  
puts “#{phrase}, #{@name}”  
# … which uses doube quotes to substitute  
end  
end

# say hello

g1 = Greeter.new(“jayeola”)  
g2 = Greeter.new(“buddy”)  
g3 = Greeter.new(“vimmer”)  
g4 = Greeter.new(“slammer”)

# we have just created four new (object) instances belonging to the

class

# Greeter. They can now use the instance variables that have been

created

# above

g1.say(“Hello”)  
g2.say(“Wotcha”)  
g3.say(“Ire!”)  
g4.say(“Elake”)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 3, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/2 "2006-01-03T16:06:00Z")

</div>

Hi –

On Tue, 3 Jan 2006, John M. wrote:

> Please have a look at the comments below to see that I’m  
> understanding things correctly;
> 
> #!/usr/bin/ruby  
> #Tue Jan 3 14:04:28 GMT 2006  
> class Greeter  
> # a new class cllaed Greeter  
> def initialize(name)  
> # creating a new method called name

There’s no method called name.

> ```
> @name = name
> # we define a new @instance variable called name
> 
> ```

Move the @ from @instance to name and you’re set 🙂

> ```
> end
> def say(phrase)
> # creating a new method called phrase...
> 
> ```

There’s no method called phrase.

> ```
> puts "#{phrase}, #{@name}"
> # ... which uses doube quotes to substitute
> end
> 
> ```
> 
> end

It seems that you’re confusing instance variable assignment with  
method creation. They’re unrelated. You can, of course, write a  
method that returns the current value of an instance variable:

def name  
@name  
end

and you can even get Ruby to do it semi-automatically for you:

attr\_reader :name # creates the above “name” method

But @name = name is just a variable assignment.

> # say hello
> 
> g1 = Greeter.new(“jayeola”)  
> g2 = Greeter.new(“buddy”)  
> g3 = Greeter.new(“vimmer”)  
> g4 = Greeter.new(“slammer”)
> 
> # we have just created four new (object) instances belonging to the class
> 
> # Greeter. They can now use the instance variables that have been created

I would say “instances of the class Greeter”.

> # above
> 
> g1.say(“Hello”)  
> g2.say(“Wotcha”)  
> g3.say(“Ire!”)  
> g4.say(“Elake”)

Have another look at the attr\_\* family of methods, and keep in mind  
that they’re essentially shortcuts for writing your own get-and/or-set  
methods (using instance variables), and I think that part of it will  
fall into place.

David

–  
David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 3, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/3 "2006-01-03T16:10:08Z")

</div>

On Jan 3, 2006, at 14:51, John M. wrote:

> Please have a look at the comments below to see that I’m  
> understanding things correctly;

You’ve made a couple of fundamental mistakes regarding how methods  
are defined. The basic schema is:

def method\_name(parameter\_name)

Where you can have a whole list of parameters, or none at all. See  
the revised (and very verbose comments. Hope this helps.

matt smillie.

Also, I’ve changed the indented to two spaces, since that’s the Ruby  
norm.

class Greeter

# open class Greeter (it’s not necessarily _new_)

def initialize(name)  
# defining the initialise method, which is called when a new  
# object is created. The method takes one parameter, ‘name’.  
# Example (such as: foo = Greeter.new(“Mark”).  
@name = name  
# define an instance variable called ‘@name’ and assign it  
# the value of the parameter ‘name’  
end

def say(phrase)  
# defining a new method called ‘say’ with a parameter  
# called ‘phrase’.  
puts “#{phrase}, #{@name}”  
# The double quotes define a string, and the #{} sections are  
# used for interpolation (substitution).  
end  
end

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 4, 2006, 3:16am UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/4 "2006-01-04T03:16:04Z")

</div>

John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> In this line of code can anyone tell me why “food” is a method? I  
> thought that they are preceded by a dot.

It is not a method, it’s a variable, very possibly a block-local one.

> ```
> __an array__ the method __ parameters for code block
> 
> ```
> 
> / / /  
> [cornflakes, yam, rice].each { |food| eat food}  
> /   
> code block \_\_/ \_\_block args

^^^ is parsed as: [corflakes, yam, rice].each {|food| eat(food)}

YS.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 4, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/5 "2006-01-04T18:26:50Z")

</div>

Quoting John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> In this line of code can anyone tell me why “food” is a method? I  
> thought that they are preceded by a dot.
> 
> ```
> __an array__ the method __ parameters for code
> 
> ```
> 
> block  
> / / /  
> [cornflakes, yam, rice].each { |food| eat food}  
> /   
> code block \_\_/ \_\_block args

Within the context of the block, “food” is a variable. “eat”,  
however, will be interpreted as a private method call (i.e. as a  
call to the “eat” method on self), provided an “eat” variable does  
not already exist in that scope.

Generally, given a bare identifier, the rule is:

1. If a previous assignment to the name exists in the current scope  
(whether or not it was actually performed), the name is a variable  
(or a parse error if you try to give it parameters)

2. otherwise, it is a call to a private method on self

Examples:

1. foo # method

2. foo = 1  
foo # variable

3. if false  
foo = 1 # never executed  
end  
foo # still a variable (initialized to nil)

4. [1, 2, 3].each { |foo| # introduced as block param  
foo # variable here  
}  
foo # method call here

5. foo = 1  
foo # variable  
[1, 2, 3].each { |foo| # same variable  
foo # yes, same variable  
}  
foo # still same variable

-mental

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 4, 2006, 1:44am UTC](https://www.ruby-forum.com/t/more-on-the-fundamentals/51570/6 "2006-01-04T01:44:09Z")

</div>

In this line of code can anyone tell me why “food” is a method? I  
thought that they are preceded by a dot.

```
__an array__ the method __ parameters for code block

```

/ / /  
[cornflakes, yam, rice].each { |food| eat food}  
/   
code block \_\_/ \_\_block args

On Tue, 3 Jan 2006 23:51:15 +0900
