Why is this variable an object?

Hi, I’ve been going through the “The well grounded rubyist” recently but
got stuck when I came
across this particular code snippet.
My questions are in the #comments lines below as well.
A variable is getting assigned a value from a singleton method but not
being initialized as an object, so how can it access the attributes of
the ticket object
below??

Thank you very kindly, for your help

#!/usr/bin/ruby
class Ticket
attr_reader :venue, :date
attr_accessor :price

    #This method wasn't in the code snippet of
    #the book, I added it, but that's not the Question
    def initialize(venue, date)
            @venue=venue
            @date=date
    end

end

def Ticket.most_expensive(*tickets)
tickets.max_by(&:price)
end

th = Ticket.new(“Town Hall”,“11/12/13”)
cc = Ticket.new(“Convention Center”,“12/13/14/”)
fg = Ticket.new(“Fairgrounds”, “13/14/15/”)

th.price = 12.55
cc.price = 10.00
fg.price = 18.00

#QUESTION, We are not using highest=Ticket.new
highest = Ticket.most_expensive(th,cc,fg)

#So why does this below line work?
puts “The highest-priced ticket is the one for #{highest.venue}.”

Hi Jesus, Thanks for your answer and I’ve put it to the test below and
it works not only
in code but also in freaking me out.
Now I’m even more confused as I don’t know how your suggestion has the
same effect.
The code below does not show me how the variable highest is connected in
any way
to the class Ticket.
It has been assigned the return value of an array’s method! isn’t it?
So I just don’t see how it becomes a Ticket object ? :slight_smile:
What am I missing to understand here ?
Thank you.

#!/usr/bin/ruby
class Ticket
attr_reader :venue, :date
attr_accessor :price

    def initialize(venue, date)
            @venue=venue
            @date=date
    end

end

th = Ticket.new(“Town Hall”,“11/12/13”)
cc = Ticket.new(“Convention Center”,“12/13/14/”)
fg = Ticket.new(“Fairgrounds”, “13/14/15/”)

th.price = 12.55
cc.price = 10.00
fg.price = 18.00

highest = [th, cc, fg].max_by(&:price)

puts highest.class
puts “The highest-priced ticket is the one for #{highest.venue}.”
p highest

This prints:
Ticket
The highest-priced ticket is the one for Fairgrounds.
#<Ticket:0x10016c168 @price=18.0, @date=“13/14/15/”,
@venue=“Fairgrounds”>

Hi Jesus!

I understand… Finally :slight_smile:

The return value of:
highest = [th, cc, fg].max_by(&:price)
is an object - Either of th, cc, fg
Therefore the object can be referenced via it’s attributes. Great.

Also, highest = Ticket.most_expensive(th,cc,fg) will set
highest to refer to an object - Any one of the “tickets” args
(which are objects!) sent to the singleton method. Awesome.

Thanks Dansei too.

I appreciate your help…

This creates an array with many different objects

and returns one randomly, saving a reference to the

object in the variable ‘var’.

var = [3,“str”,:sym,nil,7.9,false,[],{}].max_by{rand}
puts var.class

Each time you run it it prints String, or Array, or

Hash etc.

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