If statement inside an array creation ruby

Peti

New Member
#1
I am trying to do a if statement inside a array creation
Mã:
markers_index = Array.new       
@events.each_with_index do |event, index|
...

        markers_index << {
          type: 'Feature',
          geometry: {
            type: 'Point',
            coordinates: [event.longitude, event.latitude]
          },
          properties: {
            markerurl: event.photo.marker.url,
            divclass: marker_class,
            if has_popup
              popupContent: marker_popup
            end
          }
        }
    end
But it throws me a syntax error
unexpected ':', expecting keyword_end popupContent: 'marker_popup'
Is it a typo or I simply can't do this and need to repeat the whole thing wrapping it in a if else wrapping my marker_index variable ? Trying to stay DRY..
 

Admin

Administrator
Thành viên BQT
#2
For instance :
Mã:
h= {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [event.longitude, event.latitude]
  },
  properties: {
    markerurl: event.photo.marker.url,
    divclass: marker_class,
  }
}
if has_popup
  h[:properties][:popupContent]= marker_popup
end
markers_index << h
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top