How to Override [] Brackets in Ruby

how to override [] brackets in ruby?

You need to use params[:Jobs][:clearance]

params is a hash of all the request parameters. But params[:Jobs] is ALSO a hash of all :Jobs parameters. So calling params[:Jobs][:clearance] is calling the [] method on the params[:Jobs] object passing :clearance in as a parameter.

How can I override Hash native brackets ([] access)

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

Accessing variables using overloading brackets [] in Ruby

Variables and messages live in a different namespace. In order to send the variable as a message, you'd need to define it as either:

def [](iv)
send iv
end

(if you want to get it through an accessor)

or

def [](iv)
instance_variable_get "@#{iv}"
end

(if you want to access the ivar directly)

How can I override the []= method when subclassing a Ruby hash?

The method signature is def []=(key, val), and super to call the parent method. Here's a full example:

class MyHash < Hash
def []=(key,val)
printf("key: %s, val: %s\n", key, val)
super(key,val)
end
end

x = MyHash.new

x['a'] = 'hello'
x['b'] = 'world'

p x

Get value between brackets

> item.delete('[|]')
#=> "a,b,c,d"

To override self value you can use delete!

Note: It only delete brackets from your string, if your value can be anything and you want to fetch only string within brackets then use:

> item = "hi [a,b,c,d] world"
> item[/(?<=\[).*(?=\])/]
# OR
> item[/\[(.*)\]/, 1]
#=> "a,b,c,d"

Are there methods for enabling Hash-style look-ups in custom classes?

you can always define your own [] and []= methods (google: "operator overloading")
e.g.:

class MyClass
def [](key)
# ...
end

def []=(key, value)
# ...
end
end

Here are some examples:

http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-operators-and-their-methods/

https://www.ruby-forum.com/topic/4067570

how to override [] brackets in ruby?

Ruby function for putting string in parentheses

I would use string interpolation:

str = " ( #{str} ) "

Some other options might be:

str = ' ( ' +  str + ' ) '
str = [' ( ', str, ' ) '].join

Ruby: How to escape url with square brackets [ and ]?

encode doesn't escape brackets because they aren't special -- they have no special meaning in the path part of a URI, so they don't actually need escaping.

If you want to escape chars other than just the "unsafe" ones, pass a second arg to the encode method. That arg should be a regex matching, or a string containing, every char you want encoded (including chars the function would otherwise already match!).



Related Topics



Leave a reply



Submit