Initialize method arguments issue in ruby [closed] - ruby

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
"In my example, the initialize method expects to receive the two arguments. Once you associate an argument with the initialize method, you can not leave it off. If you do, it will generate an error." - so much for the theory but in my case it is the opposite ;(
class Player
attr_accessor :name, :age, :score
def inicialize(name, age, score)
#name = name
#age = age
#score = score
end
def to_s
"Player #{name} is #{age} old and have #{score} points"
end
end
So I call it simply:
player = Player.new("Name", "Surname", 25)
but the compiler responds:
`initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)
So, than I do:
player = Player.new; player.name = 'Peter'
and it works.... why?
When I delete the arguments of inicialize it is the same exact story.

it should be initialize, not inicialize.
class Player
attr_accessor :name, :age, :score
def initialize(name, age, score)
#name = name
#age = age
#score = score
end
def to_s
"Player #{name} is #{age} old and have #{score} points"
end
end
player = Player.new("Name", "Surname", 25)
#=> #<Player:0x007fca2a4dfbb0 #age="Surname", #name="Name", #score=25>

Related

Nested class initialize not being called [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am creating a custom Trie as follows:
# frozen_string_literal: true
class CustomTrie
attr_accessor :trie
def initialize(items)
end
def self.parse_to_trie(items)
end
def get(path)
end
class Node
attr_accessor :key, :parent, :children,
def initialize(key: '', parent: nil, children: [])
# This isn't being called, why?
#key = key
#parent = parent
#children = children
end
def is_parent?
end
def is_leaf?
end
def inspect
{key: #key, parent: #parent, children: #children}
end
end
class Trie
attr_accessor :root
def initialize(root = Node.new)
#root = root
end
def add(path)
end
def get(path)
end
end
end
However when I try calling CustomTrie::Node.new everything is initialized to nil instead of the default values, and when I try calling the constructor with values I get the error: "ArgumentError (wrong number of arguments (given x, expected 0))"
I'm sure I'm missing something obvious, but I haven't been able to identify what I'm doing wrong.
:facepalm:
It turns out it was because I had a comma after :children in my attr_accessor call.

How can I create a to_s with an instance variable pointing to an array of objects from another class?

With the code below, I would like to create a to_s method that prints out information as such:
Southside has 3 team members.
Those members are: Dario, who is 22 years old. Ted, who is 21 years old. Bob, who is 44 years old.
Currently, I get this:
Southside has 3 team members.
Those members are:
[#<Person:0x000000025cd6e8 #name="Dario", #age=22>, #<Person:0x000000025cd670 #name="Ted", #age=21>, #<Person:0x000000025cd620 #name="Bob", #age=44>].
#<Team:0x000000025cd7d8>
The part I'm finding difficult is accessing the instance variables of the Person class objects that are in the Team members array.
Here are the two classes:
class Team
attr_accessor :name, :members
def initialize(name)
#name = name
#members = []
end
def <<(person)
members.push person
end
def to_s
puts "#{#name} has #{#members.size} team members."
puts "Those members are: #{#members}."
end
end
class Person
attr_accessor :name, :age
def initialize(name, age)
#name = name
#age = age
end
end
south_side_bowlers = Team.new("Southside")
south_side_bowlers << Person.new("Dario", 22)
south_side_bowlers << Person.new("Ted", 21)
south_side_bowlers << Person.new("Bob", 44)
puts south_side_bowlers
Define to_s ("#{#name}, who is #{#age} years old") for the Person class. Then you can do #members.map{ |m| m.to_s}.join('. ')
First off, you don't want to have puts in a to_s method. Instead just return the string. Second, the variable members is probably not actually what you want outputted in the method. Try this instead
def to_s
%Q(#{#name} has #{#members.size} team members. Those members are #{#members.map{|i| "#{i.name} who is #{i.age}"}.join(', ')})
end

how to make a class that makes multiple objects? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I make a class that makes multiple classes. I have this:
class Person
attr_accessor :name
#name = name
def initialize
Person.new
Person.new
Person.new
Person.new
Person.new
Person.new
Person.new
Person.new
end
end
but that returns stack level to deep.
I wasn't clear where you wanted to get the names from -- External file? Manual Input? Database?
In any case, you could probably do something like:
class Person
attr_accessor :name
def initialize(name)
self.name = name
end
end
##some sort of input goes here and creates the array of names
arrayofnames = [name1,name2,name3]
arrayofnames.each do |person|
Person.new(person)
end
As part of the same enumeration you could put each new person into an array or store them somewhere else for later use. Here I built the class and added the people to it separately, although you could probably build the same enumeration into the class itself.
Hope that helps,
The problem that you are facing is that you are creating a person which in turn is creating 10 other person objects which are all returning 10 person objects. This continues on indefinitely.
What you want is:
class Person
attr_accessor :name
#name = name
end
class People
#people
def initialize()
people = []
for i in 0..10
people[i] = Person.new
end
end
end
This creates another object that in turn contains 10 Person objects. This way there is no way for the same recursive problem to happen.
First of all, this is your Person class:
class Person
attr_accessor :name
def initialize(name)
#name = name
end
end
If you want another class to create x number of Person instances you can use the following PeopleCreator class:
class PeopleCreator
def self.create_person_for(names)
new.create(names)
end
def create(names)
names.map { |name| Person.new(name) }
end
end
I've used a class method in the PeopleCreator to be able to easily call the following:
names = %w(John Jane Jake)
PeopleCreator.create_person_for(names)
# => [#<Person:0x0000000a743150 #name="John">, #<Person:0x0000000a743128 #name="Jane">, #<Person:0x0000000a743100 #name="Jake">]

What is wrong with this Ruby code 2? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is wrong with this code?
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{name}"
end
end
Write the code as
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{#name}" # <~~ you missed `#` before name.
end
end
If you write only name(instead of #name), Ruby will try to look for a local var named as name, but you didn't define any. Then it will try to check if any method you have defined as name or not, that also not present. So finally you will get
undefined local variable or method `name'
Here is an example after the fix :
#!/usr/bin/env ruby
class Person
def initialize(name)
#name = name
end
def greet(other_name)
"Hi #{other_name}, my name is #{#name}"
end
end
Person.new("Raju").greet('Welcome!') # => "Hi Welcome!, my name is Raju"

Practicing defining Classes and Methods in Ruby [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having the following error "Line 46: undefined local variable or method `app1' for main:Object (NameError)" when I run the following Ruby code about Methods and Classes on the compiler.Thanks in advance :D!!
class Apps
def initialize(name)
#name = name
end
def add_app
"#{name} has been added to the App Center.Approval is pending!!"
end
def app_approved
"#{name} has been approved by the App Center"
end
def app_posted
"Congratulations!!!!#{name} has been posted to the App Store."
end
end
class Fbapps
def initialize(name)
#name = name
#apps = []
end
def add_new(a_app)
#apps << a_app
"#{#app} has been added to the #{#apps} store!!"
end
def weekly_release
#apps.each do |app|
puts #app
end
#apps.each do |app|
app.add_app
app.app_approved
app.app_posted
end
end
end
apps = ["Bitstrip", "Candy Crush" , "Instapaper"]
apps = Fbapps.new("Apps")
apps.add_new(app1)
apps.add_new(app2)
apps.add_new(app3)
puts apps.weekly_release
app1 = Apps.new("Bitstrip")
app2 = Apps.new("Candy Crush")
app3 = Apps.new("Instapaper")
You need to create app1, app2, and app3 before adding them to apps:
apps = ["Bitstrip", "Candy Crush" , "Instapaper"]
app1 = Apps.new("Bitstrip")
app2 = Apps.new("Candy Crush")
app3 = Apps.new("Instapaper")
apps = Fbapps.new("Apps")
apps.add_new(app1)
apps.add_new(app2)
apps.add_new(app3)
puts apps.weekly_release
As noted there are other bugs in your classes, but they should be relatively trivial to fix given changing the order of execution as above.
Update: Here's your code updated to fix most of the bugs:
class Apps
attr_accessor :name
def initialize(name)
#name = name
end
def add_app
"#{name} has been added to the App Center.Approval is pending!!"
end
def app_approved
"#{name} has been approved by the App Center"
end
def app_posted
"Congratulations!!!! #{name} has been posted to the App Store."
end
end
class Fbapps
attr_accessor :name
def initialize(name)
#name = name
#apps = []
end
def add_new(a_app)
#apps << a_app
"#{a_app.name} has been added to the #{self.name} store!!"
end
def weekly_release
#apps.each do |app|
puts app.name
end
#apps.each do |app|
puts app.add_app
puts app.app_approved
puts app.app_posted
end
end
end
You're trying to do apps.add_new(app1) before you define app1. That line needs to go after app1 = Apps.new("Bitstrap") .

Resources