Class method

Need help to understand how the marked <<< def works. Thanks

class Person
def initialize(lname, fname)
@lname = lname
@fname = fname
end

def lname
return @lname
end

def fname
return @fname
end

def lname=(myarg) # see = <<<
@lname = myarg
end

def fname=(myarg)
@fname = myarg
end
end

steve = Person.new(“Litt”, “Stove”)
print "My name is ", steve.fname, " ", steve.lname, “.\n”
steve.fname = “Steve”
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve = Person.new(“Litt”, “Stove”)

print "My name is ", steve.fname, " ", steve.lname, “.\n”
steve.fname = “Steve”
print "My name is ", steve.fname, " ", steve.lname, “.\n”

#steve.fname(“Steve”) # err

#steve.lname(“Nathan”) # err ???
steve.lname() #
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve.lname = “Nat” #
print "My name is ", steve.fname, " ", steve.lname, “.\n”

steve.lname() #
print "My name is ", steve.fname, " ", steve.lname, “.\n”

I:\RubyNV>zzz-tut13-class.rb
My name is Stove Litt.
My name is Steve L…
My name is Steve L…
My name is Steve Nat.
My name is Steve Nat.

I:\RubyNV>

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

if i use it stand alone it does not work!!

class Person
def initialize(lname)
@lname = lname
end

def lname=(myarg) # see = <<<
@lname = myarg
end

end

steve = Person.new(“Viswa”)

#puts steve.lname, ‘x1’ # err undefined methid lname
#puts steve.lname(), ‘x2’ # err as above
#puts steve.lname(“nv”), ‘x2’ # err as above

#steve.lname = “Nat” #

#puts steve.lname, ‘x2’ # err undefined methid lname
#puts steve.lname(), ‘x3’ # # err undefined methid lname

#steve.lname = “Nat” #
#print "My name is ", steve.lname, “.\n” #err

#steve.lname() # err
#print "My name is ", steve.lname, “.\n”# err

On Nov 30, 2007, at 1:54 PM, Nathan V. wrote:

end
@fname = myarg
print "My name is ", steve.fname, " ", steve.lname, “.\n”
steve.fname = “Steve”
print "My name is ", steve.fname, " ", steve.lname, “.\n”

#steve.fname(“Steve”) # err

The name of the method includes the = and the form:

steve.fname = “Steve”

is just syntactic sugar for:

steve.fname=(“Steve”)

#steve.lname(“Nathan”) # err ???

the lname method doesn’t take any arguments. def lname

I:\RubyNV>zzz-tut13-class.rb
if i use it stand alone it does not work!!

class Person
def initialize(lname)
@lname = lname
end

def lname
@lname
end

#puts steve.lname(“nv”), ‘x2’ # err as above
#print "My name is ", steve.lname, “.\n”# err

Posted via http://www.ruby-forum.com/.

You should get used to the shortcut to standard accessors. Rather
than building

def myname
@myname
end

def myname=(arg)
@myname = arg
end

You can just add this to your class:

attr_accessor :myname

In your case, the first class could be written as:

class Person
attr_accessor :lname, :fname

def initialize(lname, fname)
@lname = lname
@fname = fname
end
end

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Nov 30, 2007 12:54 PM, Nathan V. [email protected] wrote:

end

def fname
return @fname
end

def lname=(myarg) # see = <<<
@lname = myarg
end

You are not defining an equals method here (=). You are defining the
method lname=. The parser will look for this method before deciding
it’s an assignment to variable.

steve = Person.new(“Litt”, “Stove”)

print "My name is ", steve.fname, " ", steve.lname, “.\n”
steve.fname = “Steve”
print "My name is ", steve.fname, " ", steve.lname, “.\n”

#steve.fname(“Steve”) # err

The fname method as you have defined it does not take a parameter. It
simply returns the fname. The correct way to call it is steve.fname
(returns Steve)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
end

end

steve = Person.new(“Viswa”)

#puts steve.lname, ‘x1’ # err undefined methid lname

This time you left out the lname method in the class.

#steve.lname() # err
#print "My name is ", steve.lname, “.\n”# err

Posted via http://www.ruby-forum.com/.

You have a getter and a setter … 2 methods #lname and #lname=

Note that this is almost exactly the same thing. It builds those 2
methods for you.

class Peep
attr_accessor :name
end

p = Peep.new
p.name = “Smith”
puts p.name # returns Smith
puts p.methods.grep /name/ #gives you name and name=

Todd

Nathan V. wrote:

I:\RubyNV>

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

if i use it stand alone it does not work!!

class Person

steve = Person.new(“Viswa”)

#puts steve.lname, ‘x1’ # err undefined methid lname
#puts steve.lname(), ‘x2’ # err as above
#puts steve.lname(“nv”), ‘x2’ # err as above

Here is another attempt, look at the following and notice that I had to
define the method lname() that takes in a parameter to get the results
that you wanted…

This is not the way to do things, just meant to show how it can be
done… :slight_smile:

irb(main):001:0> class Person
irb(main):002:1> def lname(myarg = nil)
irb(main):003:2> @lname = myarg if myarg
irb(main):004:2> @lname
irb(main):005:2> end
irb(main):006:1> def lname=(myarg); lname(myarg); end
irb(main):007:1> end
=> nil
irb(main):008:0> p = Person.new
=> #Person:0x2ac90263d940
irb(main):009:0> p.lname = “jack”
=> “jack”
irb(main):010:0> p.lname(“bill”)
=> “bill”
irb(main):011:0> p.lname
=> “bill”

hth

ilan

Hi –

On Sat, 1 Dec 2007, Todd B. wrote:

return @lname

You are not defining an equals method here (=). You are defining the
method lname=. The parser will look for this method before deciding
it’s an assignment to variable.

It’s actually the other way around (if I’m understanding your point
correctly). If something looks like it could be a local variable
assignment, the parser will assume that it is one. Therefore you
have to use an explicit receiver for methods like lname=, if you want
to use the syntactic sugar = thing.

David

On 30.11.2007 19:54, Nathan V. wrote:

Need help to understand how the marked <<< def works. Thanks

What does this have to do with the subject? For all I can see we are
dealing with instance methods here.

def fname
end
print "My name is ", steve.fname, " ", steve.lname, “.\n”
steve.lname() #
I:\RubyNV>
def lname=(myarg) # see = <<<
@lname = myarg
end

end

steve = Person.new(“Viswa”)

#puts steve.lname, ‘x1’ # err undefined methid lname
#puts steve.lname(), ‘x2’ # err as above
#puts steve.lname(“nv”), ‘x2’ # err as above

Well, if you do not define those methods, they are not there.

#steve.lname = “Nat” #

#puts steve.lname, ‘x2’ # err undefined methid lname
#puts steve.lname(), ‘x3’ # # err undefined methid lname

#steve.lname = “Nat” #
#print "My name is ", steve.lname, “.\n” #err

#steve.lname() # err
#print "My name is ", steve.lname, “.\n”# err

For a read write attribute you need to define both methods.
Assignment and query.

def foo=(x) @foo=x end
def foo; @foo end

Or, much simpler in a class

attr_accessor :foo

Kind regards

robert

On Nov 30, 2007 2:55 PM, David A. Black [email protected] wrote:

@fname = fname

def lname=(myarg) # see = <<<
have to use an explicit receiver for methods like lname=, if you want
to use the syntactic sugar = thing.

David

Yeah, after playing around a bit, I see you are right. It assumes
assignment in the absence of an explicit receiver. I’ve never used a
single = in a method name before (aside from the automation provided
by the #attr_ methods of course :slight_smile:

Todd

why does line 2 state initialize instead of breed?

Here’s the modified example - p050newdog.rb

  1. class NewDog
  2. def initialize(breed)
  3. @breed = breed
    
  4. end
  5. attr_reader :breed, :name # create reader only
  6. setter method

  7. def name=(nm)
  8. @name = nm
    
  9. end
  10. end
  11. nd = NewDog.new(‘Doberman’)
  12. #nd.name=(‘Benzy’)
  13. nd.name = ‘Benzy’
  14. puts nd.name

Found this in
http://rubylearning.com/satishtalim/ruby_syntactic_sugar.html
Describes
def methodname= usage

Programmers use the term syntactic sugar to refer to special rules that
let you write your code in a way that doesn’t correspond to the normal
rules but that is easier to remember how to do and looks better.

Let us say we want to set the name of a dog. As a starting point, name
can be set along with everything else at object creation time, as in the
example below.

  1. class NewDog
  2. def initialize(breed, name)
  3. @breed = breed
    
  4. @name = name
    
  5. end
  6. attr_reader :breed, :name # create reader only
  7. end
  8. nd = NewDog.new(‘Doberman’, ‘Benzy’)
  9. puts nd.name

class NewDog def initialize(breed, name) @breed = breed @name = name end
attr_reader :breed, :name # create reader only end nd =
NewDog.new(‘Doberman’, ‘Benzy’) puts nd.name

Let’s write a set_name method that allows us to set, or reset, the name
of an existing dog. We’ll also rewrite the initialize method so that it
doesn’t expect a name:

  1. class NewDog
  2. def initialize(breed)
  3. @breed = breed
    
  4. end
  5. attr_reader :breed, :name # create reader only
  6. setter method

  7. def set_name(nm)
  8. @name = nm
    
  9. end
  10. end
  11. nd = NewDog.new(‘Doberman’)
  12. nd.set_name(‘Benzy’)
  13. puts nd.name

class NewDog def initialize(breed) @breed = breed end attr_reader
:breed, :name # create reader only # setter method def set_name(nm)
@name = nm end end nd = NewDog.new(‘Doberman’) nd.set_name(‘Benzy’) puts
nd.name

Ruby allows you to define methods that end with an equal sign (=). Let’s
replace set_name with a method called name=

  1. def name=(nm)
  2. @name = nm
  3. end

def name=(nm) @name = nm end

name= does exactly what set_name did, and in spite of the slightly odd
method name, you can call it just like any other method:

  1. nd.name=(‘Benzy’)

nd.name=(‘Benzy’)

Here’s the modified example - p050newdog.rb

  1. class NewDog
  2. def initialize(breed)
  3. @breed = breed
    
  4. end
  5. attr_reader :breed, :name # create reader only
  6. setter method

  7. def name=(nm)
  8. @name = nm
    
  9. end
  10. end
  11. nd = NewDog.new(‘Doberman’)
  12. #nd.name=(‘Benzy’)
  13. nd.name = ‘Benzy’
  14. puts nd.name

class NewDog def initialize(breed) @breed = breed end attr_reader
:breed, :name # create reader only # setter method def name=(nm) @name =
nm end end nd = NewDog.new(‘Doberman’) #nd.name=(‘Benzy’) nd.name =
‘Benzy’ puts nd.name

The equal sign gives you that familiar “assigning a value to something”
feeling, so you know you’re dealing with a setter method. It still looks
odd, but Ruby takes care of that, too.

Ruby gives you some syntactic sugar for calling setter methods. Instead
of this:

  1. nd.name=(‘Benzy’)

nd.name=(‘Benzy’)

you’re allowed to do this:

  1. nd.name = ‘Benzy’

nd.name = ‘Benzy’

When the interpreter sees the message “name” followed by " =", it
automatically ignores the space before equal sign and reads the single
message “name=” - a call to the method whose name is name=, which we’ve
defined. As for the right-hand side: parentheses are optional on single
arguments to methods, so you can just put ‘Benzy’ there and it will be
picked up as the argument to the name= method.

IN RAILS: Method calls using the equal-sign syntax are common in Rails
applications.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs