I wrote weird, little and simple DSL on ruby, and I stuck on output values of keys.
I need to output values of concrete key of hash by that command:
p config.key1
#> Output down below:
#> "value1"
# here's key1 is key of hash, i want to output value of key that i wrote in method call.
# p config.key_name for example
There's my configus realisation:
require "deep_merge/rails_compat"
class Configus
class InHash
attr_reader :inner_hash
def initialize
#inner_hash = {}
end
def method_missing(name, *args, &block)
if block_given?
context = InHash.new
context.instance_eval &block
result = context.inner_hash
else
result = args
end
#inner_hash[name] = result
end
end
def self.config(environment, parent = nil, &block)
in_hash = InHash.new
in_hash.instance_eval &block
keys = in_hash.inner_hash.keys
index = keys.find_index(environment)
if parent && environment
parent_hash = in_hash.inner_hash[parent]
adopted_hash = in_hash.inner_hash[environment]
merged_hash = parent_hash.deeper_merge!(adopted_hash, { :overwrite_arrays => "TRUE" })
elsif environment == keys[index]
"#{environment.capitalize} hash: " + in_hash.inner_hash[environment]
end
end
end
Here's my init.rb:
require "./configus"
require "pry"
config = Configus.config :staging, :production do
production do
key1 "value1"
key2 "value2"
group1 do
key3 "value3"
key4 "value4"
end
end
staging do
key2 "new value2"
group1 do
key4 "new value4"
end
end
development do
key1 "new value1"
key2 "value2"
group1 do
key3 "new value3"
key4 "value4"
end
end
productionisgsgsd do
key10 "value10"
end
end
puts config.key1
#> I wrote output down below:
Traceback (most recent call last):
init.rb:35:in `<main>': undefined method `key1' for #<Hash:0x000056261379cdb0> (NoMethodError)
Did you mean? key
key?
keys
I just want to output value of concrete key of hash with concrete command:
p config.key_name
But i don't know how to do that and because of my stupid problem i want to ask you guys.
P.S Sorry for my english skill and that weird realisation of dsl ¯\_(ツ)_/¯
The issue is that you return a normal hash from your Configus.config method. Instead you should return your special InHash instance which you're able to customize behaviour.
The current issue lies here:
def self.config(environment, parent = nil, &block)
in_hash = InHash.new
in_hash.instance_eval &block
keys = in_hash.inner_hash.keys
index = keys.find_index(environment)
if parent && environment
parent_hash = in_hash.inner_hash[parent]
adopted_hash = in_hash.inner_hash[environment]
merged_hash = parent_hash.deeper_merge!(adopted_hash, { :overwrite_arrays => "TRUE" })
# ^ the above line returns a normal hash from the method
in_hash # <= add this to return your in_hash object
elsif environment == keys[index]
"#{environment.capitalize} hash: " + in_hash.inner_hash[environment]
end
end
The problem is not entirely solved with the above, since this now returns your InHash instance, but your InHash has no way of fetching keys, you can only set them. So you want to add a way to fetch values:
def method_missing(name, *args, &block)
if block_given?
context = InHash.new
context.instance_eval &block
#inner_hash[name] = context # set to the nested InHash instance
elsif args.empty?
#inner_hash[name] # without arguments given read the value
else
#inner_hash[name] = args # set to the arguments provided
end
end
The following code:
config = Configus.config :staging, :production do
# ...
end
Should now set config to an InHash instance, that has your custom behaviour defined. The args.empty? check in method_missing will return a value if no further arguments are given. Meaning that:
config.key1
Should now return your ["value1"] value. #inner_hash[name] = args will always set the value to an array. If you don't want this you might want to change it to:
#inner_hash[name] = args.size > 1 ? args : args.first
Related
I have a Hash which needs to be converted in a String with escaped characters.
{name: "fakename"}
and should end up like this:
'name:\'fakename\'
I don't know how this type of string is called. Maybe there is an already existing method, which I simply don't know...
At the end I would do something like this:
name = {name: "fakename"}
metadata = {}
metadata['foo'] = 'bar'
"#{name} AND #{metadata}"
which ends up in that:
'name:\'fakename\' AND metadata[\'foo\']:\'bar\''
Context: This query a requirement to search Stripe API: https://stripe.com/docs/api/customers/search
If possible I would use Stripe's gem.
In case you can't use it, this piece of code extracted from the gem should help you encode the query parameters.
require 'cgi'
# Copied from here: https://github.com/stripe/stripe-ruby/blob/a06b1477e7c28f299222de454fa387e53bfd2c66/lib/stripe/util.rb
class Util
def self.flatten_params(params, parent_key = nil)
result = []
# do not sort the final output because arrays (and arrays of hashes
# especially) can be order sensitive, but do sort incoming parameters
params.each do |key, value|
calculated_key = parent_key ? "#{parent_key}[#{key}]" : key.to_s
if value.is_a?(Hash)
result += flatten_params(value, calculated_key)
elsif value.is_a?(Array)
result += flatten_params_array(value, calculated_key)
else
result << [calculated_key, value]
end
end
result
end
def self.flatten_params_array(value, calculated_key)
result = []
value.each_with_index do |elem, i|
if elem.is_a?(Hash)
result += flatten_params(elem, "#{calculated_key}[#{i}]")
elsif elem.is_a?(Array)
result += flatten_params_array(elem, calculated_key)
else
result << ["#{calculated_key}[#{i}]", elem]
end
end
result
end
def self.url_encode(key)
CGI.escape(key.to_s).
# Don't use strict form encoding by changing the square bracket control
# characters back to their literals. This is fine by the server, and
# makes these parameter strings easier to read.
gsub("%5B", "[").gsub("%5D", "]")
end
end
params = { name: 'fakename', metadata: { foo: 'bar' } }
Util.flatten_params(params).map { |k, v| "#{Util.url_encode(k)}=#{Util.url_encode(v)}" }.join("&")
I use it now with that string, which works... Quite straigt forward:
"email:\'#{email}\'"
email = "test#test.com"
key = "foo"
value = "bar"
["email:\'#{email}\'", "metadata[\'#{key}\']:\'#{value}\'"].join(" AND ")
=> "email:'test#test.com' AND metadata['foo']:'bar'"
which is accepted by Stripe API
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 making a Ruby REPL to be used inside an application. I made code:
a = 1
b = 2
currentScope = []
Kernel.local_variables.each do |var|
currentScope << [var,Kernel.eval(var.to_s)]
end
launchREPL(currentScope)
Inside the REPL, I can execute the following code:
#a #=>1
#a+#b #=>3
Ideally I wouldn't have to write the four lines of code before I launch the REPL, and instead I would like to run them inside the launchREPL function. However this would require access to the previous scope from inside the launchREPL function.
Test1
Most notably I tried:
launchREPL(Kernel)
When I do the following:
def launchREPL(scope)
F = 0
puts scope.local_variables # => [:F]
end
it is apparent that this method is not valid.
Test2
launchREPL(Kernel.binding)
def launchREPL(scope)
Kernel.binding.local_variables #= Error: private method 'local_variables' called for #<Binding>
end
Is there any way to do what I'm trying to do?
Edit: P.S. This is currently the code inside launchREPL:
def launchREPL(scope=nil,winName="Ruby REPL")
# ICM RB file Begin:
puts "\"Starting REPL...\""
__b = binding #Evaluating in a binding, keeps track of local variables
__s = ""
###############################################################################
# SEND INSTANCE VARIABLES TO REPL
###############################################################################
#
#How to prepare scope
# currentScope = []
# Kernel.local_variables.each do |var|
# currentScope << [var,Kernel.eval(var.to_s)]
# end
# launchREPL(currentScope)
if scope != nil
scope.each do |varDef|
__b.instance_variable_set "##{varDef[0].to_s}" , varDef[1]
__b.eval("##{varDef[0].to_s} = __b.instance_variable_get(:##{varDef[0].to_s})")
end
end
# to get instance variables: __b.instance_variable_get(__b.instance_variables[0])
# or better: __b.instance_variable_get(:#pipe1)
#
###############################################################################
bStartup = true
while bStartup || __s != ""
# If startup required skip evaluation step
if !bStartup
#Evaluate command
begin
__ret = __s + "\n>" + __b.eval(__s).to_s
rescue
__ret = __s + "\n> Error: " + $!.to_s
end
puts __ret
else
#REPL is already running
bStartup = false
end
#Read user input & print previous output
__s = WSApplication.input_box(__ret,winName,"")
__s == nil ? __s = "" : nil
end
end
Although what you are trying to achieve is unclear and there are definitely many ways to do it properly, every ruby method might be called with Object#send approach:
def launchREPL(scope)
scope.send :local_variables #⇒ here you go
end
a = 42
launchREPL(binding).include?(:a)
#⇒ true
Sidenote: this is how your “4 lines” are usually written in ruby:
local_variables.map { |var| [var, eval(var.to_s)] }
And this is how they should be written (note Binding#local_variable_get):
local_variables.map { |var| [var, binding.local_variable_get(var)] }
The summing up:
def launchREPL(scope)
vars = scope.send(:local_variables).map do |var|
[var, scope.local_variable_get(var)]
end
# some other code
end
a = 42
launchREPL(binding).to_h[:a]
#⇒ 42
This won’t fit the comment, so I would post it as an answer.
def launchREPL(scope = nil, winName = "Ruby REPL")
puts '"Starting REPL..."'
scope.eval('local_variables').each do |var|
instance_variable_set "##{var}", scope.eval(var.to_s)
end if scope
s = ""
loop do
ret = begin
"#{s}\n> #{eval(s)}"
rescue => e
"#{s}\n> Error: #{e.message}"
end
puts ret
# s = WSApplication.input_box(ret, winName, "")
# break if s.empty?
s = "100 * #a" # remove this line and uncomment 2 above
end
end
a = 42
launchREPL(binding)
This is how your function should be written (I have just make it looking as ruby code.) The above works (currently it has no break at all, but you can see as it’s calculating 4200 infinitely.)
I want to override the Hash class native brackets in ruby.
Note I don't want to override them in a class that inherits from Hash (no subclassing), I want to actually override Hash itself, such that any hash anywhere will always inherit my behavior.
Specifically (bonus points for..) - I want this in order to natively emulate a hash with indifferent access. In JavaScript I would modify the prototype, Ruby is known for its metaprogramming, so I hope this is possible.
So what I am aiming for is:
>> # what do I do here to overload Hash's []?...
>> x = {a:123} # x is a native Hash
>> x[:a] # == 123, as usual
>> x['a'] # == 123, hooray!
I've tried:
1)
class Hash
define_method(:[]) { |other| puts "Hi, "; puts other }
end
and
class Hash
def []
puts 'bar'
end
end
Both crash irb.
This seems to get the job done.
class Hash
def [](key)
value = (fetch key, nil) || (fetch key.to_s, nil) || (fetch key.to_sym, nil)
end
def []=(key,val)
if (key.is_a? String) || (key.is_a? Symbol) #clear if setting str/sym
self.delete key.to_sym
self.delete key.to_s
end
merge!({key => val})
end
end
And now:
user = {name: 'Joe', 'age' => 20} #literal hash with both symbols and strings as keys
user['name'] == 'Joe' # cool!
user[:age] == 20 # cool!
For more details see: http://www.sellarafaeli.com/blog/ruby_monkeypatching_friendly_hashes
class Hash
def [] key
value = fetch key rescue
case key
when Symbol then "#{value}, as usual"
when String then "#{value}, hooray!"
else value end
end
end
If using Rails HashWithIndifferentAccess supports this functionality already, even if using Ruby you can weigh including Active Support to have this functionality.
#hash is a global hash that looks something like:
#hash = {
"xmlns:xsi" =>"http://www.w3.org/2001/XMLSchema-instance",
"xsi:noNamespaceSchemaLocation"=>"merchandiser.xsd",
"header" =>[{"merchantId"=>["35701"],
"merchantName" =>["Lingerie.com"],
"createdOn" =>["2011-09-23/00:33:35"]}],
"trailer" =>[{"numberOfProducts"=>["0"]}]
}
And I would expect this to work if I call the method below like:
def amethod
hash_value("header", "merchantName") // returns "Lingerie.com"
end
def hash_value *attributes, hash = nil
hash = #hash unless hash
att = attributes.delete_at.first
attributes.empty? ? hash[att].first : hash_value(attributes, hash[att].first)
end
You can't have a default argument after a splat arg. Instead, require that the attribute list be passed as an array, like so:
def hash_value(attributes, hash = #hash)
return hash if attributes.empty?
hash_value(attributes[1..-1], hash[attributes.first].first)
end
p hash_value(["header", "merchantName"]) # => "Lingerie.com"
p hash_value(["trailer", "numberOfProducts"]) # => "0"
Try this:
def hash_value(*attributes, hash)
hash = #hash unless hash
att = attributes.delete_at.first
attributes.empty? ? hash[att].first : hash_value(attributes, hash[att].first)
end