I am struggling to understand how I can access an array with a hash key. In my code, I create a hash with keys and values. Now, I want to set the values in a Car class. Whenever I try to instantiate the Car, the argument expects Integer and not a String.
I am getting the following error: TypeError (no implicit conversion of String into Integer)
Here is my code:
class Car_maker
attr_accessor :car_maker
def initialize(car_maker)
#car_maker = car_maker
end
end
class Car_model < Car_maker
attr_accessor :km, :type, :transmission, :stock, :drivetrain, :status,
:fuel, :car_maker, :model, :year, :trim, :features
#total number of instances & array with car objects
##totalCars = 0
##catalogue = []
def initialize(km, type, transmission, stock, drivetrain, status, fuel, car_maker, model, year, trim, features)
super(car_maker)
#km = km
#type = type
#transmission = transmission
#stock = stock
#drivetrain = drivetrain
#status = status
#fuel = fuel
#model = model
#year = year
#trim = trim
#features = features
##totalCars += 1
end
def self.convertListings2Catalogue(line)
#Initialise arrays and use them to compare
type = ["Sedan", "coupe", "hatchback", "station", "SUV"]
transmission = ["auto", "manual", "steptronic"]
drivetrain = ["FWD", "RWD", "AWD"]
status = ["new", "used"]
car_maker = ["honda", "toyota", "mercedes", "bmw", "lexus"]
hash = Hash.new
#In this part, we hash the set of features using regex
copyOfLine = line
regex = Regexp.new(/{(.*?)}/)
match_array = copyOfLine.scan(regex)
match_array.each do |line|
hash["features"] = line
end
#Now, we split every comma and start matching fields
newStr = line[0...line.index('{')] + line[line.index('}')+1...line.length]
arrayOfElements = newStr.split(',')
arrayOfElements.each do |value|
if value.include?("km") and !value.include?("/")
hash["km"] = value
elsif type.include?(value)
hash["type"] = value
elsif transmission.include?(value.downcase)
hash["transmission"] = value
elsif value.include?("/") and value.include?("km")
hash["fuel economy"] = value
elsif drivetrain.include?(value)
hash["drivetrain"] = value
elsif status.include?(value.downcase)
hash["status"] = value
elsif /(?=.*[a-zA-Z])(?=.*[0-9])/.match(value) and !value.include?("km")
hash["stock"] = value
elsif car_maker.include?(value.downcase)
hash["carmaker"] = value
elsif /^\d{4}$/.match(value)
hash["year"] = value
elsif value.length == 2
hash["trim"] = value
else
if value.length > 2
hash["model"] = value
end
end
end
end
end
textFile = File.open('cars.txt', 'r')
textFile.each_line{|line|
if line.length > 2
result = Car_model.convertListings2Catalogue(line)
puts "Hash: #{result}"
carObj = Car_model.new(result["km"], result["type"], result["transmission"], result["stock"], result["drivetrain"],
result["status"], result["fuel"], result["carmaker"], result["model"], result["year"], result["trim"], result["features"])
###catalogue.push (carObj)
end
}
This line
result = Car_model.convertListings2Catalogue(line)
Doesn't return the hash object. It returns arrayOfElements since that's what the each method actually returns and the each method is the last method executed in the method (although there are hash assignments within it, it's only the last value that's returned unless you use an explicit return statement.
Just use the variable hash in the last line of the convertListing2Catalog method
if value.length > 2
hash["model"] = value
end
end
end
hash # < this is the last line of the method so it's the value that will be returned
end
end
If you think about it, there were several variables created in the method. There's no reason to expect that the contents of any specific variable such as hash would be returned, and ruby methods by default return the last executed command.
Related
I have the following function:
def parse_var(var)
value = instance_variable_get(var)
puts(value)
puts(value.to_s)
value.is_a?(Numeric) ? value.to_s : "\"#{value}\""
end
Variables of certain form are converted into an array when they are interpolated. In the above function, when value equals (684) 029-6183 x01024, value.to_s comes out to be ["(684) 029-6183 x01024", nil]. The same thing also happens when I try "#{value}".
What is causing this?
Here's the context of the code in question:
Entity.rb (context of above code)
require 'securerandom'
# Entity.rb
class Entity
def initialize
generate_uuid
end
def to_cypher
first_char = self.class.name.chr.downcase
"MERGE (#{first_char}:#{self.class.name} {#{attrs_to_cypher.join(', ')}}) RETURN #{first_char};"
end
protected
def rand_bool
[true, false].sample
end
private
def attrs_to_cypher
self.instance_variables.map do |var|
"#{camelize(var.to_s[1..-1])}:#{parse_var(var)}"
end
end
def generate_uuid
#uuid = SecureRandom.uuid
end
def parse_var(var)
value = instance_variable_get(var)
puts(value)
puts(value.to_s)
value.is_a?(Numeric) ? value.to_s : "\"#{value}\""
end
def camelize(s)
(s == "uuid") ? "UUID" : s.downcase.split('_').map(&:capitalize).join
end
end
PhoneNumber.rb (where the value is coming from)
require 'faker'
require_relative 'Entity'
# PhoneNumber.rb
class PhoneNumber < Entity
def initialize(**opts)
super()
#type = opts[:type] || rand_bool ? "cell" : "home"
#number = opts[:number] || #type == "cell" ? Faker::PhoneNumber.cell_phone : Faker::PhoneNumber.phone_number,
#country_code = opts[:country_code] || nil
#area_code = opts[:area_code] || nil
end
end
The following line of code is causing a couple of issues
#number = opts[:number] || #type == "cell" ? Faker::PhoneNumber.cell_phone : Faker::PhoneNumber.phone_number,
First, the || operator has a higher precedence than the ? operator, so it actually looks like:
#number = (opts[:number] || #type == "cell") ? Faker::PhoneNumber.cell_phone : Faker::PhoneNumber.phone_number,
and you probably were wanting this:
#number = opts[:number] || (#type == "cell" ? Faker::PhoneNumber.cell_phone : Faker::PhoneNumber.phone_number),
As it stands, it doesn't matter what you pass into opts[:number], you'll always get a Faker::PhoneNumber assigned. (The line above, assigning type, looks like it would have this same precedence issue)
Second, you have a stray comma at the end of the line. This is turning the entire line into the first element of an array, and doesn't interfere with assigning the variable on the next line, so it's hard to catch:
opts = { number: '123' }
type = "cell"
number = opts[:number] || type == "cell" ? "truthy" : "falsey",
country = "some value"
p number # => ["truthy", "some value"]
p country # => "some value"
Your variable contains ["(684) 029-6183 x01024", nil], not a string. You rely on puts(value) output to determine it’s value which is plain wrong:
puts ["(684) 029-6183 x01024", nil]
#⇒ (684) 029-6183 x01024
#
puts(nil) returns an empty string, making you think the original value is a string.
To overcome this issue you might:
value = [*value].compact.join
I am trying to implement a trie in Ruby but can't figure out what the problem is with my print + collect methods.
I just implemented the same in JS and working fine. I guess the issue could be that Ruby is passed by reference (unlike JS) and how variable assignment works in Ruby.
So if I run the code with string.clone as argument when I recursively call the collect function then I get:
["peter", "peter", "petera", "pdanny", "pdjane", "pdjanck"]
and if I pass string then:
["peterradannyjaneck", "peterradannyjaneck", "peterradannyjaneck", "peterradannyjaneck", "peterradannyjaneck", "peterradannyjaneck"]
Any ideas how to fix this?
the code:
class Node
attr_accessor :hash, :end_node, :data
def initialize
#hash = {}
#end_node = false
#data = data
end
def end_node?
end_node
end
end
class Trie
def initialize
#root = Node.new
#words = []
end
def add(input, data, node = #root)
if input.empty?
node.data = data
node.end_node = true
elsif node.hash.keys.include?(input[0])
add(input[1..-1], data, node.hash[input[0]])
else
node.hash[input[0]] = Node.new
add(input[1..-1], data, node.hash[input[0]])
end
end
def print(node = #root)
collect(node, '')
#words
end
private
def collect(node, string)
if node.hash.size > 0
for letter in node.hash.keys
string = string.concat(letter)
collect(node.hash[letter], string.clone)
end
#words << string if node.end_node?
else
string.length > 0 ? #words << string : nil
end
end
end
trie = Trie.new
trie.add('peter', date: '1988-02-26')
trie.add('petra', date: '1977-02-12')
trie.add('danny', date: '1998-04-21')
trie.add('jane', date: '1985-05-08')
trie.add('jack', date: '1994-11-04')
trie.add('pete', date: '1977-12-18')
print trie.print
Ruby's string concat mutates the string and doesn't return a new string. You may want the + operator instead. So basically change the 2 lines inside collect's for-loop as per below:
stringn = string + letter
collect(node.hash[letter], stringn)
Also, you probably want to either always initialize #words to empty in print before calling collect, or make it a local variable in print and pass it to collect.
I am trying to write a function that takes a string and takes desired indices and scrambles the string:
def scramble_string(string, positions)
temp = string
for i in 0..(string.length-1)
temp[i] = string[positions[i]]
end
puts(string)
return temp
end
When I call the above method, the "string" is altered, which you will see in the output of puts.
Why does this happen since I didn't put string on the left-hand side of an equation I wouldn't expect it to be altered.
You need a string.dup:
def scramble_string(string, positions)
temp = string.dup
for i in 0..(string.length-1)
temp[i] = string[positions[i]]
end
puts(string)
return temp
end
For more understanding try the following snippet:
string = 'a'
temp = string
puts string.object_id
puts temp.object_id
The result of two identical object ids, in other words, is that both variables are the same object.
With:
string = 'a'
temp = string.dup
puts string.object_id
puts temp.object_id
puts string.object_id == temp.object_id #Test for same equal -> false
puts string.equal?( temp) #Test for same equal -> false
puts string == temp #test for same content -> true
you get two different objects, but with the same content.
In the code below, the initial get '/' contains a form, whose action is post '/'. when the user inputs a number, it should be converted to a variable that will be used to call the Game class, for which I have generated another action to reveal a new form at get '/game'. the variable generated in the post method is not being stored. how can I both store the variable created in post and then link into the get '/game' action?
require 'sinatra'
require 'sinatra/reloader'
##count = 5
Dict = File.open("enable.txt")
class Game
attr_accessor :letters, :number, :guess, :disp
##count = 5
def initialize (number)
letters = find(number)
end
def find (n)
words =[]
dictionary = File.read(Dict)
dictionary.scan(/\w+/).each {|word| words << word if word.length == n}
letters = words.sample.split("").to_a
letters
end
def counter
if letters.include?guess
correct = check_guess(guess, letters)
else
##count -= 1
end
end
end
get '/' do
erb :index
end
post '/' do
n = params['number'].to_i
#letters = Game.new(n)
redirect '/game'
end
get "/game" do
guess = params['guess']
letters = #letters
if guess != nil
correct = check_guess(guess, letters)
end
disp = display(letters, correct)
erb :game, :locals => {:letters => letters, :disp => disp}
end
def display(letters, correct)
line = "__"
d=[]
letters.each do |x|
if correct == nil
d << line
elsif correct.include?x
d << x
else
d << line
end
end
d.join(" ")
end
def check_guess(guess, letters)
correct = []
if guess != nil
if letters.include?guess
correct << guess
end
end
correct
end
You cannot do this:
#letters = Game.new(n)
each time you create a request, and new Request instance created and so the #letters attribute no longer exists.
It's the equivalent of
r = Request.new()
r.letters = Game.new()
r = Request.new()
r.letters # not defined anymore!!
You could achieve what you want using a class variable instead
##letters = Game.new(n)
Although this will become a nightmare when you have multiple users and will only work when you have a single ruby server process.
A more advanced approach would be to store params['number'] in a session cookie or in a database.
I have a class in Ruby where I pass in a Hash of commodity prices. They are in the form
{"date (string)" => price (float), etc, etc}
and in the initialise method I convert the dates to Dates like so:
#data = change_key_format(dates)
But I notice that that method seems to change the original argument as well. Why is that? Here is the code:
def initialize(commodity_name, data)
puts "creating ...#{commodity_name}"
#commodity_name = commodity_name
#data = change_hash_keys_to_dates(data)
#dates = array_of_hash_keys(data)
puts data ######## UNCHANGED
#data = fix_bloomberg_dates(#data, #dates)
puts data ######## CHANGED -------------------- WHY???
#get_price_data
end
def fix_bloomberg_dates(data, dates)
#Fixes the bad date from bloomberg
data.clone.each do |date, price|
#Looks for obvious wrong date
if date < Date.strptime("1900-01-01")
puts dates[1].class
date_gap = (dates[1] - dates[2]).to_i
last_date_day = dates[1].strftime("%a %d %b")
last_date_day = last_date_day.split(" ")
last_date_day = last_date_day[0].downcase
#Correct the data for either weekly or daily prices
#Provided there are no weekend prices
if date_gap == 7 && last_date_day == "fri"
new_date = dates[1] + 7
data[new_date] = data.delete(date)
elsif date_gap == 1 && last_date_day == "thu"
new_date = dates[1] + 4
data[new_date] = data.delete(date)
else
new_date = dates[1] + 1
data[new_date] = data.delete(date)
end
end
end
return data
end
def change_hash_keys_to_dates(hash)
hash.clone.each do |k,v|
date = Date.strptime(k, "%Y-%m-%d")
#Transforms the keys from strings to dates format
hash[date] = hash.delete(k)
end
return hash
end
def array_of_hash_keys(hash)
keys = hash.map do |date, price|
date
end
return keys
end
Because of these lines:
data[new_date] = data.delete(date)
You're modifying the original data object. If you don't want to do this, create a copy of the object:
data2 = data.clone
and then replace all other references to data with data2 in your method (including return data2).