Manipulating a class to alter another class while using CSV file - ruby
In ruby, if I have a CSV file that looks like this:
make,model,color,doors
dodge,charger,black,4
ford,focus,blue,5
nissan,350z,black,2
mazda,miata,white,2
honda,civid,brown,4
corvette,stingray,red,2
ford,fiesta,blue,5
bmw,m4,black,2
audi,a5,blue,2
subaru,brz,black,2
lexus,rc,black,2
If I run my code and choose "doors" for my "wanted_attribute" and "2" for my "value" (my gets.chomp), it outputs all the cars that only have 2 doors from the CSV file:
make: nissan, model: 350z, color: black, doors: 2
make: mazda, model: miata, color: white, doors: 2
make: corvette, model: stingray, color: red, doors: 2
make: bmw, model: m4, color: black, doors: 2
make: audi, model: a5, color: blue, doors: 2
make: subaru, model: brz, color: black, doors: 2
make: lexus, model: rc, color: black, doors: 2
How would I be able to condense this even more and make it that from this group of, doors 2, it condenses more into, color black, for example this should be the final output (outputs only color black cars):
make: nissan, model: 350z, color: black, doors: 2
make: bmw, model: m4, color: black, doors: 2
make: subaru, model: brz, color: black, doors: 2
make: lexus, model: rc, color: black, doors: 2
This is my current code:
require "csv"
class Car
attr_accessor :make, :model, :color, :doors
def initialize(make, model, color, doors)
#make, #model, #color, #doors = make, model, color, doors
end
def to_s
"make: #{self.make}, model: #{self.model}, color: #{self.color}, doors: #{self.doors}"
end
end
cars = CSV.read("so.csv").map{|car| Car.new(car[0], car[1], car[2], car[3])}
print "Select attribute: "
wanted_attribute = gets.chomp
print "Select value: "
value = gets.chomp
wanted_cars = cars.select{|car| car.instance_variable_get("##{wanted_attribute}") == value}
puts wanted_cars
please comment code
you can add multiple conditions inside your main logic:
print "Select color attribute: "
wanted_color_attribute = gets.chomp
print "Select color: "
color = gets.chomp
wanted_cars = cars.select{|car| car.instance_variable_get("##{wanted_attribute}") == value && car.instance_variable_get("##{wanted_color_attribute}") == "#{color}"}
Hope it helps!
Related
How to convert Str to enum?
enum Colors<red green blue> say red; # OUTPUT: red my $foo = "red"; my Colors $color = $foo.(...) What code do I put in the Stub to convert the Str "red" to the Color red?
The enum declarator installs the elements under the Colors package as well as providing the short names, thus red can also be accessed as Colors::red. Therefore, one can use the package lookup syntax to do the job: my Colors $color = Colors::{$foo}; Optionally providing an error or default: my Colors $color = Colors::{$foo} // die "No such color $foo";
Issue with YAML-based configuration defaults in Spring Boot project
I have a YAML configuration file as follows: spring.profiles: master cars: - color: red style: sedan doors: 4 - color: blue doors: 4 - color: black --- spring.profiles: default cars-default: color: not specified style: not specified doors: not specified As you can see, cars is a List, but cars-default is not. I know that if you define your default profile such that the "flattened" layout matches your active profile, Spring will happily merge the two profiles such that properties defined in the active profile will override the defaults. My challenge is that I want a single set of defaults to override each List object, so the resulting properties end up like this: cars: - color: red style: sedan doors: 4 - color: blue style: not specified doors: 4 - color: black style: not specified doors: not specified To make matters slightly more complicated, the Car POJO that these properties map to has hard-coded default values of their own that I can't override, so simply populating a Car object and a DefaultCar object and applying a merge operation on the two won't work. Essentially, what I need to have happen is that the two sets of properties are merged before instantiating and setting values in the Car object. I'm hoping there is a way I can tap into Spring's property resolver such that I can override the default behavior, though I'm not terribly familiar enough with this aspect of Spring, so I don't really know where to start.
You might just be able to use the standard YAML anchors and aliases and the merge key facility, if your YAML parser supports it. This: spring.profiles: master _: &car-default color: not specified style: not specified doors: not specified cars: - <<: *car-default color: red style: sedan doors: 4 - <<: *car-default color: blue doors: 4 - <<: *car-default color: black loads in a compatible parser as if you used: spring.profiles: master _: color: not specified style: not specified doors: not specified cars: - color: red style: sedan doors: 4 - color: blue style: not specified doors: 4 - color: black style: not specified doors: not specified Make sure the anchor (&car-default) is defined in the same YAML document (you have two) and before it's being used (with *car-default). It doesn't have to be defined as a value to a (top-level or other) key, the mapping could be an element in a sequence, e.g. the first element of the sequence that is the value of cars: spring.profiles: master cars: - &car-default color: not specified style: not specified doors: not specified - <<: *car-default color: red style: sedan doors: 4 - <<: *car-default color: blue doors: 4 - <<: *car-default color: black Of course Spring would have to ignore the defaults in the anchor values, so you might need to find some "comment" key-value pair and stash this in the value part.
Change column color in Oracle apex
How can I change the background color of every second column using interactive report.
First of all I recommend using css. If you don't like CSS just go directly to IR section in my answer CSS th:nth-child(even), td:nth-child(even){ background-color: red !important } or th:nth-child(2), td:nth-child(2) { background-color: red !important } th:nth-child(4), td:nth-child(4) { background-color: blue !important } or th#C152380905075548116, td[headers="C152380905075548116"] { background-color: red !important; } th#C152381026269548117, td[headers="C152381026269548117"] { background-color: blue !important; } where C152380905075548116 and C152381026269548117 are columns id that should be replaced. IR If you really need to use native IR functionalities you should follow 3 steps: Change report SQL query so the column contains name of the color you want to use as background eg: select ... VALUE1||'<span style="display: none">red</red>' as Artikl VALUE2||'<span style="display: none">blue</red>' as "Broj gresaka" .. from .. Add IR highlights Actions > Format > Highlight Name = Red background (Artikl) Sequence = Sequence Enabled = Yes Highlight Type = Cell Background Color = #FF7755 Text Color = null Column = Artikl Operator = Contains Expression = red and Name = Blue background (Broj gresaka) Sequence = Sequence Enabled = Yes Highlight Type = Cell Background Color = #99CCFF Text Color = null Column = Artikl Operator = Contains Expression = blue Set columns attribute Security > Escape special characters to No It is not the perfect solution but it works :-)
Colors are not concatenating correctly in Sass
I am trying to concatenate two colors in Sass: $helloWorld: pink; $secondString: red; p { color:$helloWorld + $secondString; } But the result is: p { color: pink; } Why aren't the colors concatenating to produce pinkred?
This is because Sass treats all colors as their hex value, regardless if they're named like pink. They're all hex values under the hood. Per the Sass Documentation: Colors Any CSS color expression returns a SassScript Color value. This includes a large number of named colors which are indistinguishable from unquoted strings. The emphasis is mine. The documentation states that the color value is returned, which is the hex value. The included link also shows that named colors such as pink are just hex values under the hood. To address the adding issue, refer to the documentation again: Color Operations All arithmetic operations are supported for color values, where they work piecewise. This means that the operation is performed on the red, green, and blue components in turn. For example: p { color: #010203 + #040506; } computes 01 + 04 = 05, 02 + 05 = 07, and 03 + 06 = 09, and is compiled to: p { color: #050709; } The same principle applies here. When you use addition on colors, you aren't concatenating them like strings, so pink + red is not pinkred. Instead, the hex values are added piecewise. Here's an example: $blue: blue; $red: red; p { color: $blue + $red } This yields: p { color: magenta } From the example above, you can see that this does not perform string concatenation, but it's adding blue (#0000FF) and red (#FF0000) to create magenta (#FF00FF). In your case, pink (#FFC0CB) and red (#FF0000) are added piecewise to produce #FFC0CB, which is just pink. That's why you get pink instead of pinkred. If you want to concatenate them like strings, do not use+. Instead, you can try string interpolation so the colors are treated as strings, not colors: p { color: $helloWorld#{$secondString} } That will yield: p { color: pinkred } You can also use a more verbose method so that it's forced to act like a string (unquote just gets rid of the quotes): p { color: unquote($helloWorld+ "" + $secondString); } Try it at SassMeister. Note that pinkred isn't a named colors in Sass.
Random color from array in Sass [duplicate]
This question already has an answer here: SASS: randomly pick background-image from a list (1 answer) Closed 7 years ago. I want to specify an array of colours and then apply the colors randomly to a list. So far I have it so that the colors will cycle through in order. How can I randomise it? Here is the Sass code so far: $colors: red, orange, yellow, green, blue, purple; #for $i from 1 through length($colors) { li:nth-child(#{length($colors)}n+#{$i}) { background: lighten(nth($colors, $i), 20%); } } li { list-style: none; padding: 1em; } and the markup: <ul> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> <li>g</li> <li>h</li> <li>i</li> <li>j</li> <li>k</li> <li>l</li> </ul> Example on Codepen: http://codepen.io/anon/pen/azbwJm
Edit: Sass introduced a module system. The random() function is transitioning to math.random(). See the documentation for the function and for the module system for more information. First, I should state a reminder to everyone reading that Sass is precompiled into CSS; you cannot achieve random behavior "at runtime" (i.e. on page load) using Sass. Sass has a random() function that might interest you: $colors: red, orange, yellow, green, blue, purple; $repeat: 20 // How often you want the pattern to repeat. // Warning: a higher number outputs more CSS. #for $i from 1 through $repeat { li:nth-child(#{length($colors)}n+#{$i}) { background: lighten(nth($colors, random(length($colors))), 20%); } } li { list-style: none; padding: 1em; } This chooses a random index of your $colors array and uses the associated color. Note: the documentation states that random($limit) "returns a random whole number between 1 and $limit." This includes $limit as a possible value. As a result, if you use nth($colors, random(length($colors) + 1)), you are liable to get an error for using an index out of bounds.