How i can convert string into hash?
now i use:
eval "{'1627207:28320'=>'text'}"
=> {'1627207:28320'=>'text'}
but "eval" is not good for my case - string passed from params, and such case it is not secure
Edited:
passed string can also be:
"{'1627207'=>'text', '11:167:28320'=>'text 1 / text 2 / unicode=>привет!'}"
Then need result hash:
{'1627207:28320'=>'text',
'11:167:28320'=>'text 1 / text 2 / unicode=>привет!'}
str = "{'1627207:28320'=>'text'}"
p Hash[*str.delete("{}'").split('=>')] #{"1627207:28320"=>"text"}
edit for different input:
str = "{'1627207:28320'=>'text', 'key2'=>'text2'}"
p Hash[*str.delete("{}'").split(/=>|, /)] #{"1627207:28320"=>"text", "key2"=>"text2"}
class String
def to_h
h={}
self.scan(/'(\w+.\w+)'=>'(\w+)'/).each { |k,v| h[k]=v }
h
end
end
p "{'1627207:28320'=>'text','test'=>'text2'}".to_h
=>{"1627207:28320"=>"text", "test"=>"text2"}
EDIT: shorter version
class String
def to_h
Hash[self.scan(/'([^']+)'=>'([^']+)'/)]
end
end
Quite straight forward:
$ irb
irb(main):001:0> k='1627207:28320'
=> "1627207:28320"
irb(main):002:0> v='text'
=> "text"
irb(main):003:0> h={k => v}
=> {"1627207:28320"=>"text"}
irb(main):004:0> h
=> {"1627207:28320"=>"text"}
irb(main):005:0>
You could simply try this:
text_hash['1627207:28320'] = 'text'
text_hash
Related
I just want to delete a portion from a string.
My string: "&product=Software"
Need output: "Software"
Tried delete, split , slice but doesn't work. Can someone help me on this? I am very very new to Ruby.
It's slightly surprising, but Ruby lets you use [] and assignment to "overwrite" the substring you want to replace:
x = "&product=Software"
x['&product='] = ''
x # "Software"
str = "&product=Software"
str['&product='] = '' # method 1
str.sub!('&product=', '') # method 2
But if you wanna be smarter about it...
str = '&product=Software&price=19.99'
h = {}
str.split('&').each do |s|
next if s.length == 0
key, val = s.split '='
h[key] = val
end
puts h # {"product"=>"Software", "price"=>"19.99"}
Another two ways of achieving this:
Using split:
2.3.0 :014 > "&product=software".split('=')[1]
=> "software"
Using sub:
2.3.0 :015 > "&product=software".sub(/^.*?=/,'')
=> "software"
I have a string like this,
str = "uu#p, xx#m, yy#n, zz#m"
I want to know how to convert the given string into a hash. (i.e my actual requirement is, how many values (before the # symbol) have the m, n and p. I don't want the counting, I need an exact value). The output would be better like this,
{"m" => ["xx", "zz"], "n" => ["yy"], "p" => ["uu"]}
Can help me anyone, please?
Direct copy/past of an IRB session:
>> str.split(/, /).inject(Hash.new{|h,k|h[k]=[]}) do |h, s|
.. v,k = s.split(/#/)
.. h[k] << v
.. h
.. end
=> {"p"=>["uu"], "m"=>["xx", "zz"], "n"=>["yy"]}
Simpler code for a newbie :)
str = "uu#p, xx#m, yy#n, zz#m"
h = {}
str.split(",").each do |x|
v,k = x.split('#')
h[k] ||= []
h[k].push(v)
end
p h
FP style:
grouped = str
.split(", ")
.group_by { |s| s.split("#")[1] }
.transform_values { |ss| ss.map { |x| s.split("#")[0] } }
#=> {"m"=>["xx", "zz"], "n"=>["yy"], "p"=>["uu"]}
This is a pretty common pattern. Using Facets.map_by:
require 'facets'
str.split(", ").map_by { |s| s.split("#", 2).reverse }
#=> {"m"=>["xx", "zz"], "n"=>["yy"], "p"=>["uu"]}
I only want to capitalize the first char and leave the others as is.
For example:
"fooBar".titleize returns "Foo Bar". Should return FooBar.
"foo_Bar".capitalize returns "Foo_bar" Should return Foo_Bar.
Any way I can do this?
irb(main):001:0> s = "foo_Bar"
=> "foo_Bar"
irb(main):002:0> s[0] = s[0].upcase
=> "F"
irb(main):003:0> s
=> "Foo_Bar"
Or with regex for in-place substitution:
irb(main):001:0> s = "foo_Bar"
=> "foo_Bar"
irb(main):002:0> s.sub!(/^\w/) {|x| x.upcase}
=> "Foo_Bar"
class String
def fazzinize
first, *last = self.split("_")
[first.capitalize, *last].join("_")
end
end
"fooBar".fazzinize
#=> "Foobar"
"foo_Bar".fazzinize
#=> "Foo_Bar"
UPD
if it is a typo:
"fooBar".titleize returns "Foo Bar". Should return Foobar -> FooBar
then #Mchi is right
class String
def fazzinize
self[0] = self[0].upcase; self;
end
end
irb(main):001:0> s = "foo_Bar"
=> "foo_Bar"
irb(main):002:0> s1 = s.slice(0,1).capitalize + s.slice(1..-1)
=> "Foo_Bar"
Just substitute the first character with its uppercase version using a block.
"fooBar".sub(/^./) { |char| char.upcase }
Is there a built-in method in Ruby to support this?
if you are in Rails, you can convert 1 to 1st, 2 to 2nd, and so on, using ordinalize.
Example:
1.ordinalize # => "1st"
2.ordinalize # => "2nd"
3.ordinalize # => "3rd"
...
9.ordinalize # => "9th"
...
1000.ordinalize # => "1000th"
And if you want commas in large numbers:
number_with_delimiter(1000, :delimiter => ',') + 1000.ordinal # => "1,000th"
in ruby you do not have this method but you can add your own in Integer class like this.
class Integer
def ordinalize
case self%10
when 1
return "#{self}st"
when 2
return "#{self}nd"
when 3
return "#{self}rd"
else
return "#{self}th"
end
end
end
22.ordinalize #=> "22nd"
How about Linguistics? Its not built in though. If you want built in , you have to set it up using hashes etc..
See here also for examples
I wanted an ordinalize method that has "first, second, third" rather than '1st, 2nd, 3rd' - so here's a little snippet that works up to 10 (and falls back to the Rails ordinalize if it can't find it).
class TextOrdinalize
def initialize(value)
#value = value
end
def text_ordinalize
ordinalize_mapping[#value] || #value.ordinalize
end
private
def ordinalize_mapping
[nil, "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
"eighth", "ninth", "tenth" ]
end
end
Here's how it works:
TextOrdinalize.new(1).text_ordinalize #=> 'first'
TextOrdinalize.new(2).text_ordinalize #=> 'second'
TextOrdinalize.new(0).text_ordinalize #=> '0st'
TextOrdinalize.new(100).text_ordinalize #=> '100th'
if you are not in Rails you could do
def ordinalize(n)
return "#{n}th" if (11..13).include?(n % 100)
case n%10
when 1; "#{n}st"
when 2; "#{n}nd"
when 3; "#{n}rd"
else "#{n}th"
end
end
ordinalize 1
=> "1st"
ordinalize 2
=> "2nd"
ordinalize 11
=> "11th"
Using humanize gem, is probably the easiest way. But, yes, it is not built in, however it has only one dependency, so I think its a pretty good choice..
require 'humanize'
2.humanize => "two"
Why isnt that working:
>> s = "hi"
=> "hi"
>> s == ("hi"|"ho")
NoMethodError: undefined method `|' for "hi":String
from (irb):2
>>
I don't get it.. Is there a solution for this kind of syntax? Because
s == ("hi"|"ho")
#is shorther than
s == "hi" || s == "ho"
Yes, the bitwise operator | is not defined in the String class: http://ruby-doc.org/core/classes/String.html
Consider this for expressiveness:
["hi", "ho"].include? myStr
irb(main):001:0> s = "hi"
=> "hi"
irb(main):002:0> ["hi", "ho"]
=> ["hi", "ho"]
irb(main):003:0> ["hi", "ho"].include? s
=> true
irb(main):004:0> s = "foo"
=> "foo"
irb(main):005:0> ["hi", "ho"].include? s
=> false
In most high level languages that syntax will not work, you have to stick to the longer syntax of:
s == "hi" || s == "ho"
Note that | is a bitwise or, whereas || is a regular or
You could use the include? method on array if you've got several == tests to do:
["hi", "ho"].include?(s)
Not shorter for two checks admittedly but it will be shorter for three or more.
This syntax doesn't exist in any language as far as I know.
What you are saying
s == ("hi"|"ho")
Literally translates to 'bitwise OR the strings "hi" and "ho" together and then compare them with s'. If you can't see why this is not what you are looking for, try writing down the ASCII codes for "hi" and "ho" and then bitwise ORing them together. You are going to get complete gibberish.
You could make it work that way:
irb> class Pair
def initialize(strA,strB)
#strA,#strB = strA,strB
end
def ==(string)
string == #strA || string == #strB
end
def |(other)
Pair.new(self,other)
end
end
#=> nil
irb> class String
def |(other)
Pair.new(self,other)
end
alias old_equals :==
def ==(other)
if other.kind_of? Pair
other == self
else
old_equals other
end
end
end
#=> nil
irb> ("one"|"two") == "one"
#=> true
irb> ("one"|"two") == "two"
#=> true
irb> ("one"|"two") == "three"
#=> false
irb> "one" == ("one"|"two")
#=> true
irb> "three" == ("one"|"two"|"three")
#=> true
But since this involves some monkey-patching of a fairly lowlevel class, I wouldn't advise relying on it. Other people will hate reading your code.
Ruby supports binary 'or' and other binary operations on values of type Fixnum and Bignum, meaning any integer. Bitwise operations aren't supported on strings or any other type, as far as I know.
As other people have mentioned, you probably want something other than binary operations altogether. However, you can easily get integer representations of characters, so you can compare characters like so:
a = "Cake"
b = "Pie"
puts a[0] | b[0] # Prints "83" - C is 67 and P is 80.
You can get an array of the comparisons easily with some conversions.
a = "Cake"
b = "Pie " # Strings of uneven length is trivial but more cluttered.
a_arr = a.split(//)
b_arr = b.split(//)
c_arr = []
a.each_with_index { |char, i| c.push(a[i].to_i | b[i].to_i) }
# If you *really* want an ASCII string back...
c = c_arr.collect(&:chr).join
You could use a regex:
Like so:
regex = /hi|ho/
s = "hi"
t = "foo"
s =~ regex
#=> 0
t =~ regex
#=> nil