Array length not getting correctly - ruby-on-rails

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!

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

simple_format changes the text itself

In Rails 3.0, the helper method simple_format changes the parameter itself.
I expected that it only returns the wrapped text.
2.0.0-p648 :001 > Rails.version
=> "3.0.20"
2.0.0-p648 :002 > s = "Hello"
=> "Hello"
2.0.0-p648 :003 > helper.simple_format(s)
=> "<p>Hello</p>"
2.0.0-p648 :004 > s
=> "<p>Hello</p>"
I checked with Rails 4.2 and it doesn't change the text.
Can someone please explain it?
Sam
The difference between implementations of this method in Rails 4.2 and Rails 3.0 is that in Rails 3.0 the passed string is modified (mutated by gsub!) and in Rails 4.2 it's not (it just returns a new modified string):
Rails 4.2:
2.4.0 :006 > s = "hello"
=> "hello"
2.4.0 :007 > simple_format s
=> "<p>hello</p>"
2.4.0 :008 > s
=> "hello"
The source code of different implementations can be found in the documentation

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.

Mongoid - how to get second record?

I can do
1.9.3p125 :122 > User.first
=> #<StandardUser _id: 4f849..
and
1.9.3p125 :124 > User.last
and
1.9.3p125 :125 > User.count
=> 5
but I can't find any way to get at the other records (2 thru 4).
User.skip(1).first returns you second document.

rails 3 json encode for consumption in javascript

I know that to_json is deprecated and as_json is giving me problems.
This line works fine, but to_json is deprecated:
new IS.Presentation(<%= raw(#course_step.step.step_presentation.step_presentation_files.map { |item| {'url' => item.slide.url, 'title' => item.title}}.to_json) %>)
Any ideas?
ActiveSupport supports JSON. You can see here:
ruby-1.9.2-p136 :003 > j = ActiveSupport::JSON
=> ActiveSupport::JSON
ruby-1.9.2-p136 :004 > j.encode({:team => "Celtics", :players => "20"})
=> "{\"team\":\"Celtics\",\"players\":\"20\"}"
ruby-1.9.2-p136 :005 > j.decode("{\"team\":\"Celtics\",\"players\":\"20\"}")
=> {"team"=>"Celtics", "players"=>"20"}
So for you it would be:
new IS.Presentation(<%= ActiveSupport::JSON.encode(raw(#course_step.step.step_presentation.step_presentation_files.map { |item| {'url' => item.slide.url, 'title' => item.title}})) %>)

Resources