Dynamic variable from user input - ruby

I'm trying to get a user's input to create a new User object:
class User
attr_accessor :name
attr_accessor :age
attr_accessor :email
end
puts "Name:"
input = gets.chomp.to_s
name = "#{input} = User.new"
puts "Age:"
age = gets.chomp.to_s
puts "Email:"
email = gets.chomp.to_s
name.name = "#{input}"
name.age = "#{age}"
name.email = "#{email}"
puts "Name: #{input.name}, Age: #{input.age}, E-mail: #{input.email}"
How can I call the newly created object so that I can input new attributes? Do I need to change the name part of
name.name = "#{input}"
name.age = "#{age}"
name.email = "#{email}"
to the user's input?

In your case, input = gets.chomp.to_s is a String, and a string does not have any name, age and email methods you should be creating a new User object and then fill it with the information the user gives you, you're code is not so far from the answer but there are a lot's of little mistakes, here is the following working code:
class User
attr_accessor :name
attr_accessor :age
attr_accessor :email
end
user = User.new
puts "Name:"
user.name = gets.chomp.to_s
puts "Age:"
user.age = gets.chomp.to_s
puts "Email:"
user.email = gets.chomp.to_s
puts "Name: #{user.name}, Age: #{user.age}, E-mail: #{user.email}"
I hope it helps you.
By the way, what were you trying to achieve with this line: name = "#{input} = User.new" ? Because you're only creating a new string with the content "User.new" (as a string literal, it's not executed by Ruby in any way, you will never have a new object by surrounding its declaration with double (or single) quotes.

Related

obj.method(argument)

I am looking at this code:
class Student
attr_accessor :first_name, :last_name, :age
def initialize(first, last, age)
#first_name = first
#last_name = last
#age = age
end
def birthday
#age += 1
end
end
class ViewStudent
def initialize(student)
#student = student
end
def do_something
puts "Student name: #{#student.first_name} #{#student.last_name}"
end
end
class UpdateStudent
def initialize(student)
#student = student
end
def do_something
puts "What is the student's first name?"
#student.first_name = gets.chomp
puts "What is the student's last name?"
#student.last_name = gets.chomp
puts "Updated student: #{#student.first_name} #{#student.last_name}"
end
end
choices = [ViewStudent, UpdateStudent]
student = Student.new("John", "Doe", 18)
puts "Select 1 to view student or 2 to update student."
selection = gets.chomp.to_i
obj = choices[selection - 1]
obj = obj.new(student)
obj.do_something
In the last five lines, I understand that selection = gets.chomp.to_i converts the selection options to integers, but how does that work in tandem with obj = choices[selection - 1]?
I'm also not sure what obj = obj.new(student) and obj.do_something do. It looks like a local variable is being set to create a new object with student as the argument. However, obj isn't a class or method to call on?
I can also gather that obj.do_something calls the methods defined for both ViewStudent and UpdateStudent given the selection.
I saw this, but it doesn't answer my question.
obj = choices[selection - 1] just select ViewStudent if 1 and UpdateStudent if 2 from your array by index (choices[0] or choices[1]).
Then you creating an instance of selected class (ViewStudent.new or UpdateStudent.new) and call do_something method on this instance, because this methos difined in both classes:
obj = choices[selection - 1] # obj is ViewStudent or UpdateStudent class
obj = obj.new(student) # obj is ViewStudent or UpdateStudent instance
obj.do_something # call `do something` method on instance

Ruby - Making my code object orientated

I'm just starting out with OOP and am struggling to work out how to implement it. I can't work out how to create an instance of the class using the hash.new method. It's a basic program that takes input from a user and stores it in a hash. I was then planning on pushing the hash into an array to take advantage of the index. Below is the code
class Customer
def initialize(id, name, address, phone, email, bank_balance)
#id = id
#name = name
#address = address
#phone = phone
#email = email
#bank_balance = bank_balance
end
end
puts "Would you like to create an account?"
answer = gets.chomp
loop do
new_hash = {}
if answer.downcase.start_with?('y')
puts "Enter your name"
name = gets.chomp.downcase
new_hash['name'] = name
puts "Enter your address"
address = gets.chomp.downcase
new_hash['address'] = address
puts "Enter your ph number"
number = gets.chomp
number = number.split.join
new_hash['number'] = number
puts "Enter your email"
email = gets.chomp.downcase
new_hash['email'] = email
puts "Enter your bank balance"
bank_balance = gets.chomp
bank_balance = "$" + '%.2f' % bank_balance
new_hash['bank_balance'] = bank_balance
customer << new_hash
binding.pry
puts "Thankyou, details successfully Added"
break
else
break
end
end
you should remove everything inside initialize(...) and instead use
attr_accessor :name, :address, :phone, :email, :bank_balance) write it above your initialize statement
for more information on attr_accessor see this post What is attr_accessor in Ruby?
also follow it up by reading up on attr_reader and attr_writer
you can then assign values to those initialized variables in the rest of your code.
you will call your class by running Customer.new
you can get a little more snap out of your code by assigning your gets.chomp values to additional methods, then you can do something like
class Customer
attr_accessor :name, :address, :phone
def initialize
#name = ''
#address = ''
#phone = ''
end
def name
puts "Enter your name"
name = gets.chomp.downcase
end
def address
puts "Enter your address"
address = gets.chomp.downcase
end
def phone
puts 'enter your phone'
phone = gets.chomp
phone = phone.split.join
end
def all
name
address
phone
end
end
customer = Customer.new
customer.all

How can user input dynamically create objects?

I would like users to be able to dynamically create objects of the Incomes class below. That is, I would like to fire my program and let users enter as many incomes as they like, all stored as instances of the Incomes class.
def prompt
puts "> "
end
class Incomes
def initialize(aName, aAmount, aCOLA)
#name = aName
#amount = aAmount
#COLA = aCOLA
end
end
def addIncome
puts "What is the company name?"
prompt
aName = gets.chomp
puts "What is the monthly amount?"
aAmount = gets.chomp
puts "What is the cost of living adjustment?"
aCOLA = gets.chomp
end
#Now I want to be able to loop back through addIncome and create as many objects as the
#user wants. Perhaps there's a better way to store this type of data?
def prompt question
print "#{question} > "
gets
end
class Incomes
attr_reader :name, :amount, :COLA
##instances_of_Incomes = Array.new
def initialize(aName, aAmount, aCOLA)
#name = aName
#amount = aAmount
#COLA = aCOLA
#instances_of_Incomes = Array.new
end
def self.addIncome
name = prompt "What is the company name?"
amount = prompt "What is the monthly amount?"
_COLA = prompt "What is the cost of living adjustment?"
##instances_of_Incomes << Incomes.new(name, amount, _COLA)
end
def self.instances
##instances_of_Incomes
end
end
5.times do
Incomes.addIncome
end
puts Incomes.instances
Incomes.instances.each do |company|
puts company.name
end
I have refactored the code to show that you can use inputs to create the instances. They are unnamed classes, but stored in a class variable.
I also show that you can extract the name of each Incomes instance.
I have also edited your SE Code Review question, with the same code, so hopefully you can get some good reviews.

Checking Variables In The Constructor

I am learning ruby and know that in other languages I can ensure that variables are not empty when initializing.
How would I do this in Ruby with this example?
class Person
attr_reader :name
def initialize name
#name = name
end
end
Without setting a default name, and raising an exception if an empty string is sent (not nil) as the argument, then you could raise an exception.
class Person
attr_reader :name
def initialize name
raise "Name can not be an empty String" if name.empty?
#name = name
end
end
Then if you were to send an empty string you would get an error similar to this:
temp.rb:5:in `initialize': Name can not be an empty String (RuntimeError)
from temp.rb:11:in `new'
from temp.rb:11:in `<main>'
shell returned 1
As far as the part of "It makes them enter another name until valid" as you mentioned in another comment... you should let the running code do this.
This is allowing no default, but forcing the user to use something other than an empty string.
class PersonNameError < Exception
end
class Person
attr_reader :name
def initialize name
#name = name
end
end
begin
print "What is your name? "
name = gets.chomp
raise PersonNameError if name.empty?
person = Person.new(name)
rescue PersonNameError
if name.empty?
puts "You can not have a name that is ''"
retry
else
raise
end
end
puts person.name
It all depends on what you mean by empty. If you mean calling Person.new without the name variable set like:
a = Person.new
then you should define a default value for it:
def initialize( name = 'Jeffrey' )
#name = name
end
If you mean a nil value then you can do:
def initialize name
#name = (name.nil?) ? 'Jeffrey' : name
end
then from here I think you know how to go with empty strings ( #name = (name == '') ? 'Jeffrey' : name ) and so on.
I'm sure people who throw some interesting and much better answers but here's mine.
class Person
attr_reader :name
# By setting name = nil, we are letting initialize
# know that the parameter name may or may not be given.
def initialize(name = nil)
if name
#name = name
else
# This is what you do when the user
# did not add a name in the initializer.
#name = "No name"
end
end
end
So if you do something like
person = Person.new
the person will have the name No name.
However, if you do
person = Person.new("Bob")
then person will have the name Bob.
Finally if you set a variable as nil, for example
name = nil
then pass the name into the new method
Person.new(name)
then the person will have the name No name.
Based on what the other answers are doing, the best way IMO is:
def initialize(name = 'Default name')
#name = name
end
After your comment...
Is this Rails? That would change a few things. But you can just nil check the value:
def initialize(name)
if name.nil? || name.empty?
# raise an exception
else
#name = name
end
end
If you're using Rails, you can also just use blank? which will check both nil? and empty?.
Why not accept the user name from within the constructor / initialize method?
def initialize name
begin
while name.empty?
puts "You can not have a name that is ''"
print "What is your name? "
name = gets.chomp
end
#name = name
end
end

getting variable value dynamically

I have the following class
class User
attr_accessor :name, :age, :address
def initialize()
self.name = "new user"
self.age = 19
self.address = "address"
end
end
What I want if to have a method to get the assigned values to the attributes. Means, I have another method to get all the method names inside the above class
methods = self.public_methods(all = false)
and I can get the method name from that (I mean the getter of name etc..) and I want a way to pass that method name (which I have it as string) and get the return value of the method
Ex:
when I pass 'name' as the method name I should be able to get 'new user' as the value
hope I made my question clear, thanks in advance
** Please note, i have to use this way as my class has so many attributes and it has so many inherited classes. So accessing each and every attr individually is not possible :D
That’s what send is for:
user = User.new
user.name # => "new user"
user.send(:name) # => "new user"
getters = user.public_methods(false).reject { |m| m =~ /=$/ }
getters.each { |m| puts user.send(m) }
Using instance_variable_get is another option if you don’t have an accessor method:
user.instance_variable_get(:#name) # => "new user"
Using public_methods to get a list of attributes could be dangerous, depending on how you determine which methods to call. Instead, you could create your own class method which both defines the accessor and stores the attribute name for future use:
class User
class << self
attr_reader :fields
def field (*names)
names.flatten.each do |name|
attr_accessor name
(#fields ||= []) << name
end
end
end
field :name
field :age
field :address
end
user = User.new
user.name = "Me"
user.age = 22
user.address = "1234"
user.class.fields.each do |field|
puts user.send(field)
end

Resources