Ruby class to get sales tax [closed] - ruby

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 9 years ago.
Improve this question
What am I doing wrong here? I'm trying to get sales tax and eventually use it for math calculations?
class Item
def initialize(type)
#type = type
def tax_calc(type)
if type.include?("book")
sales_tax = 0
else
sales_tax = 2
end
end
end
puts "what is type"
type2 = gets
Item.new(type2)
puts sales_tax

Your current code is missing an end and you have a nested method definition, which is a very advanced topic, and I don't see it used very often except as a toy.
This code will return the tax number.
class Item
def initialize(type)
#type = type
end
def tax_calc
if #type.include("book")
#sales_tax = 0
else
#sales_tax = 2
end
end
def sales_tax
tax_calc
end
end
puts "what is type"
type = gets
purchase = Item.new(type)
puts purchase.sales_tax
I changed type2 to simply type because there is no reason to worry about mirroring the local variable inside the class due to scope.
This code is far from being optimal, but it is at least 'working code'.

In your code,sales_tax is a local variable of the initialize method. It doesn't exist outside of that scope.
Here's one way to get the sales tax:
class Item
def initialize(type)
#sales_tax = type.include?('book') ? 0 : 2
end
def sales_tax
#sales_tax
end
end
item = Item.new('foo')
puts Item.new('foo').sales_tax

Related

In Ruby when you initialize a class do you set an instance variable equal to a variable? [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
Why in ruby, when you initiazlize a class do you set the instance variable equal to a variable of the same name?
def initialize(number)
#number = number
end
We do that so that newly-created object (not a class! with initialize and #vars, you initialize the object that was just created with new() method!) remembers the value of number.
Try using this one:
def initialize(number)
end
This gets a number, but does nothing with it. When this inializer ends, the object created will not remember what was the 'number'.
Here:
def initialize(number)
#foo = 5
#bar = number
end
the newly-created object will remember a 5 in #foo and the number in #bar.
The idea to name the #variable just like the parameter is just to make it easier. In the example above, it's hard to guess what the bar is about. Instead, if I rename the #bar into #number, it wil be obvious that it holds .. the number.
def initialize(number) def initialize(number)
#bar = number <-same thing-> #number = number
end just different name end

Is this a good metaprogramming practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I wanted to make a simple DSL where I could pass a bunch of methods to a block, relying on self as the implicit receiver. So basically here you can call the 'say' method on this class object, passing it 'things to say' as methods in the block. The last line returns ['Maria']. I was wondering if this is a good programming practice for creating DSLs and if there are any problems with this approach.
class SaySomething
def initialize
#said = []
end
def hey(name)
#said << name
end
def say(&block)
instance_eval(&block)
end
end
a = SaySomething.new
name = 'Maria'
a.say do
hey(name)
end
a.instance_eval { p #said } #=> produces ['Maria']
I would probably add an attr_accessor :said and then replace your last line with
a.said
#=> ['Maria']
Other than that your code looks fine to me. If you want to learn more about metaprogramming in Ruby, I can recommend the book "Eloquent Ruby".
The only problem with this approach is that any class variables will collide with variables in the same scope as the block. The usual approach is to provide instance evaluation, but also allow the user to specify the class as an argument as a fall back.
class Test
def test; "hello"; end
def say(&b)
if b.arity == 1
b.call(self)
else
instance_eval &b
end
end
end
t = Test.new
test = "fred"
t.say { p test } # "fred"
t.say { |t| p t.test } # "hello"

Ruby square bracket setter/getter [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
Is it possible in Ruby to define such setters and getters for class that can be used with [] or () or smth alike? E.g.
word.meaning[:english] = "ruby"
puts word.meaning[:german] # "Rubin"
Note that word.meaning must not be a hash! and :english, :german are kind of additional parameters for setter/getter meaning.
Yes, it can be done. You need to define a []= and [] methods.
In this example, I am using a Hash as the internal data structure - you are free to use whatever you like.
class Word
attr_reader :meaning
def initialize
#meaning = Meaning.new
end
end
class Meaning
attr_reader :h
def initialize
#h = {}
end
def []=(key, value)
#h[key] = value
end
def [](key)
#h[key]
end
end
Example:
word = Word.new
word.meaning[:english] = 'Hello'
word.meaning[:english] # => 'Hello'

How do I get my code to loop? [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 9 years ago.
Improve this question
im trying to get my code to loop (im very new to ruby)
I want to make the program loop when it doesnt.
I am told that my code is very wrong, but is there anyway I can get it to loop anyways?
class Screen
def clear
end
def pause
STDIN.gets
end
end
class Lottery
attr_accessor :greeting, :register, :morereg, :goodbye, :lotterynumbers, :randomtwenty
def initialize
end
def say_greeting
greeting = "\n\n\n\t\t ***Welcome to elGordo lottery!***\n\n" +
"\t\t Press Enter to continue.\n\n\n"
print greeting
end
def say_register
register = "Type in the lotterynumber you want to register and then press Enter.\n\n"
print register #nedenunder [#counter]
Lotterynumbers1 << gets.chomp.to_i
end
def say_morereg
morereg = "would you like to type in another lottery number? (y/n)\n"
print morereg
gets.chomp!
if gets.chomp! == "y" then
say_register
else
puts "\nThank you for the register.\n\n"
end
end
# def say_randomtwenty
# Lotterynumbers1 << << 20.times.map { rand(00000..99999).to_i }
# end
def say_goodbye
goodbye = "\nThank you for the register.\n\n"
print goodbye
end
end
#main Script Logic
Lotterynumbers1 = []
Console_Screen = Screen.new
Hej = Lottery.new #starter nyt Lotteri object
Hej.say_greeting
Console_Screen.pause
Hej.say_register
Hej.say_morereg
puts Lotterynumbers1
you got confused with conditional statement and looping thinking if user go on says yes(y), then it would be a kind of looping(executing the same procedure many times) by your code.
But its not

Basic Class Inheritance [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
I have two files:
answer.rb
class Answer
def decision(string)
if string == 'Richard'
puts "Hello"
else
puts "I dont know you"
end
end
end
question.rb
require './answer'
class Question < Answer
puts "What is your name?"
response = gets.chomp
puts decision("#{response}")
end
How do I access the method in class Answer from class Question given the file is not enough? If I remove the the class Answer, then everything works.
To make your example work you need to call your code. For instance you can amend your Question class with the following code :
#question.rb
require './answer'
class Question < Answer
def ask
puts "What is your name?"
response = gets.chomp
puts decision(response)
end
end
Question.new.ask
The inheritance will be that your instance of question (i.e. Question.new) will be inherited from Answer => it will have both methods (in your case 'ask' and 'decision').
Just do
puts self.new.decision(response) # no need for string interpolation.
#decision is an instance method of Answer class, so it will be available as an instance method of the class Question. Now inside the class, self is set to the class itself, thus the bare method call like your, will throw error as no method found. Thus you have to create an instance of the class Answer or Question, and on that instance you have to call the method.
Complete code :
class Answer
def decision(string)
if string == 'Richard'
puts "Hello"
else
puts "I dont know you"
end
end
end
class Question < Answer
puts "What is your name?"
response = gets.chomp
puts self.new.decision(response)
end
Run the code:
(arup~>Ruby)$ ruby so.rb
What is your name?
arup
I dont know you
(arup~>Ruby)$

Resources