Ruby <, <=, >, >= value comparison code [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 6 years ago.
Improve this question
The intent is to display message based on age.
puts "Age, please"
value = gets.chomp
if value < 21
puts "Here you cannot to buy alchohol"
end
puts "You can buy all the alchohol you want"
What is the missing part in this code?

if you get value by gets, value is String.
Use value.to_i
puts "Age, please"
value = gets
value = Integer(value) rescue 0
if value < 21
puts "Here you cannot to buy alchohol"
else
puts "You can buy all the alchohol you want"
end

Related

= gets.chomp ... not working in terminal [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 6 years ago.
Improve this question
puts 'Hi, What\'s Your Name?'
name = gets.chomp
puts = 'Nice, to meet you ' + name
When I run in terminal, it allows me to type in name. "Bob"
But it stops. Does not put "Nice to meet you Bob"
Change
puts = 'Nice, to meet you ' + name
to
puts 'Nice, to meet you ' + name
otherwise you're storing the result in the variable puts rather than printing it out.

Converting Strings to Symbols and Integers inside my hash 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
I made it this far, but don't know where to add the .to_i and the .to_sym methods since I'm assuming I put them in the wrong place. Can someone help me out? When I test the code out it says "undefined method `to_sym=' for nil:NilClass"
movies = {
spiderman: 3,
superman: 4,
batman: 5
}
puts "what movie do you like?"
choice = gets.chomp
case choice
when 'add'
puts "What movie do you want to add?"
title.to_sym = gets.chomp
puts "what is the rating of that movie?"
rating.to_i = gets.chomp
movies[title]=rating
puts "Added!"
when 'update'
puts "What movie would you like to update?"
title = gets.chomp
puts "Updated!"
when 'display'
movies.each do |movies, ratings|
puts "#{movies}: #{ratings}"
end
puts "Movies!"
when 'delete'
puts "What movie would you like to delete from the list?"
title = gets.chomp
puts "Deleted!"
else
puts "Error!"
end
Besides []=, there is no method of the form foo=, or to_sym=, as in your code, in plain Ruby. If you want to get a user input as a symbol, you can do:
title = gets.chomp.to_sym
If you want to get an integer, you can do:
rating = gets.to_i
I don't know why you had chomp in the latter case.

How can I produce one whole string from iteration on array of arrays 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
I have array of arrays looking something like this :
arr = [[f,f,f,f,f], [f,f,t,f,f], [f,t,f,t,f]]
and am I outputing it formatted on the console like this:
arr.each {|a| puts a.join.gsub('t','<b></b>').gsub('f','<i></i>')}
and it generates something like this:
<i></i><i></i><i></i><i></i><i></i>
<i></i><i></i><b></b><i></i><i></i>
<i></i><b></b><i></i><b></b><i></i>
but it is only in the output. I am wondering how I can assign it to a string? With the new lines and everything, exactly the way it looks,
a= [["f","f","f","f","f"], ["f","f","t","f","f"], ["f","t","f","t","f"]].map do |arr|
arr.join.gsub(/[ft]/) do |x|
if x =~ /f/
'<i></i>'
elsif x =~ /t/
'<b></b>'
end
end
end.join("\n")
puts a
# >> <i></i><i></i><i></i><i></i><i></i>
# >> <i></i><i></i><b></b><i></i><i></i>
# >> <i></i><b></b><i></i><b></b><i></i>

How can I print out the value of each key in a hash represented by *'s? [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
Here is my code:
class String
def frequency
chars.each_with_object(Hash.new(0)) do |char, h|
h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/]
end
end
end
I've tried breaking it down in smaller bit's of code, such as using a .times do loop but I couldn't figure it out
for example:
str = "\*"
h["A:"] = count('a').times do
str
end
Are you trying to do something like:
counts = 'aassssvvvvv'.frequency
counts.each{|key,count| puts key + '*'*count}
# A:**
# S:****
# V:*****
Or if you want to change the key you can do:
counts.each{|key,amount| counts[key] = '*'*amount}

Ruby Trying to get a 3 line return [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
Hi I'm trying to get the program to return all three lines on top of one another when a 5 is entered. It's only returning the third line. This is Ruby. (My first time trying it)
moveOne = gets.to_i
if moveOne == 5
puts = "1,2,X"
"4,O,6"
"X,8,9"
puts is a method that accepts one or more arguments and writes them (their #inspect-ed value) to an IO object separated by a newline. As written you are trying to assign puts a value rather than passing the values as parameters.
Try this
puts "1, 2, X",
"4, 0, 6",
"X, 8, 9"
That's passing three strings to puts and preserves your desired readability.
moveOne = gets.to_i
if moveOne == 5
puts "1,2,X"
puts "4,O,6"
puts "X,8,9"
end
P.S: Please try to learn more before posting a question.

Resources