Stop rails console from printing out the object at the end of a loop
If you don't want to disable the echo in general you could also call multiple expressions in one command line. Only the last expression's output will be displayed.
big_result(input); 0
Stop console printing the same message multiple times when inputting an string instead of integer
If you can, switch the type of response
from int
to string
then parse the input string using package strconv
(such as strconv.Atoi()
). I suspect Scan
is trying to read each character you entered as a separate int, failing each time, and running the loop each time until all the characters have been consumed.
for example
for {
var response string
fmt.Printf("How many %v would you like to buy? ", product.Name)
_, err := fmt.Scanln(&response)
if err != nil { // probably don't need to check err from Scan()
fmt.Println(err)
continue
}
num, err := strconv.Atoi(response)
if err != nil {
fmt.Println("Enter an integer.")
continue
}
if ok, err := validResponse(num); ok && err==nil {
break
} else {
fmt.Println(err)
continue
}
}
How to suppress Rails console/irb outputs
You can append ; nil
to your statements.
Example:
users = User.all; nil
irb
prints the return value of the last executed statement; thus in this case it'll print only nil
since nil
is the last executed valid statement.
How can i make the loop end when user writes stop and to start a new number in the loop?
Here is a code solution I produced for this problem. Try it out and see if it is to your liking:
https://gist.github.com/BKSpurgeon/1a2346e278836d5b4448424cb93fd0e9
class NumberGuessingGame
def initialize
welcome
end
def welcome
puts "Welcome to Guess the Number \n Human VS Machine"
end
def guess
loop do
@number= rand(0..9)
p "Write a number between 0 and 9"
numer = -1
while numer != @number
numer = gets.chomp.to_i
if numer<@number
p "Too low"
elsif numer>@number
p "Too high"
elsif numer == @number
p "You got it!"
puts "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write stop otherwise press any key."
break
end
end
answer = gets.chomp # The Critical Line to break out of the Loop is here:
break if answer.downcase == "stop"
end
end
end
and you'd call it like this:
g = NumberGuessingGame.new
g.guess
that escapes if you write 'stop'. i made minor modifications of the functionality. The loop is broken if the "stop" answer is detected. that is the critical line.
I can't see a good reason for client code to be doing this type of thing:
game = NumberGuessingGame.new
# Pruebas
a = ""
p "Welcome to Guess the Number"
p "Human VS Machine"
while a != "Stop"
x = ""
while x != "you got it!"
p"Write a number between 0 and 9"
y = gets.chomp.to_i
p x = game.guess(y)
end
p "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write
stop or guess the new number"
NumberGuessingGame
a = gets.chomp
end
ideally you want to let all the methods in a class to do all the work - you should not have to write 'welcome to the game etc' from outside the class - that should be the responsibility of the NumberGuessingGame class.
hope this helps.
Ruby / Rails - .each Iterator is printing entire array at the end of the loop
# Change this line with an =:
<%= @categories.each do |c| %>
# ...to this:
<% @categories.each do |c| %>
You only want the side effects on the block of the #each
method, you don't want interpolation of the returned value.
Entire hash getting returned at the end of the loop
The each method returns the original enumerable object it was called upon, this is why you keep puts
ing the entire hash after the end of the loop.
Why is this rails view spitting out a raw array at the end of an .each do loop?
Try removing the equals sign in <%= @post.comments.each do |comment| %>
. The equals is only necessary if the method itself outputs something. In this case you're just using it to iterate a collection.
whole RUBY hashmap printed out after .each loop
You're getting this because you've included a =
sign in your .each
loop. When you write <%=
you're telling the .erb
interpreter that what's inside of the brackets is going to get displayed on the page. Change the line
<%= @tag_color_hash.each do |tag, color| %>
to
<% @tag_color_hash.each do |tag, color| %>
and your problem should get fixed. Here's a link to a useful StackOverflow answer.
Print object attributes in a table in rails console
For added flexibility, you can use something like terminal-table. With it, you can display anything in a table.
table = Terminal::Table.new do |t|
t << ['One', 1]
t << :separator
t.add_row ['Two', 2]
t.add_separator
t.add_row ['Three', 3]
end
puts table
# +-------+---+
# | One | 1 |
# +-------+---+
# | Two | 2 |
# +-------+---+
# | Three | 3 |
# +-------+---+
Related Topics
Link_To Method and Click Event in Rails
How to Create a Copy of Some Columns of a CSV File in Ruby with Different Data in One Column
Add Element to an Array If It's Not There Already
What Should Be Removed from Public Source Control in Ruby on Rails
Using Htaccess Password Protection on Rails
How to Time an Operation in Milliseconds in Ruby
Calling Method in Parent Class from Subclass Methods in Ruby
How to Install (Build) Ruby 1.9.3 on Osx Lion
How to Ban/Block Users with Devise for Rails
Two Controllers for One Shared View in Ruby on Rails
How to Get the Intersection, Union, and Subset of Arrays in Ruby
Rails Mapping Array of Hashes Onto Single Hash
Generate and Publish Ruby Based Rest APIs Documentation
How to Access Url Helper from Rails Module