Ruby – How do I learn in class?

class X
def initialize
@name = "Bob"
end
blah blah
end

puts X.new # I want this to print X:Bob
puts [X.new, X.new] # I want this to print [X:Bob, X:Bob]
to_s method of overriding class:

class X
def initialize
@name = "Bob"
end

def to_s
"X:#{@name}"
end
end

puts X.new # prints X:Bob
puts [X.new, X.new].to_s # prints [X:Bob, X:Bob ]

class X
def initialize
@name = "Bob"
end
blah blah
end

puts X.new # I want this to print X:Bob
puts [X.new, X.new] # I want this to print [X:Bob, X :Bob]

Override the to_s method of the class:

class X
def initialize
@name = "Bob"
end

def to_s
"X:#{@name}"
end
end

puts X.new # prin ts X:Bob
puts [X.new, X.new].to_s # prints [X:Bob, X:Bob]

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3288 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.