i’m transalating a BeanShell prog using :
Map fieldsHash = new LinkedHashMap();
fieldsHash.put(“btl_desc”, “Description”);
fieldsHash.put(“btl_vintage”, “Millesime”);
fieldsHash.put(“btl_buy_date”, “Date achat”);
fieldsHash.put(“wcol_id”, “Coul.”);
fieldsHash.put(“btl_init_nbr”, “Qte A”);
fieldsHash.put(“btl_current_nbr”, “Qte R”);
i’ve seen the PseudoHash of Gotoken :
class PseudoHash < Hash
VERSION = “2000-12-25”
def order @order ||= []
end
def order=(ary)
order.replace ary
end
def with_order(ary)
self.order = ary
self
end
def order_of(key)
order.index(key)
end
alias each each
alias aset []=
def []=(k, *rest)
val, set = rest.reverse
order.push k if set and not order.include? k
aset(k,val)
end
def each
rest = keys - order
order.each {|k| yield(k, self[k]) if include?(k)}
rest.each {|k| yield(k, self[k]) }
end
alias each_pair each
end
i want to emulate that LinkedHashMap into ruby without calling the java
class (it’s possible because i’m using JRuby) how could i make
modifications, if any, Gotoken’s code for that ?
“Yvon Thoraval” yvon.thoravalNO-SPAM@free.fr schrieb im Newsbeitrag
news:1gbat4z.108b9591bhia0iN%yvon.thoravalNO-SPAM@free.fr…
i’m transalating a BeanShell prog using :
Map fieldsHash = new LinkedHashMap();
fieldsHash.put(“btl_desc”, “Description”);
fieldsHash.put(“btl_vintage”, “Millesime”);
fieldsHash.put(“btl_buy_date”, “Date achat”);
fieldsHash.put(“wcol_id”, “Coul.”);
fieldsHash.put(“btl_init_nbr”, “Qte A”);
fieldsHash.put(“btl_current_nbr”, “Qte R”);
i’ve seen the PseudoHash of Gotoken :
i want to emulate that LinkedHashMap into ruby without calling the java
class (it’s possible because i’m using JRuby) how could i make
modifications, if any, Gotoken’s code for that ?
Dunno that class but what is it that you want to achieve? Do you need a
sorted Hash? Then you can easily do
no ordered only as it is entered, in the mean time i’ve found a solution
:
OrderedHash.rb
class OrderedHash < Hash
VERSION = “2004-03-28”
def []=( key, value )
( @ordered_keys ||= [] ) << key unless has_key?( key )
super
end
def each
@ordered_keys.each do | key |
yield key, fetch( key )
end if @ordered_keys
end
def keys
a = Array.new
@ordered_keys.each do | key |
a << key
end if @ordered_keys
return a
end
def join_keys(s)
return self.keys.join(s)
end
def values
a = Array.new
@ordered_keys.each do | key |
a << fetch(key)
end if @ordered_keys
return a
end
def join_values(s)
return self.values.join(s)
end
“Yvon Thoraval” yvon.thoravalNO-SPAM@free.fr schrieb im Newsbeitrag
news:1gbf4b9.12juctd1qzju96N%yvon.thoravalNO-SPAM@free.fr…
Dunno that class but what is it that you want to achieve? Do you need
a
sorted Hash?
no ordered only as it is entered, in the mean time i’ve found a solution
Ah, I see. Alternatively you can use an LRU hash map like the one
attached. You can easily adapt this to only reflect insertion order and
not general access order.
I guess there are other implementations in the RAA.
Making a subclass of a standard class like Hash can be dangerous,
you should verify that it works for all methods, otherwise you may get
surprises:
h = OrderedHash.new
h[‘key1’] = 1
h[‘key2’] = 2
h.delete(‘key1’)
Now ‘key1’ is still in the ‘@ordered_keys’ array, and methods like
‘keys’ and ‘each’ will give you wrong results.
Regards
Karsten Meier
···
no ordered only as it is entered, in the mean time i’ve found a solution
:
OrderedHash.rb
class OrderedHash < Hash
VERSION = “2004-03-28”
def []=( key, value )
( @ordered_keys ||= [] ) << key unless has_key?( key )
super
end
def each
@ordered_keys.each do | key |
yield key, fetch( key )
end if @ordered_keys
end
def keys
a = Array.new
@ordered_keys.each do | key |
a << key
end if @ordered_keys
return a
end
def join_keys(s)
return self.keys.join(s)
end
def values
a = Array.new
@ordered_keys.each do | key |
a << fetch(key)
end if @ordered_keys
return a
end
def join_values(s)
return self.values.join(s)
end
Ah, I see. Alternatively you can use an LRU hash map like the one
attached. You can easily adapt this to only reflect insertion order and
not general access order.
I guess there are other implementations in the RAA.
h = OrderedHash.new
h[‘key1’] = 1
h[‘key2’] = 2
h.delete(‘key1’)
Now ‘key1’ is still in the ‘@ordered_keys’ array, and methods like
‘keys’ and ‘each’ will give you wrong results.