I tried to write a wrapper for ets with which you can read and write structures in etc, question is: how to make id to be generated automatically
defmodule StructTable do
defstruct id: 0, data: nil
def create_table do
:ets.new(__MODULE__, [:orderedset, :named_table, {:keypos, 1}])
end
def insert_into_table(%__MODULE__{ id: id, data: data}) do
if hd(:ets.lookup(__MODULE__, id)) == false do
:ets.insert(__MODULE__, {id,data})
else IO.puts("already exists")
end
end
def select_data(iid) do
hd(:ets.lookup(__MODULE__, iid))
end
def select_all do
:ets.tab2list(__MODULE__)
end
end
I used :ets.last() to get the last key and added one to it; if it was an empty table, it gets an id of 1. Table type is ordered_set, which sorts keys.
defmodule StructTable do
defstruct id: 0, data: nil
def create_table do
:ets.new(__MODULE__, [:ordered_set, :public, :named_table])
end
def insert_into_table(%__MODULE__{data: data}) do
:ets.insert(__MODULE__, {generate_id(), data})
end
def select_all do
:ets.tab2list(__MODULE__)
end
def generate_id() do
if :ets.tab2list(__MODULE__) == [] do
1
else
(:ets.last(__MODULE__) ) + 1
end
end
def select_data(iid) do
hd(:ets.lookup(__MODULE__, iid))
end
end
Related
im new with ruby and i tried to put an array into initialize method but its not work like that, so how to put an array with this argument ? thanks
class User
attr_accessor :name, :friends
def initialize(name, friends)
#name = name
#friends = friends
end
def friendNbr
return friends.count
end
def isFriendWith(value)
friends.each do |user|
if (user.name == value)
return "Yes, #{name} is friend with #{user.name}"
end
end
return "No, #{name} is not friend with #{value}"
end
end
jane = User.new("Jane", [boris, francois, carlos, alice])
bob = User.new("Bob", [jane, boris, missy])
alice = User.new("Alice", [bob, jane])
# bob.isFriendWith("Jane")
# jane.isFriendWith("Alice")
# alice.isFriendWith("Carlos")
You have multiple ways of solving your problem :
First friends could be array of names (string)
class User
attr_accessor :name, :friends
def initialize(name, friends)
#name = name
#friends = friends
end
def friendNbr
return friends.count
end
def isFriendWith(value)
friends.each do |friend_name|
if (friend_name == value)
return "Yes, #{name} is friend with #{friend_name}"
end
end
return "No, #{name} is not friend with #{friend_name}"
end
end
jane = User.new("Jane", ["Boris", "Francois", "Carlos", "Alice"])
bob = User.new("Bob", ['Jane', 'Boris', 'Missy'])
alice = User.new("Alice", ['Bob', 'Jane'])
bob.isFriendWith("Jane")
jane.isFriendWith("Alice")
alice.isFriendWith("Carlos")
Other way is to pass object as you did but in this case you can only pass object already instanciated. In this second choice you can add a method addFriend and first create User and then add them friends.
class User
attr_accessor :name, :friends
def initialize(name, friends)
#name = name
#friends = friends
end
def friendNbr
return friends.count
end
def isFriendWith(value)
friends.each do |user|
if (user.name == value)
return "Yes, #{name} is friend with #{user.name}"
end
end
return "No, #{name} is not friend with #{value}"
end
def addFriend(...)
...
end
end
jane = User.new("Jane", [])
bob = User.new("Bob", [jane])
alice = User.new("Alice", [bob, jane])
bob.isFriendWith("Jane")
jane.isFriendWith("Alice")
alice.isFriendWith("Carlos")
I'm trying to improve the release_bike method.
I have gone into irb, and the first guard condition works, and the release_working_bikes work, but the second guard condition keeps on return nil in irb, even when I run a feature test and know that there is only a broken bike available.
Is there something wrong with the way I'm phrasing my second fail line, or is there a flaw in broken_bikes ?
The release_bike method should work as follows;
if there are no bikes in the docking station, then there should be a warning saying - No bikes available
if there are bikes in the docking station, but they are ALL broken, then there should be a warning saying - No working bikes available
if there are some working bikes, then release_bike should release one of the workign bikes.
Below are the two classes, that are involved;
require_relative 'bike'
class DockingStation
DEFAULT_CAPACITY = 20
attr_reader :capacity, :bikes
def initialize(capacity = DEFAULT_CAPACITY)
#bikes = []
#capacity = capacity
end
def release_bike
fail 'No bikes available' if empty?
fail 'No working bikes available' unless broken_bikes
release_working_bikes
end
def dock(bike)
fail 'Docking Station Full' if full?
#bikes << bike
end
private
def working_bikes
#bikes.each { |bike| return bike unless bike.broken? }
end
def broken_bikes
not_working = []
not_working << #bikes.each { |bike| return bike if bike.broken? }
not_working.empty?
end
def release_working_bikes
bike = working_bikes
#bikes.delete(bike)
end
def full?
#bikes.count >= #capacity
end
def empty?
#bikes.empty?
end
end
class Bike
attr_accessor :broken
def initialize
#broken = false
end
def working?
#working
end
def report_broken
#broken = true
end
def broken?
#broken
end
end
As already pointed out in comments, you're trying to check if all bikes are broken, so why not name your method all_bikes_broken? . See comments in code.
require_relative 'bike'
class DockingStation
DEFAULT_CAPACITY = 20
attr_reader :capacity, :bikes
def initialize(capacity = DEFAULT_CAPACITY)
#bikes = []
#capacity = capacity
end
def release_bike
fail 'No bikes available' if empty?
fail 'No working bikes available' unless all_bikes_broken?
release_working_bikes
end
def dock(bike)
fail 'Docking Station Full' if full?
#bikes << bike
end
private
def working_bikes
#this will select only bikes which are NOT broken
#bikes.reject{ |bike| bike.broken? }
end
def all_bikes_broken?
#this is shorthand for #bikes.all?{ |bike| bike.broken? }
#it says send :broken? method to each instance of bike.
#.all? returns true only if all instances return true, otherwise false.
#bikes.all?(&:broken?)
end
def release_working_bikes
bike = working_bikes
#bikes.delete(working_bikes.first)
#or you could do .last but order probably doesn't matter here.
end
def full?
#bikes.count >= #capacity
end
def empty?
#bikes.empty?
end
end
class Bike
attr_accessor :broken
def initialize
#broken = false
end
def working?
#working
end
def report_broken
#broken = true
end
def broken?
#broken
end
end
class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
#inventory = inventory
end
def inventory
#inventory = []
end
def buy(item)
if #gold >= item.price
#gold-=item.price
puts "You hand over #{item.price} gold, and get the #{item.name}."
puts "You have #{gold} gold left over."
#inventory.push([item.name,item.price,item.attack,item.defence])
puts "ITEMS IN INV: #{#inventory}" # output: ITEMS IN INV: [["Dagger", 4, 1, 0], ["Mucky Tunic", 2, 0, 2]]
else
puts "You can't afford this item."
end
end
end
player = Player.new(10,1,2,6)
puts player.inventory.inspect # output: []
The inventory.push line pushes the element to the array while it is inside the method, but when returned outside the method, the inventory array is empty. This is confusing because other variables that were changed inside that method in the same way come back as altered.
sample output when printed from inside the buy method:
ITEMS IN INV: [["Dagger", 4, 1, 0], ["Mucky Tunic", 2, 0, 2]]
output with player.inventory.inspect outside of the method:
[]
Whenever you call your inventory method:
def inventory
#inventory = []
end
... it assigns a new (empty) array to #inventory, thus overwriting any existing items.
The correct way is to either assign #inventory in initialize and just return it from within the getter:
class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
#inventory = []
end
def inventory
#inventory
end
# ...
end
or to not assign it at all in initialize and use the conditional assignment operator:
class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
end
def inventory
#inventory ||= []
end
# ...
end
which will assign [] to #inventory only if it was nil or false (i.e. the first time you call inventory).
A getter that just returns the corresponding instance variable (as in the former example) can also be created via attr_reader:
class Player
attr_reader :inventory
# ...
end
I figured it out 10 seconds after posting this, after messing about with it for an hour.
I needed to add inventory to def initialize, and then pass an empty array to player = Player.new(10,1,2,6) so it became player = Player.new(10,1,2,6,[]).
I still don't know why this works.
I start with an empty array, and a Hash of key, values.
I would like to iterate over the Hash and compare it against the empty array. If the value for each k,v pair doesn't already exist in the array, I would like to create an object with that value and then access an object method to append the key to an array inside the object.
This is my code
class Test
def initialize(name)
#name = name
#values = []
end
attr_accessor :name
def values=(value)
#values << value
end
def add(value)
#values.push(value)
end
end
l = []
n = {'server_1': 'cluster_x', 'server_2': 'cluster_y', 'server_3': 'cluster_z', 'server_4': 'cluster_x', 'server_5': 'cluster_y'}
n.each do |key, value|
l.any? do |a|
if a.name == value
a.add(key)
else
t = Test.new(value)
t.add(key)
l << t
end
end
end
p l
I would expect to see this:
[
#<Test:0x007ff8d10cd3a8 #name=:cluster_x, #values=["server_1, server_4"]>,
#<Test:0x007ff8d10cd2e0 #name=:cluster_y, #values=["server_2, server_5"]>,
#<Test:0x007ff8d10cd1f0 #name=:cluster_z, #values=["server_3"]>
]
Instead I just get an empty array.
I think that the condition if a.name == value is not being met and then the add method isn't being called.
#Cyzanfar gave me a clue as to what to look for, and I found the answer here
https://stackoverflow.com/a/34904864/5006720
n.each do |key, value|
found = l.detect {|e| e.name == value}
if found
found.add(key)
else
t = Test.new(value)
t.add(key)
l << t
end
end
#ARL you're almost there! The last thing you need to consider is when found actually returns an object since detect will find a matching one at some point.
n.each do |key, value|
found = l.detect {|e| e.name == value}
if found
found.add(key)
else
t = Test.new(value)
t.add(key)
l << t
end
end
You actually only want to add a new instance of Test when found return nil. This code should yield your desired output:
[
#<Test:0x007ff8d10cd3a8 #name=:cluster_x, #values=["server_1, server_4"]>,
#<Test:0x007ff8d10cd2e0 #name=:cluster_y, #values=["server_2, server_5"]>,
#<Test:0x007ff8d10cd1f0 #name=:cluster_z, #values=["server_3"]>
]
I observe two things in your code :
def values=(value)
#values << value
def add(value)
#values.push(value)
two methods do the same thing, pushing a value, as << is a kind of syntactic sugar meaning push
you have changed the meaning of values=, which is usually reserved for a setter method, equivalent to attire_writer :values.
Just to illustrate that there are many ways to do things in Ruby, I propose the following :
class Test
def initialize(name, value)
#name = name
#values = [value]
end
def add(value)
#values << value
end
end
h_cluster = {} # intermediate hash whose key is the cluster name
n = {'server_1': 'cluster_x', 'server_2': 'cluster_y', 'server_3': 'cluster_z',
'server_4': 'cluster_x', 'server_5': 'cluster_y'}
n.each do | server, cluster |
puts "server=#{server}, cluster=#{cluster}"
cluster_found = h_cluster[cluster] # does the key exist ? => nil or Test
# instance with servers list
puts "cluster_found=#{cluster_found.inspect}"
if cluster_found
then # add server to existing cluster
cluster_found.add(server)
else # create a new cluster
h_cluster[cluster] = Test.new(cluster, server)
end
end
p h_cluster.collect { | cluster, servers | servers }
Execution :
$ ruby -w t.rb
server=server_1, cluster=cluster_x
cluster_found=nil
server=server_2, cluster=cluster_y
cluster_found=nil
server=server_3, cluster=cluster_z
cluster_found=nil
server=server_4, cluster=cluster_x
cluster_found=#<Test:0x007fa7a619ae10 #name="cluster_x", #values=[:server_1]>
server=server_5, cluster=cluster_y
cluster_found=#<Test:0x007fa7a619ac58 #name="cluster_y", #values=[:server_2]>
[#<Test:0x007fa7a619ae10 #name="cluster_x", #values=[:server_1, :server_4]>,
#<Test:0x007fa7a619ac58 #name="cluster_y", #values=[:server_2, :server_5]>,
#<Test:0x007fa7a619aac8 #name="cluster_z", #values=[:server_3]>]
#members = {
approved: ["Jill"],
unapproved: ["Daniel"],
removed: ["John"],
banned: ["Daniel", "Jane"]
}
Very simply: making a program to track membership. In the above hash you can see the four membership status keys each with an array containing names.
I'm trying to create a find_member method which allows the user to enter a name and then searches each array for the name and tells the user which key the name was found in.
I'm not very good with hashes and in attempting to do this I've created a mess of loops and I imagine there's a very easy solution, I just haven't found it so far. Is there a really simple way to do this?
I've tried a few things and don't have all my past efforts still, but this is the latest mess I've ended up with, which is probably worse than what I had previously:
def find_member
puts "==Find Member=="
puts "Name: "
#name = gets.chomp
#members.each do |key|
key.values.each do |array|
array.each do |element|
if #name == element
puts "#{#name} found in #{key}"
else
puts "#{#name} not found in #{key}"
end
end
end
end
end
Thanks.
The most efficient way to do this is to create a one-to-many mapping of names to keys, and update that mapping only when #members changes.
def find_member(name)
update_names_to_keys_if_necessary
#member_to_keys[name]
end
def update_names_to_keys_if_necessary
new_hashcode = #members.hash
return if #old_members.hashcode == new_hashcode
#member_to_keys = #members.each_with_object(Hash.new { |h,k| h[k] = [] }) { |(k,v),h|
v.each { |name| h[name] << k } }
#old_members_hashcode = new_hashcode
end
Note that #old_members_hashcode evaluates to nil the first time update_names_to_keys_if_necessary is called, so #member_to_keys will be created at that time.
Initially we obtain
#member_to_keys
#=> {"Jill"=>[:approved], "Daniel"=>[:unapproved, :banned],
# "John"=>[:removed], "Jane"=>[:banned]}
Try it.
find_member("Jill")
#=> [:approved]
find_member("Daniel")
#=> [:unapproved, :banned]
find_member("John")
#=> [:removed]
find_member("Jane")
#=> [:banned]
find_member("Billy-Bob")
#=> []
You can use this itteration with include? method.
#members = {
approved: ["Jill"],
unapproved: ["Daniel"],
removed: ["John"],
banned: ["Daniel", "Jane"]
}
def find_member_group(name)
#members.each { |group, names| return group if names.include?(name) }
nil
end
#name = 'Jane'
group_name = find_member_group(#name)
puts group_name ? "#{#name} found in #{group_name}." : "#{#name} not found."
# => Jane found in banned.
Hash#select is the method to use here:
def find_member(name)
#members.select {|k,v| v.include? name }.keys
end
find_member("Jill") #=> [:approved]
find_member("Daniel") #=> [:unapproved, :banned]
find_member("John") #=> [:removed]
find_member("Jane") #=> [:banned]
Explanation:
select as the name suggests selects and maps only those elements that satisfy the condition in the corresponding code-block. The code-block negates the need for an if statement. Within the code-block we check each key-value pair and if its value includes the name argument, then that key-value pair is selected and mapped to the final output. Finally we're only interested in the memberships (namely the keys), so we apply the keys method to get these in the form of an array.