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.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In Ruby code, I noticed some variables or other objects placed in between | |. I have no idea why. Is it usually used in hashmaps?
These are arguments to a closure, a little anonymous function.
my_method do |argument1, argument2|
puts argument1, argument2
end
|argument1, argument2| says it takes two arguments. Just like if you wrote a method you'd say def method(argument1, argument2). Then my_method can accept the closure using & and call it using call.
def my_method(&block)
block.call(23, 42)
end
This is how Ruby does iterators. For example, here's how you'd implement map.
def my_map(list, &block)
list.each do |element|
block.call(element)
end
end
my_map([23, 42, 99]) do |element|
puts element
end
Or you can use yield which implicitly calls the block and it's slightly faster.
def my_map(list)
list.each do |element|
yield element
end
end
A method can check if a block was passed in with block_given?. This is how, for example, File.open can either return an open filehandle, or it can give it to a block and close it when the block is done.
def open(filename, mode='r')
file = File.new(filename, mode)
if block_given?
yield file
file.close
else
file
end
end
# open foo.txt
puts open("foo.txt")
# open foo.txt, execute the block, and close it
open("foo.txt") do |file|
puts file.read
end
This is very useful for when you need to take action, such as closing a file or shutting down a connection, once the work is done,
For more see Block Arguments in the Ruby Docs.
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"
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 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)$
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 was wondering if something like this would be possible say we have
def call_something
yield a
end
where a is an undefined variable? Is this kind of thing possible and if so can you provide an example of how it can be useful?
I know you can pass arguments to yield but so far I know you can only pass actual arguments that have actual values.
To clarify, I meant something like this:
class A
def initialize
print "Enter a value: "
#a = gets.chomp
end
def m
yield #a
end
end
a = A.new
a.m do |x|
puts "You entered #{x}"
end
Where you could supply something and then pass a block using that 'something' as an argument.
Even if you could, I'm not sure it would make sense, as to use that value in the passed block you would have to assign it to an identifier:
call_something do |arg|
# you want `arg` to be the "unidentified" value
end
At that point, it's not the same "unidentified variable" you were talking about before, and the only way you can really represent it is as nil. So you may as well just pass nil in the first place