= gets.chomp ... not working in terminal [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
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.

Related

Data Parsing 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 2 days ago.
This post was edited and submitted for review 2 days ago.
Improve this question
I like to parse data like:
data = [{"dev_type"=>[{"rate"=>1000, "class_type"=>"2020"}], "sys_type"=>{"techs"=>[{"tech"=>"2", "type"=>"classA"}]}, "MOD"=>[{"id"=>"00:00:00:08:00:00", "add"=> "0"}], "user"=>"test", "host_id"=>1, "SYSTEM"=>2}]
for instance if i like to extract "rate" in dev_type.
data.each do |d|
pp d["dev_type"][0]["rate"]
end
# >> 1000
same to extract for type in sys_type i need:
data.each do |d|
pp d["sys_type"]["techs"][0]["type"]
end
# >> ClassA
But this i think not the right way doing for extracting data, what i wanted to do is to extract values like "type" in sys_type, "rate" in dev_type, "id" and "add" in MOD , and "user" . What is the best way of doing this.

Ruby <, <=, >, >= value comparison code [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
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

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 metaprogramming better solution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How this will work without eval
%w{user_id for_whom_id category_id product_status_id}.each do |f|
code = <<-C
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.#{f} = nil }
end
C
eval code
end
?
This should work:
%w{user_id for_whom_id category_id product_status_id}.each do |f|
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.public_send "#{f}=", nil }
end
end
It's best to avoid the eval if possible. And in this case, it isn't necessary.

Resources