how to add created objects in static array? - ruby

How can i add all created objects in self.persons ?
class Person
attr_accessor :name, :last_name, :age
def initialize(name, last_name, age = "no_age")
##persons = []
#name = name
#last_name = last_name
#age = age
add(#name, #last_name, #age)
end
def self.persons
##persons
end
private
def add(name, last_name, age)
##persons << [name, last_name, age]
end
end
person1 = Person.new("name1", "lastname1", 12)
person2 = Person.new("name2", "lastname2", 16)
p Person.persons # => [["name2", "lastname2", 16]]

You can use "conditional assignment operator" to check if array is nil.
def initialize(name, last_name, age = "no_age")
##persons ||= []
#name = name
#last_name = last_name
#age = age
add(#name, #last_name, #age)
end

Related

how to print elements of a class variable array in ruby

I have just started learning ruby and I am unable to find out a solution on printing first_name and last_name for each element of ##people array...
class Person
#have a first_name and last_name attribute with public accessors
attr_accessor :first_name, :last_name
#have a class attribute called `people` that holds an array of objects
##people = []
#have an `initialize` method to initialize each instance
def initialize(x,y)#should take 2 parameters for first_name and last_name
#assign those parameters to instance variables
#first_name = x
#last_name = y
#add the created instance (self) to people class variable
##people.push(self)
end
def print_name
#return a formatted string as `first_name(space)last_name`
# through this method i want to print first_name and last_name
end
end
p1 = Person.new("John", "Smith")
p2 = Person.new("John", "Doe")
p3 = Person.new("Jane", "Smith")
p4 = Person.new("Cool", "Dude")
# Should print out
# => John Smith
# => John Doe
# => Jane Smith
# => Cool Dude
Why would you make the class Person to hold an array of persons?
It's easier if you just wrap your person objects in an array and then iterate over them and invoke their first_name and last_name accessors:
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
#first_name = first_name
#last_name = last_name
end
end
p1 = Person.new("John", "Smith")
p2 = Person.new("John", "Doe")
p3 = Person.new("Jane", "Smith")
p4 = Person.new("Cool", "Dude")
[p1, p2, p3, p4].each { |person| p "#{person.first_name} #{person.last_name}" }
# "John Smith"
# "John Doe"
# "Jane Smith"
# "Cool Dude"

how to use attribute in different files?

I am finishing my coursework, a simple library system.
I am facing a problem like below:
book.rb
class Book
attr_accessor :title, :author, :language, :classification, :isbn, :book_id, :borrow_status
def initialize(title, author, language, classification, isbn, book_id, borrow_status)
#title = title
#author = author
#language = language
#classification = classification
#isbn = isbn
#book_id = book_id
#borrow_status = borrow_status
end
def bookid
#book_id
end
def booklist
#title = #title.split(/ |\_|\-/).map(&:capitalize).join(" ")
#author = #author.split(/ |\_|\-/).map(&:capitalize).join(" ")
#language = #language.capitalize!
#isbn.to_s
#book_id.to_s
{
"Title" => #title,
"Author" => #author,
"Language" => #language,
"Classification" => #classification,
"ISBN" => #isbn,
"Book ID" => #book_id,
"Status" => #borrow_status,
}
end
end
user.rb
require_relative 'book.rb'
class User
attr_accessor :name, :address, :gender, :age, :id, :borrow
def initialize(name, address, gender, age, id, borrow)
#name = name
#address = address
#gender = gender
#age = age
#id = id
#borrow = borrow
end
def userlist
#name = #name.split(/ |\_|\-/).map(&:capitalize).join(" ")
#address = #address.split(/ |\_|\-/).map(&:capitalize).join(" ")
#age.to_s
#id.to_s
if #borrow.nil?
puts "nothing"
elsif
puts #I wish I can put book's name here, if I entered correct #borrow. eg.,for user3's #borrow=4(in top.rb), user3.#borrow=book4.#book_id, then print #name of book4
else
puts "error"
end
the problem is inside user.rb, in if-elsif-else loop, which is
I wish I can put book's name here, if I entered correct #borrow.
eg.,for user3's #borrow=4(in top.rb), user3.#borrow=book4.#book_id,
then print #name of book4
any solution?
#user.rb
require_relative 'book.rb'
class User
attr_accessor :name, :address, :gender, :age, :id, :borrow
def initialize(name, address, gender, age, id, borrow)
#name = name
#address = address
#gender = gender
#age = age
#id = id
#borrow = borrow
end
def userlist
#name = #name.split(/ |\_|\-/).map(&:capitalize).join(" ")
#address = #address.split(/ |\_|\-/).map(&:capitalize).join(" ")
#age.to_s
#id.to_s
if borrow.nil?
puts "nothing"
elsif borrow
puts borrow.title
else
puts "error"
end
end
end
#book = Book.new('Book1', 'Auth1', 'EN', 'fiction', 'isb123', 1, 'borrowed')
#user = User.new('Mr Foo', '123 st', 'male', 23, 123, nil)
#user.borrow=#book
#user.userlist
#=>Book1
But you might instead want to send an array of books, then you'll need to modify your method to handle array
def userlist
#name = #name.split(/ |\_|\-/).map(&:capitalize).join(" ")
#address = #address.split(/ |\_|\-/).map(&:capitalize).join(" ")
#age.to_s
#id.to_s
if borrow.nil?
puts "nothing"
elsif borrow.length > 0 # checks if anythint in the array
borrow.each{ |b| puts b.title }
else
puts "error"
end
end

Ruby Search by using input

modify this code to create search by first name using user input output must show age and title
# A simple Employee class
class Employee
attr_reader :first_name, :last_name, :title, :age
def initialize(fname, lname, title, age)
#first_name = fname
#last_name = lname
#title = title
#age = age
end
# A string representation of the Employee object
def to_s
"#{first_name} #{last_name}, #{title}, #{age}"
end
end
# The collection class for Employee objects
class Employees
include Enumerable
def initialize
#employees = []
end
# Add Employee objects to the collection
def <<(employee)
#employees << employee
end
# Method mandated by the Enumerable module
def each
#employees.each { |e| yield(e) }
end
end
employees = Employees.new
employees << Employee.new('Anita', 'Baker', 'President', 48)
employees << Employee.new('Frank', 'Gifford', 'Director', 58)
employees << Employee.new('Barbara', 'Eden', 'Secretary', 34)
employees << Employee.new('George', 'Clooney', 'Project Manager', 37)
employees << Employee.new('Emily', 'Davies', 'Programmer', 28)
employees << Employee.new('David', 'Faber', 'Programmer', 55)
employees << Employee.new('Cindy', 'Adams', 'Programmer', 33)
employees << Employee.new('Helen', 'Hamilton', 'Business Analyst', 42)
You have already done all the necessary work by implementing Enumerable interface. Just accept user input and call the Enumerable#find method:
fname = gets.chomp
e = employees.find {|i| i.first_name == fname}

m.one + m.two + m.three doesn't work

This is my code:
class Person
def initialize(first_name, last_name, age)
#first_name = first_name
#last_name = last_name
#age = age
end
def first_name
puts #first_name
end
def last_name
puts #last_name
end
def age
puts #age
end
end
class Musician < Person
def initialize(first_name, last_name, age, instrument)
#first_name = first_name
#last_name = last_name
#age = age
#instrument = instrument
end
def instrument
puts #instrument
end
end
Then when I try to do the following:
m = Musician.new("George", "Harrison", 58, "guitar")
m.first_name + " " + m.last_name + ": " + m.age.to_s
I get an error:
in <main>': undefined method+' for nil:NilClass (NoMethodError)
Why can't I just concatenate the results of objects method?
all your methods return nil rather than the value you wish, that is, "puts" returns nil.
just eliminate the "puts" and try again

Ruby class instance and inheritance

I want to make a subclass to add attributes to the subclass in addition to the superclass. this is what I've tried:
Version I:
class Person
attr_reader :first_name, :last_name, :age
def initialize (first_name, last_name, age)
#first_name = first_name
#last_name = last_name
#age = age
end
end
class Musician < Person
attr_reader :first_name, :last_name, :age, :instrument
def initialize (first_name, last_name, age, instrument)
super
#instrument
end
end
Version II
class Person
ATTRS = ['first_name', 'last_name', 'age']
def attributes
ATTRS
end
end
class Musician < Person
ATTRS = ['instrument']
def attributes
super + ATTRS
end
end
Neither of these work.
In version 1 try
class Musician < Person
attr_reader :instrument
def initialize(first_name, last_name, age, instrument)
# Pass arguments to the super class' constructor!
super first_name, last_name, age
#instrument = instrument
end
end
Thanks to attr_reader :first_name, :last_name, :age in Person Musician will have those three accessors available because of inheritance.

Resources