How to refactor following unless statement in Ruby [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 8 years ago.
Improve this question
While learning Ruby, I got stucked with unless statement. Doing a full unless statement is sometimes too much. Please refactor the method below to use a single-line unless statement
games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
unless games.empty?
puts "Games in your vast collection: #{games.count}"
end
This code doesn't changes the unless statement to be inline, Please help me to modify this code so that it may work, Thanks.

use then
games = ["Super Mario Bros.", "Contra", "Metroid", "Mega Man 2"]
puts "Games in your vast collection: #{games.count}" unless games.empty?
# >> Games in your vast collection: 4

Related

Parse code file [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 5 years ago.
Improve this question
nd maybe there is some tool for this? I must extract entire blocks and subblocks like 'it'
I doubt any parser could help to load code blocks into variables. It would require eval, but even with eval it would be extremely hard to collect all the context etc.
It the target is rspec scenarios, I would go with monkeypatching rspec core, prepending your own detectors like:
def before(*args, &block)
MyCollector.collect_block(block)
super(*args, &block)
end
You can parse it with
https://github.com/seattlerb/ruby_parser
or
https://github.com/whitequark/parser
and will receive an AST (Abstrax Syntax Tree) which you then can process further. Depending on the amount of details you need from the source, you could also use some Regexps or write your own parser...
Perhaps you can tell us a little more about your project (input, output, reasons)

Inhibiting repetition with user input [closed]

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 6 years ago.
Improve this question
I need a code that collects a user's input with gets.chomp, and if they repeat the same answer twice, it will read an error message. What is the best method to do this? Is there a way to collect the answers and then continue to check for duplicates?
Keep an history of answers.
history = []
loop do
answer = gets.chomp
if history.include? answer
puts "already answered"
next
else
history.push answer
end
# do something
end

Time difference compare to presence like Twitter in Ruby [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
Is there a gem or another easy way to get time difference compare to present time like Twitter?
Of course I can write it myself, but I don't want to reinvent the wheel.
If you are using rails, you could do
distance_of_time_in_words_to_now #user.created_at
#=> "15 days"
or
time_ago_in_words #post.created_at
#=> "about 13 hours"

Should I use File.write? [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 5 years ago.
Improve this question
I recently run into problems converting a ruby script to .EXE because I had a File.write statement in it. The documentation doesn't mention the write method but when I do a
pp File.methods
it is there. So should I use File.write? In a normal Ruby script the following works
File.write("test.txt", "test")
But is it good practice and why doesn't the documentation mention it?
File.write is in fact IO.write (File is a child of IO) which can be verified by monkey-patching:
class IO
def IO.write
puts "IO's class method write was called."
end
end
File.write # outputs "IO's class method write was called."
It is very well in the documentation.
Thus, I see no reason not to use it.

Ruby 1.9, return array if keys include a particular object [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
Code:
#albums = #genres.each_with_index { |item,key|
if item.keys.include?('Albums')
break
end
}
This should be returning the Albums array (the #genres object is a huge multidimensional JSON response)
I reckon this is what I get for trying to code while being sick... or just simply doing things wrong... either way, any help is much appreciated!
I think you want #detect (or its synonym #find):
#albums = #genres.detect { |item| item.key?('Albums') }['Albums']
EDIT | Also note that you can provide an argument to break just like you can do with return, if you want to break and return a specific value.

Resources