ruby titleize gives space in between alphabet and numbers - ruby-on-rails

1.9.3p385 :010 > "Dell Inspiron 14R Laptop".titleize
=> "Dell Inspiron 14 R Laptop"
Did you see that?, A space is added between 14 and R which interferes with my search. Any solution you could think of?

1.9.3p385 :015 > "dell inspiron 14r laptop".titleize
=> "Dell Inspiron 14r Laptop"
1.9.3p385 :016 > "dell inspiron 14R laptop".titleize
=> "Dell Inspiron 14 R Laptop"
1.9.3p385 :017 > "dell inspiron 14R laptop".downcase.titleize
=> "Dell Inspiron 14r Laptop"
The alpha numeric whose alpha part is downcased are not treated as titles. So, doing a downcase and titleizing it saves your day.

Related

Check values in a hash

I am working on a legacy Rails project that uses Ruby 1.8 .
I have an hash of key-value where value is a float number.
For example, my_hash = ['foo'=>12.20, 'bar'=>10.0]. How can I check if my_hash contains all zero values? e.g. ['foo'=>0, 'bar'=>0, 'whatever'=>0] or ['foo'=>0.0, 'bar'=>0, 'whatever'=>0.0].
I know I can loop through & check element one by one, but I just wonder is there a more elegant way?
use below code if my_hash is:
[{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
my_hash.first.values.all?{|item| item.zero?}
or if my_hash is array, just:
my_hash.all?{|item| item.zero?}
Check all values in the hash are zero or not?
2.2.3 :006 > h={"foo"=>0.0, "bar"=>0, "whatever"=>0.0}
=> {"foo"=>0.0, "bar"=>0, "whatever"=>0.0}
2.2.3 :007 > h.values.all?{|a| a.zero?}
=> true
If it is an array of hash then
2.2.3 :001 > h = ['foo'=>0.0, 'bar'=>0, 'whatever'=>0.0]
=> [{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
2.2.3 :004 > h[0].values.all?{|a| a.zero? }
=> true
OR
2.2.3 :014 > h
=> [{"foo"=>0.0, "bar"=>0, "whatever"=>0.0}]
2.2.3 :015 > h.first.values.all?{|a| a.zero?}
=> true
Any element is zero
2.2.3 :009 > h={"foo"=>0.0, "bar"=>2, "whatever"=>1.1}
=> {"foo"=>0.0, "bar"=>2, "whatever"=>1.1}
2.2.3 :010 > h.values.any?{|a| a.zero?}
=> true
2.2.3 :011 > h={"foo"=>0.2, "bar"=>2, "whatever"=>1.1}
=> {"foo"=>0.2, "bar"=>2, "whatever"=>1.1}
2.2.3 :012 > h.values.any?{|a| a.zero?}
=> false

truncate text after exceeding certain size

Rails offers the truncate method when you want to truncate text that exceeds a certain character length. This is the example provided here:
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
It truncates a given text after a given :length if text is longer than :length
But I have the following example:
str = "abcdefhijklmno"
After 12 characters I want it truncated, so the above text should look like this:
abcdefhijklm...
But I tried using the truncate method and cannot get the desired result:
> str = "abcdefhijklmno"
=> "abcdefhijklmno"
> str.truncate(15)
=> "abcdefhijklmno"
> str.truncate(14)
=> "abcdefhijklmno"
> str.truncate(13)
=> "abcdefhijk..."
> str.truncate(12)
=> "abcdefhij..."
I would think that truncate(12) would do it but it truncates after 9 characters. What am I doing wrong?
argument of truncate means size of output string (with "..."):
str.truncate(13)
=> "abcdefhijk..."
str.truncate(13).size
=> 13
You can change default omission(...) by empty space if you want:
str.truncate(13, omission: '')
=> "abcdefhijklmn"
More about Rails String#truncate here.
I don't believe str.truncate will do what you want out of the box. But since it's really just an extra if statement, it's easy to write:
def trunc(str, length)
addition = str.length > length ? '...' : ''
"#{str.truncate(length, omission: '')}#{addition}"
end
Or slightly simplified, and without a Rails dependency (as mentioned by Ilya in the comments):
def trunc(str, len)
"#{str.first(len)}#{'...' if str.size > len}"
end
And the test cases:
2.2.1 :005 > s = 'abcdefghijklmno'
=> "abcdefghijklmno"
2.2.1 :006 > trunc(s, 20)
=> "abcdefghijklmno"
2.2.1 :007 > trunc(s, 15)
=> "abcdefghijklmno"
2.2.1 :008 > trunc(s, 14)
=> "abcdefghijklmn..."
2.2.1 :009 > trunc(s, 13)
=> "abcdefghijklm..."
2.2.1 :010 > trunc(s, 0)
=> "..."
2.2.1 :011 > trunc(s, 1)
=> "a..."

method match1.regex seems not working in ruby

I'm following a tutorial on regular expressions in ruby, but a method called regex that seems not working
Tutorial
re = /(\w*)\s(\w*),\s?([\w\s]*)/
match1 = str1.match re
match2 = str2.match re
match1.regex # => wsw,s[ws] (this is IRB's unique way of showing regular expressions; it will still work normally)
My console
The regex method the method throws an error
1.9.3-p547 :033 > re = /(\w*)\s(\w*),\s?([\w\s]*)/
=> /(\w*)\s(\w*),\s?([\w\s]*)/
1.9.3-p547 :034 > match1 = str1.match re
=> #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber">
1.9.3-p547 :035 > match2 = str2.match re
=> #<MatchData "Stephen Harper, Prime Minister" 1:"Stephen" 2:"Harper" 3:"Prime Minister">
1.9.3-p547 :036 > match1.regex
NoMethodError: undefined method `regex' for #<MatchData "Joe Schmo, Plumber" 1:"Joe" 2:"Schmo" 3:"Plumber">
from (irb):36
from /home/fernando/.rvm/rubies/ruby-1.9.3-p547/bin/irb:12:in `<main>'
1.9.3-p547 :037 >
I think it should be
match1.regexp
with a final 'p'

check if object has been updated mongoid 3.x

1.9.3p448 :014 > l.reload
=> #<Lesson _id: 527246641d41c81d14000006, title: "ola">
1.9.3p448 :015 > l.changed?
=> false
1.9.3p448 :016 > l.changes
=> {}
1.9.3p448 :017 > l.previous_changes
=> {"title"=>["olaaaaa", "ola"]}
1.9.3p448 :018 > l.changed?
=> false
1.9.3p448 :019 > l.update_attributes(title: "olaaa")
=> true
1.9.3p448 :020 > l.changes
=> {}
1.9.3p448 :021 > l.changed?
=> false
I have updated the attribute "title" but when I try l.changed? I get false.
I know the new_record? method, to know if a object is a new object but I need to know if a object is updated.
I would like to know, how can I know if a object has been updated with mongoid 3.x?
When you reload, save, update, the changes are moved to previous_changes. You also have access to the changes on callbacks, otherwise you will have to use previous_changes .
Thats by design, to be consistent with ActiveRecord.

Array length not getting correctly

in view.html.erb page I have code like:
<%
str="D:\\projects\\curator\\java\\hpc"
no=str.count("\\")
splitstr=str.split(pattern="\\",no+1)
%>
After this I printed
<%= splitstr.length %>
It gives me the output as 9. But the splitstr has only 5 elements.
How is it happening.
You end up having four of ""(empty) strings in your array
use this instead..
splitstr=str.split("\\")
splitstr.length
1.9.3p385 :007 > str="D:\\projects\\curator\\java\\hpc"
=> "D:\\projects\\curator\\java\\hpc"
1.9.3p385 :008 > no=str.count("\\")
=> 4
1.9.3p385 :009 > splitstr=str.split(pattern="\\",no+1)
=> ["D:", "projects", "curator", "java", "hpc"]
1.9.3p385 :010 > splitstr.length
=> 5
1.9.3p385 :011 >
Im getting it right!

Resources