I'm working on an exercise where I have to create a roman to arabic number converter. As far as I can tell, the code below is totally kosher, but I keep getting an error when I run my tests. Ruby thinks there's an undefined method or variable on line 37 (noted by comment below).
I'm wondering if my snytax is off or if it's something else. Suggestions?
class ArabicNumeral
def replace_troublesome_roman_numerals(letters)
tough_mappings = {"CM" => "DCCCC", "CD" => "CCCC", "XC" => "LXXXX", "XL" => "XXXX", "IX"=> "VIIII", "IV" => "IIII"}
tough_mappings.each { |roman, arabic| letters = letters.gsub(roman, arabic) }
letters
end
def convert_and_add(letters)
digits = { "M" => 1000, "CM" => 900, "D" => 500, "C" => 100, "XC" => 90, "L" => 50, "XL" => 40, "X" => 10, "IX" => 9, "V" => 5, "IV" => 4, "I" => 1}
letters = letters.split("")
letters.inject(0) do |sum, letter|
arabic = digits[letter]
sum += arabic
end
end
def self.convert(letters)
roman_string = replace_troublesome_roman_numerals(letters) ###LINE 37!
arabic_number = convert_and_add(roman_string)
arabic_number
end
end
The problem here is the method you are calling on line 37. replace_troublesome_roman_numerals(letters). The problem is the method self.convert(letters) is a class method. You can call it like this:
ArabicNumeral.convert(letters)
However, it contains a call to an instance variable (being that replace_troublesome_roman_numerals(letters) I mentioned earlier.
def self.convert(letters)
roman_string = ArabicNumeral.new.replace_troublesome_roman_numerals(letters)
ArabicNumeral.new.convert_and_add(roman_string)
end
This creates an instance of ArabicNumeral and calls the method you need without saving it to a variable and taking up memory. I also removed the variable arabic_number from your method because you are calling convert_and_add(roman_string), adding it to the variable, and then returning the variable. since convert_and_add(roman_string) is the last thing handled by the method, it will return this anyway without the variable.
If you never plan on using those methods in an instance of ArabicNumeral then I would suggest making all the methods class level or wrapping them in a Module that you would include in your projects. If you don't plan on using them outside of the ArabicNumeral class at all, consider putting them behind a protected or private while leaving convert(letters) available.
class ArabicNumberal
def self.convert(letters)
# Code...
end
private
def self.replace_troublesome_roman_numerals(letters)
# Code...
end
def self.convert_and_add(roman_string)
# Code...
end
end
Ok... First of all, you're trying to use an instance method from a class one.
The problem could be solved by changing the method convert from:
def self.convert(letters)
roman_string = replace_troublesome_roman_numerals(letters) ###LINE 37!
arabic_number = convert_and_add(roman_string)
arabic_number
end
To:
def convert(letters)
roman_string = replace_troublesome_roman_numerals(letters) ###LINE 37!
arabic_number = convert_and_add(roman_string)
arabic_number
end
Then you'll need to create an instance and call convert method:
x = ArabicalNumeral.new()
x.convert('param')
And that's it.
By the way, I suggest you to add a constructor method (in Ruby is named initialize).
The complete script below:
class ArabicNumeral
def replace_troublesome_roman_numerals(letters)
tough_mappings = {"CM" => "DCCCC", "CD" => "CCCC", "XC" => "LXXXX", "XL" => "XXXX", "IX"=> "VIIII", "IV" => "IIII"}
tough_mappings.each { |roman, arabic| letters = letters.gsub(roman, arabic) }
letters
end
def convert_and_add(letters)
digits = { "M" => 1000, "CM" => 900, "D" => 500, "C" => 100, "XC" => 90, "L" => 50, "XL" => 40, "X" => 10, "IX" => 9, "V" => 5, "IV" => 4, "I" => 1}
letters = letters.split("")
letters.inject(0) do |sum, letter|
arabic = digits[letter]
sum += arabic
end
end
def convert(letters)
roman_string = replace_troublesome_roman_numerals(letters) ###LINE 37!
arabic_number = convert_and_add(roman_string)
arabic_number
end
end
x = ArabicNumeral.new()
puts x.convert('MDC')
Related
I'm trying to make a calorie counter for the below hash menu. in this example I've passed 3 arguments - what would the function need to look like it the number of parameters/arguments is unknown?
#menu = {
"hamburger" => 250,
"Cheese burger" => 350,
"cola" => 35,
"salad" => 120,
"dessert" => 350
}
def order(a, b, c)
return #menu[a] + #menu[b] + #menu[c]
end
puts order("hamburger", "Cheese burger", "cola")
tried
def order(**a)
total = 0
total += #menu[**a]
end
i know (*a) works for arrays.
I'd like to be able to get results for
puts order("hamburger")
and equally for
puts order("Cheese burger", "salad"), for example
In Ruby, it is often possible to write the code exactly the same way you would describe the solution in English. In this case, you need to get the values at specific keys of the hash and then compute the sum of the values.
You can use the Hash#values_at method to get the values at the specific keys and you can use the Array#sum method to compute the sum of the values:
def order(*items)
#menu.values_at(*items).sum
end
Note that it is strange to use an instance variable of the top-level main object. It would make much more sense to use a constant:
MENU = {
'hamburger' => 250,
'Cheese burger' => 350,
'cola' => 35,
'salad' => 120,
'dessert' => 350,
}
def order(*items)
MENU.values_at(*items).sum
end
It would also make sense to freeze the hash:
MENU = {
'hamburger' => 250,
'Cheese burger' => 350,
'cola' => 35,
'salad' => 120,
'dessert' => 350,
}.freeze
And last but not least, I find the name of the order method somewhat misleading. It is also ambiguous: is order meant to be a noun and this is meant to be a getter method that retrieves an order? Or is it meant to be a verb and it is meant to be a command method which tells the object to execute an order?
Either way, it does not seem that the method is doing either of those two things, rather it seems to compute a total. So, the name should probably reflect that.
I would do:
MENU = {
"hamburger" => 250,
"Cheese burger" => 350,
"cola" => 35,
"salad" => 120,
"dessert" => 350
}
def order(*args)
MENU.values_at(*args).sum
end
order("hamburger", "Cheese burger", "cola")
#=> 635
Read about the Ruby Splat Operator, Hash#values_at and Array#sum.
When you really want to use each (what I would not recommend), like mentioned in the comment, then you can implement it like this:
def order(*args)
total = 0
args.each { |name| total += MENU[name] }
total
end
or
def order(*args)
total = 0
MENU.values_at(*args).each { |value| total += value }
total
end
I have a nested hash and I need to return the inside hash, which is a value of the key. The problem is one of the values is a string and it needs to be returned as an integer.
def player_stats(player_name)
game_hash.keys.each do |data|
if game_hash[data][:players].keys.include?(player_name)
return game_hash[data][:players][player_name]
end
end
end
game_hash = {
:home => {team_name:"Brooklyn Nets", colors:["Black", "White"],
:players => {"Alan Anderson" => {:number => 0, :shoe => "16",
:points => 22, :rebounds => 12, :assists =>12, :steals => 3,
:blocks => 1, :slam_dunks => 1},
The code is correct, the only thing is that I need to input a line that will convert the string**(:shoe)** to an integer.
game_hash = {
:home => {:team_name =>"Brooklyn Nets",
:colors => ["Black", "White"],
:players => {
"Alan Anderson" => {
:number => 0,
:shoe => "16",
:points => 22,
}
}
}
}
In this case, assuming you do not want to mutate (modify) the original hash, it's easiest to first make a deep copy of game_hash:
h = Marshal.load(Marshal.dump(game_hash))
#=> {:home=>{:team_name=>"Brooklyn Nets", :colors=>["Black", "White"],
# :players=>{"Alan Anderson"=>{:number=>0, :shoe=>"16", :points=>22}}}}
See Marshall::load and Marshall::dump.
Then just modify the value of interest:
h[:home][:players]["Alan Anderson"][:shoe] =
h[:home][:players]["Alan Anderson"][:shoe].to_i
h #=> {:home=>{:team_name=>"Brooklyn Nets", :colors=>["Black", "White"],
# :players=>{"Alan Anderson"=>{:number=>0, :shoe=>16, :points=>22}}}}
Lastly, let's confirm the original hash was not mutated:
game_hash
#=> {:home=>{:team_name=>"Brooklyn Nets", :colors=>["Black", "White"],
# :players=>{"Alan Anderson"=>{:number=>0, :shoe=>"16", :points=>22}}}}
Following your code, just a couple of changes (see inline comments in the option below).
Given the game_hash, which could have integers as string somewhere:
game_hash = {:home => { team_name:"Brooklyn Nets", colors:["Black", "White"],:players => {"Alan Anderson" => {:number => "0", :shoe => "16", :points => 22, :rebounds => 12, :assists =>12, :steals => 3, :blocks => 1, :slam_dunks => 1},"Reggie Evans" => {:number => "30",:shoe => "14",:points => 12, :rebounds => 12, :assists => 12, :steals => 12, :blocks => 12, :slam_dunks => 7}}}}
This is the option:
def player_stats(player_name, game_hash) # <-- pass game_hash as parameter
game_hash.keys.each do |data|
if game_hash[data][:players].keys.include?(player_name)
return game_hash[data][:players][player_name].transform_values { |v| v.to_i } # <-- transform values to integer
end
end
end
Then, you can call:
player_stats("Reggie Evans", game_hash)
#=> {:number=>30, :shoe=>14, :points=>12, :rebounds=>12, :assists=>12, :steals=>12, :blocks=>12, :slam_dunks=>7}
There are a few "issues" that I would like to point out in your #player_stats method, but I will focus on your problem with converting the player hash values to integers. To start off, I am making the following assumptions:
There is no consistency in whether the values are formatted as a string or integer.
All values are of integer form, in either the Integer type, or String type.
One thing I would like to point out is that shoe size could be 9.5 or 10.5, etc. This could be the reason why the shoe value is formatted as a string. If this is the case, then you would need to take this into account and convert to a float using #to_f instead of #to_i. But since your question is asking for it to be an integer, I will use #to_i in my examples below.
Given my assumptions, you would simply loop over all the values in the player hash and call #to_i on the value to convert it to an integer. This can be done using the following method:
def convert_hash_values_to_int(hash)
hash.each do |key, value|
hash[key] = value.to_i
end
end
You can call this method inside your #player_stats method. Note that this method is mutating the original hash values in place without creating a new hash. If this is not desirable, then you should use #inject:
def convert_hash_values_to_int(hash)
hash.inject({}) do |result, (key, value)|
result[key] = value.to_i
result
end
end
This creates a new hash with the transformed values, and returns it.
This can be simplified even further using #each_with_object, which also creates a new hash instead of mutating the original hash:
def convert_hash_values_to_int(hash)
hash.each_with_object({}) do |(key, value), result|
result[key] = value.to_i
end
end
This approach does not require that you return result in each loop. Note that the arguments are switched for #each_with_object - |(key, value), result| instead of |result, (key, value)|.
There is one more approach that you could use which is the most succinct, but it is only available in Ruby 2.4+:
def convert_hash_values_to_int(hash)
hash.transform_values do |value|
value.to_i
end
end
Which can be even more succinct:
def convert_hash_values_to_int(hash)
hash.transform_values(&:to_i)
end
#transform_values does not mutate the original hash. If you would like to mutate the original hash, you would need to use a bang:
def convert_hash_values_to_int(hash)
hash.transform_values!(&:to_i)
end
I want:
{
"CATTLE" => {"Heifers" => 647, "Cows" => 633, "Weaners" => 662, "Steers" => 653},
"BULL" => {"Bulls" => 196},
"SHEEP" => {"Rams" => 410, "Ewes" => 1629, "Wethers" => 1579, "Calves" => 1241, "Weaners" => 300}
}
To get that, I start with an empty mobs = {} hash, and then populate it as I loop. If the key is nil, I set it and then populate it. I was wondering if there was a nicer way to do as below:
mob_livestock_group_response.each do |livestock_group|
mobs[livestock_group['assetType']] = {} unless mobs[livestock_group['assetType']]
mobs[livestock_group['assetType']][livestock_group['subtype']] = 0 unless mobs[livestock_group['assetType']][livestock_group['subtype']]
mobs[livestock_group['assetType']][livestock_group['subtype']] += livestock_group['size']
end
You could write:
mob_livestock_group_response.each do |livestock_group|
mobs[livestock_group['assetType']] ||= {}
mobs[livestock_group['assetType']][livestock_group['subtype']] ||= 0
mobs[livestock_group['assetType']][livestock_group['subtype']] += livestock_group['size']
end
Furthermore I would write this like this:
mob_livestock_group_response.each do |livestock_group|
type = livestock_group['assetType']
sub = livestock_group['subtype']
size = livestock_group['size']
mobs[type] ||= {}
mobs[type][sub] ||= 0
mobs[type][sub] += size
end
I have a helper module to generate an array hash data, which is something like:
[{:date => d, :total_amount => 31, :first_category => 1, :second_category => 2,...},
{:date => d+1, :total_amount => 31, :first_category => 1, :second_category => 2,...}]
So I make the method like:
def records_chart_data(category = nil, start = 3.weeks.ago)
total_by_day = Record.total_grouped_by_day(start)
category_sum_by_day = Record.sum_of_category_by_day(start)
(start.to_date..Time.zone.today).map do |date|
{
:date => date,
:total_amount => total_by_day[date].try(:first).try(:total_amount) || 0,
Category.find(1).title => category_sum_by_day[0][date].try(:first).try(:total_amount) || 0,
Category.find(2).title => category_sum_by_day[1][date].try(:first).try(:total_amount) || 0,
Category.find(3).title => category_sum_by_day[2][date].try(:first).try(:total_amount) || 0,
}
end
end
Since the Category will always change, I try to use loop in this method like:
def records_chart_data(category = nil, start = 3.weeks.ago)
total_by_day = Record.total_grouped_by_day(start)
category_sum_by_day = Record.sum_of_category_by_day(start)
(start.to_date..Time.zone.today).map do |date|
{
:date => date,
Category.all.each_with_index do |category, index|
category.title => category_sum_by_day[index][date].try(:first).try(:total_amount) || 0,
end
:total_amount => total_by_day[date].try(:first).try(:total_amount) || 0
}
end
end
But ruby alerts me with an error:
/Users/tsu/Code/CashNotes/app/helpers/records_helper.rb:10: syntax error, unexpected tASSOC, expecting keyword_end
category.title => category_sum_by_day[index][d...
Why does it say expecting keyword_end, and how should I fix it?
The method category_sum_by_day it calls looks like:
def self.sum_of_category_by_day(start)
records = where(date: start.beginning_of_day..Time.zone.today)
records = records.group('category_id, date(date)')
records = records.select('category_id, date, sum(amount) as total_amount')
records = records.group_by{ |r| r.category_id }
records.map do |category_id, value|
value.group_by {|r| r.date.to_date}
end
end
Or should I alter this method to generate a more friendly method for the helper above?
Category.all.each_with_index do |category, index|
category.title => category_sum_by_day # ...snip!
end
Unfortunately, this piece of code does not adhere to Ruby's grammar. The problem is the body of the block. x => y is not an expression and the syntax requires bodies of blocks to be expressions.
If you want to generate a hash by one key-value pair at a time try the following combination of Hash::[], Array#flatten and the splat operator (i.e. unary *):
Hash[*5.times.map { |i| [i * 3, - i * i] }.flatten]
As a result I'd rewrite the last expresion of records_chart_data more or less as follows
(start.to_date..Time.zone.today).map do |date|
categories = Hash[*Category.all.each_with_index do |category, index|
[ category.title, category_sum_by_day[...] ]
end .flatten]
{ :date => date,
:total_amount => total_by_day[date].try(:first).try(:total_amount) || 0
}.merge categories
end
If you consider it unreadable you can do it in a less sophisticated way, i.e.:
(start.to_date..Time.zone.today).map do |date|
hash = {
:date => date,
:total_amount => total_by_day[date].try(:first).try(:total_amount) || 0
}
Category.all.each_with_index do |category, index|
hash[category.title] = category_sum_by_day[...]
end
hash
end
Another idea is to use Array#reduce and adopt a more functional approach.
(start.to_date..Time.zone.today).map do |date|
Category.all.each_with_index.reduce({
:date => date,
:total_amount => total_by_day[date].try(:first).try(:total_amount) || 0
}) do |hash, (category, index)|
hash.merge category.title => category_sum_by_day[...]
end
hash
end
Given the following code,
How would you refactor this so that the method search_word has access to issueid?
I would say that changing the function search_word so it accepts 3 arguments or making issueid an instance variable (#issueid) could be considered as an example of bad practices, but honestly I cannot find any other solution. If there's no solution aside from this, would you mind explaining the reason why there's no other solution?
Please bear in mind that it is a Ruby on Rails model.
def search_type_of_relation_in_text(issueid, type_of_causality)
relation_ocurrences = Array.new
keywords_list = {
:C => ['cause', 'causes'],
:I => ['prevent', 'inhibitors'],
:P => ['type','supersets'],
:E => ['effect', 'effects'],
:R => ['reduce', 'inhibited'],
:S => ['example', 'subsets']
}[type_of_causality.to_sym]
for keyword in keywords_list
relation_ocurrences + search_word(keyword, relation_type)
end
return relation_ocurrences
end
def search_word(keyword, relation_type)
relation_ocurrences = Array.new
#buffer.search('//p[text()*= "'+keyword+'"]/a').each { |relation|
relation_suggestion_url = 'http://en.wikipedia.org'+relation.attributes['href']
relation_suggestion_title = URI.unescape(relation.attributes['href'].gsub("_" , " ").gsub(/[\w\W]*\/wiki\//, ""))
if not #current_suggested[relation_type].include?(relation_suggestion_url)
if #accepted[relation_type].include?(relation_suggestion_url)
relation_ocurrences << {:title => relation_suggestion_title, :wiki_url => relation_suggestion_url, :causality => type_of_causality, :status => "A", :issue_id => issueid}
else
relation_ocurrences << {:title => relation_suggestion_title, :wiki_url => relation_suggestion_url, :causality => type_of_causality, :status => "N", :issue_id => issueid}
end
end
}
end
If you need additional context, pass it through as an additional argument. That's how it's supposed to work.
Setting #-type instance variables to pass context is bad form as you've identified.
There's a number of Ruby conventions you seem to be unaware of:
Instead of Array.new just use [ ], and instead of Hash.new use { }.
Use a case statement or a constant instead of defining a Hash and then retrieving only one of the elements, discarding the remainder.
Avoid using return unless strictly necessary, as the last operation is always returned by default.
Use array.each do |item| instead of for item in array
Use do ... end instead of { ... } for multi-line blocks, where the curly brace version is generally reserved for one-liners. Avoids confusion with hash declarations.
Try and avoid duplicating large chunks of code when the differences are minor. For instance, declare a temporary variable, conditionally manipulate it, then store it instead of defining multiple independent variables.
With that in mind, here's a reworking of it:
KEYWORDS = {
:C => ['cause', 'causes'],
:I => ['prevent', 'inhibitors'],
:P => ['type','supersets'],
:E => ['effect', 'effects'],
:R => ['reduce', 'inhibited'],
:S => ['example', 'subsets']
}
def search_type_of_relation_in_text(issue_id, type_of_causality)
KEYWORDS[type_of_causality.to_sym].collect do |keyword|
search_word(keyword, relation_type, issue_id)
end
end
def search_word(keyword, relation_type, issue_id)
relation_occurrences = [ ]
#buffer.search(%Q{//p[text()*= "#{keyword}'"]/a}).each do |relation|
relation_suggestion_url = "http://en.wikipedia.org#{relation.attributes['href']}"
relation_suggestion_title = URI.unescape(relation.attributes['href'].gsub("_" , " ").gsub(/[\w\W]*\/wiki\//, ""))
if (!#current_suggested[relation_type].include?(relation_suggestion_url))
occurrence = {
:title => relation_suggestion_title,
:wiki_url => relation_suggestion_url,
:causality => type_of_causality,
:issue_id => issue_id
}
occurrence[:status] =
if (#accepted[relation_type].include?(relation_suggestion_url))
'A'
else
'N'
end
relation_ocurrences << occurrence
end
end
relation_occurrences
end