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
Please help me implement class Repeat and it's methods:
class Repeat
def initialize(n)
#TODO
end
def each
#TODO
end
end
def n_times(n)
#TODO
end
This piece of code:
n_times(2) { |count| puts "You called me #{count} times" }
should return this result:
# You called me 1 times
# You called me 2 times
Welcome to StackOverflow. It seems like you are new to OOP and passing a block to a method in Ruby. This answer simplifies your question and just focuses on passing a block to a method. Here is some functioning code:
def n_times(n, &block)
n.times do |counter|
yield(counter + 1)
end
end
n_times(2) { |count| puts "You called me #{count} times" }
Related
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'
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
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
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
I'm new to ruby and I'am building a little program on ruby alone,the problem is that I'am trying to launch it without success
Imagine that i have this code
#usr/bin/ruby
Class Whatever
def get_user_input
#user_input = gets.chomp
user_doing(#user_input)
end
def user_doing
#something
end
end
What I want is to call the get_user_input method as soon as i feed my rb file to ruby
I tried to call it on a initialize method
def initialize
get_user_input
end
I also tried to define it as a "class method"
def get_user_input
#user_input = gets.chomp
user_doing(#user_input)
end
but neither of them seems to work as when I'm start the rb file the program doesn't expect my input so how can i do this?.
You just define a class. What you did not call the method. Just add Whatever.new.get_user_input to your file.
#usr/bin/ruby
class Whatever
def initialize(input)
#input = input
end
def self.get_user_input
whatever = new(gets.chomp)
whatever.user_doing
end
def user_doing
puts "Input was: #{#input}"
end
end
Whatever.get_user_input
Btw: Your user_doing does not take args in the moment. You may want to check that.
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 9 years ago.
Improve this question
What is "better" is Ruby
class BaseClass
def items
#items ||= get_items
end
def get_items
raise NotImplementedError
end
end
class ClildClass < BaseClass
def get_items
# ... fetching items...
end
end
or simply
class BaseClass
def items
#items ||= get_items
end
end
class ClildClass < BaseClass
def get_items
# ... fetching items...
end
end
?
It's up to you. There is no right answer for this. You will simply get 2 different errors. First coince should be better if someone else will implement other ChildClasses, because they can see the "interface" they need to implement on their BaseClass.