So, I made a hash called "facing". I call a case statement after, where I want to subtract 1 from the value if the user types L, or (I didn't get here yet) add 1 if the input = 'R'.
class Rover
attr_accessor :orientation
end
#facing = Hash.new
facing = {
0 => 0
}
bot = Rover.new
bot.orientation = "N"
puts "Bot's current orientation is: " + bot.orientation
puts "What direction to turn ('L' or 'R')?"
input = gets.chomp.to_s.capitalize
case input
when input = 'L'
facing do |key, value|
value - 1
end
end
The problem is that I get a method undefined (facing) error message. What am I doing wrong?
Have a look at the documentation for case.
This statement: input = 'L' means "give the variable input the value 'L'", not "check if it equals 'L'".
But that's not where your error is coming from. With facing do... You're giving a block to a hash, which is confusing the compiler and causing it to look for a facing method.
What exactly are you trying to do with the hash?
Related
I'm trying to get an if statement for users who put incorrect data.
Here's my code:
class Breweries::CLI
def start
puts "Hello!"
puts "---------------------------"
puts "Please enter your location:"
input = gets.strip.downcase
#data = Breweries::API.get_breweries(input)
#objects = Breweries::HoppyCode.all
if input.length < 1
puts "Sorry!!"
puts "```````"
start
else
display_info
end
end
def display_info
puts "You'll love the following spots!"
puts "********************************"
#objects.each.with_index(1) {|brewery, index| puts "#{index}. #{brewery.name}"}
puts "Please make a selection by index number for more information:"
input = gets.strip.downcase
if(input.to_i > 0)
#brewery = #objects[input.to_i - 1]
puts "name: #{#brewery.name}"
puts "street: #{#brewery.street}"
puts "city: #{#brewery.city}"
puts "phone: #{#brewery.phone}"
puts "website_url: #{#brewery.website_url}"
display_info
elsif (input == "quit")
quit
elsif (input == "menu")
start
end
end
def quit
puts "Goodbye. Drink responsibly and enjoy."
end
end
When I put something that would generate an error, it returns the following:
Please enter your location: nvifpejvf80ejvip
Traceback (most recent call last):
2: from bin/breweriesCLI:6:in `<main>'
1: from /home/munificent-format-5297/Development/breweries/lib/breweries/cli.rb:8:in `start' /home/munificent-format-5297/Development/breweries/lib/breweries/api.rb:6:in `get_breweries': undefined method `[]' for nil:NilClass (NoMethodError)
How can I solve the undefined method '[]' error? Here's the API code in case that's necessary.
class Breweries::API
def self.get_breweries(input)
#breweries_hash = HTTParty.get("https://api.openbrewerydb.org/breweries?by_city=#{input}")
breweries_obj = {
name: #breweries_hash[1]["name"],
street: #breweries_hash[3]["street"],
city: #breweries_hash[4]["city"],
phone: #breweries_hash[10]["phone"],
website_url: #breweries_hash[11]["website_url"]
}
Breweries::HoppyCode.new(breweries_obj)
end
end
When the input is invalid, the call to
#breweries_hash = HTTParty.get("...")
returns not the object you expect (I’d suggest it returns an empty hash.) That makes it impossible to get to details in the following lines. Depending on how are you to handle it, you might decide to e. g. early return from this function, or raise, or do something else.
To approach this, start with debugging the issue, like this:
#breweries_hash = HTTParty.get("...")
puts #breweries_hash.inspect
...
That way you’ll see what gets returned and get the ideas of how to handle it.
If I am right, and what is returned is an empty hash, you might want to early return from this function.
#breweries_hash = HTTParty.get("...")
return if #breweries_hash.empty?
...
Identifying the Problem
There are lots of ways to solve for the nil problem, but at a quick glance it seems like part of the problem here is that you're somehow expecting input to return a valid Hash object from your API call, but an empty String or an instance of FalseClass may not do that. Consider the following:
input = gets.strip.downcase # <RETURN> here gets an empty string
input #=> ""
input.to_i > 0 #=> false
Then consider that some downstream of Breweries::API.get_breweries is expecting to operate on a Hash object instead if an instance of NilClass. In this case, that looks like #breweries_hash[1]["name"] and other operations on #breweries_hash.
Some Options
Without knowing more about your code, I don't want to be prescriptive here. But in general, you can do one or more of the following:
Coerce arguments into the expected class in the method call, the method signature, or the method body. For example, for Array objects:
# coerce a String to an Array, raising an exception if it can't
input = ""
Array(input)
#=> [""]
# coerce some Array to a Hash
array = [:name, "foo", :street, "bar"]
Array(array.each_slice 2).to_h
#=> {:name=>"foo", :street=>"bar"}
Explicitly check that you have an Hash object:
fail "#breweries is not a Hash" unless #breweries.is_a? Hash
Raise an exception rather than return 0 if input isn't actually a valid Integer representation in the first place:
input = Integer(gets.strip.downcase)
Check if your Hash or Array object responds to the relevant method calls, and raise a more helpful exception message:
raise sprintf("#brewery: %s", #brewery.class) unless #brewery.respond_to? :[]
There are other things you might do as well. Broadly speaking, you need to adjust your code to check the return value of your call to ensure it's not nil, then branch/raise/rescue appropriately depending on whether or not you ever expect nils as a valid return value from Breweries::API.get_breweries.
A Note on Using Exceptions for Non-Exceptional Circumstances
As a rule of thumb, you should only raise exceptions for truly unexpected circumstances, or when the program should halt because some condition can't (or shouldn't) be handled within the program during runtime. Which is best in your particular use case is really a design decision, and outside the scope of the original question. However, you might want to read Avdi Grimm's Exceptional Ruby for a deeper explanation of when exceptions might better than branching or handlers (or vice versa), but the choice in your code is a little left of center of the problem you're actually dealing with right now.
I'm trying to sum the value of a specific field every time it shows, the field is in this format: [cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets] and its value is a numeric field (it shows the number of bits consumed).
What I'm trying to do is the following:
a = event.get("[cdr][Service-Information][PS-Information][Service-Data-Container][Accounting-Output-Octets]")
if a
sum = 0
a.each_index { |x|
sum += a["amount"]
}
event.set("amount-sum", sum)
end
I'm getting the following error:
Ruby exception occurred: undefined method `each_index' for Integer
I am a newbie in Ruby, so I've got no idea if this code serves for this type of field too.
If a is integer You can use something like this:
a = 123456789
a = a.to_s.split("").map(&:to_i)
sum = 0
a.each do |x|
sum += x
end
p sum #=> 45
And please DO NOT USE brace if block is not in one line. Here I did it with do end just to show how it must be. If You want to use one line block - You must write like that:
a.each {|x| sum += x}
if block > 1 lines than use do/end | else { }
I had a working logic to print pyramid and square dynamically by accepting the number of rows from terminal. I am facing error after including "module,classes and begin-end block".
module PatternPrinting
class Operation
def input
puts 'Enter the number of rows:'
rows = Integer(gets.chomp)
raise StandardError if rows <= 0 || rows > 10
pyramid(rows)
square(rows)
rescue StandardError
raise StandardError, 'Invalid Input, the entered value exceeds is not between 1-10 '
end
def pyramid(rows)
rows.times do |n|
print ' ' * (rows - n)
puts '*' * (2 * n + 1)
end
end
puts "Pyramid Rows: #{pyramid(rows)}"
def square(rows)
rows.times do |_n|
puts '*' * 10
end
end
puts "Sqaure Rows: #{square(rows)}"
end
end
begin
res = PatternPrinting::Operation.new
res.input
end
But I am facing error
pattern.rb:20:in `<class:Operation>': undefined local variable or method `rows' for PatternPrinting::Operation:Class (NameEr
ror)
from ./pattern.rb:3:in `<module:PatternPrinting>'
from ./pattern.rb:2:in `<main>'
rows is a local variable only available in the input method and nowhere else. Once that method completed, the local variables are lost.
If you want data to be available to all methods of a class object, you need to use instance variables.
Do
#rows = Integer.get_chomp
And then do
#rows.times do |n|
and
#rows.times do |_n|
You are missing a basic concept in ruby.
Read about ruby implicit and explicit receiver. https://www.reddit.com/r/ruby/comments/436d1m/what_is_the_difference_between_an_implicit_and/
Local variable has method/function scoping. So, at 20 rows is not visible.
Even though, let suppose your rows is accessible then also it should throw error coz, here implicit receiver self is your class PatternPrinting. PatternPrinting will try to invoke a method pyramid which is defined as class method https://github.com/rubocop-hq/ruby-style-guide#def-self-class-methods and your receiver PatternPrinting will not find the method and end up being calling method missing.
I highly recommend take a look https://rubymonk.com/.
I'm completely new to ruby and wanted to ask for some help with this ruby script.
it's supposed to take in a string and find out which character occurs the most frequently. It does this using a hash, it stores all the characters in a hash and then iterates through it to find the one with greatest value. As of right now it doesn't seem to be working properly and i'm not sure why. It reads the characters in properly as far as i can tell with print statements. Any help is appreciated.
Thanks!
puts "Enter the string you want to search "
input = gets.chomp
charHash = Hash.new
input.split("").each do |i|
if charHash.has_key?(i)
puts "incrementing"
charHash[i]+=1
else
puts"storing"
charHash.store(i, 1)
end
end
goc = ""
max = 0
charHash.each { |key,value| goc = key if value > max }
puts "The character #{goc} occurs the most frequently"
There are two major issues with you code:
As commented by Holger Just, you have to use += 1 instead of ++
charHash.store(:i, 1) stores the symbol :i, you want to store i
Fixing these results in a working code (I'm using snake_case here):
char_hash = Hash.new
input.split("").each do |i|
if char_hash.has_key?(i)
char_hash[i] += 1
else
char_hash.store(i, 1)
end
end
You can omit the condition by using 0 as your default hash value and you can replace split("").each with each_char:
char_hash = Hash.new(0)
input.each_char do |i|
char_hash[i] += 1
end
Finally, you can pass the hash into the loop using Enumerator#with_object:
char_hash = input.each_char.with_object(Hash.new(0)) { |i, h| h[i] += 1 }
I might be missing something but it seems that instead of
charHash.each { |key,value| goc = key if value > max }
you need something like
charHash.each do |key,value|
if value > max then
max = value
goc = key
end
end
Notice the max = value statement. In your current implementation (i.e. without updating the max variable), every character that appears in the text at least once satisfies the condition and you end up getting the last one.
I wrote a program to simulate the rolling of polyhedral dice but every time I run it I get the error shown below. It displays my prompts and lets me input my numbers, but after that it screws up. I've been trying to find a fix online but I only find people talking about different problems with the times method being undefined. I'm new to Ruby, so any help would be appreciated.
My program:
p = 0
while p < 1
puts "Choose your die type"
die_type = gets.chomp
puts "Choose your number of die"
die_number = gets.chomp
total = 0
i = 1
die_number.times do
random_int = 1 + rand(die_type)
total += random_int
i += 1
end
puts total
end
The error I get:
/dieroll.rb:13: undefined method 'times' for "5":String (NoMethodError)
Change die_number = gets.chomp to die_number = gets.to_i. die_number = gets.chomp, assigns a string to the local variable die_number( Because Kernel#gets gives us a string object). String don't have any method called #times, rather Fixnum class has that method.
Change also die_type = gets.chomp to die_type = gets.to_i, to avoid the next error waiting for you, once you will fix the first one.
1.respond_to?(:times) # => true
"1".respond_to?(:times) # => false
In you case die_number was "5", thus your attempt "5".times raised the error as undefined method 'times' for "5":String (NoMethodError).