Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > More on the fundamentals...

Reply
Thread Tools

More on the fundamentals...

 
 
John Maclean
Guest
Posts: n/a
 
      01-03-2006
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")

--
John Maclean
MSc (DIC)
07739 171 531



 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      01-03-2006
Hi --

On Tue, 3 Jan 2006, John Maclean 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


"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black


 
Reply With Quote
 
 
 
 
Matthew Smillie
Guest
Posts: n/a
 
      01-03-2006


On Jan 3, 2006, at 14:51, John Maclean 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



>
> #!/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")
>
> --
> John Maclean
> MSc (DIC)
> 07739 171 531
>
>




 
Reply With Quote
 
John Maclean
Guest
Posts: n/a
 
      01-04-2006
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
John Maclean <> 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
> @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")
>



--
John Maclean
MSc (DIC)
07739 171 531



 
Reply With Quote
 
Yohanes Santoso
Guest
Posts: n/a
 
      01-04-2006
John Maclean <> 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.


>
> On Tue, 3 Jan 2006 23:51:15 +0900
> John Maclean <> 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
>> @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")
>>

>
>
> --
> John Maclean
> MSc (DIC)
> 07739 171 531



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
DVD Verdict reviews: SYLVESTER AND THE MAGIC PEBBLE AND MORE MAGICAL TALES and more! DVD Verdict DVD Video 0 04-07-2005 08:10 AM
Sygate uses more and more memory? Louise Computer Security 0 06-01-2004 05:30 AM
Re: With More Flash More Lumix: using an external flash unit with the FZ1 and other digicams Hans-Georg Michna Digital Photography 4 08-24-2003 06:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57