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!
I am trying to use App Search, and when I index a document that looks like:
name: 'metalica t-shirt'
colors: ['red', 'black']
The engine removes 'black' and leaves it as:
name: 'metalica t-shirt'
colors: 'red'
But I want it to appear both under 'red' color and 'black' color filter on Search UI, is it supported?
Using a yaml configuration as below, I am trying to use the default_car as one of the items in a list and override the color attribute.
But there is a parse error stating: did not find expected key while parsing a block mapping
default_car: &default_car
brand: xxx
model: xxx
color: red
cars_sold:
- <<: *default_car
color: blue
cars_available:
- <<: *default_car
color: yellow
Is it possible to achieve something like this ? If I am approaching this wrong, what would be the best way to not to repeat the default car mapping again and again ?
For the sake of answering an old, unanswered question to help future searchers, it seems as though you simply need to remove erroneous additional spaces before the color overrides.
default_car: &default_car
brand: xxx
model: xxx
color: red
cars_sold:
- <<: *default_car
color: blue
cars_available:
- <<: *default_car
color: yellow
Test it with https://jsonformatter.org/yaml-validator or https://yamlchecker.com/ to confirm this.
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.
When creating a color scheme in SASS what's the conventional variable names for defining colors?
I know using color names are bad. Such as:
$blue
$red
$green
But I've not seen an alternative. I'm struggling for variable names for colors on the site that convey meaning.
Any ideas?
I found another idea in "SASS & Color Variables" article. The solution suggested by Sacha Greif is to use some variables to store descriptive color names, and some other to assign those colors to their functions:
// first we set descriptive variables:
$darkgrey: #333333;
$blue: #001eff;
// then we set functional variables:
$text_color: $darkgrey;
$link_color: $lightblue;
$border_color: $lightblue;
.myClass {
color: $text_color;
border-color: $border_color;
}
a {
color: $link_color;
}
I'm just beginning with SASS and don't know which approach is more practical, but I like the way it separates colors from their function.
In my personal experience the most useful way to name colors is to do it in regards of the color's function, such as
$background
$contrast
$text
$aside
$link
And so on. Of course which colors and name may depend on the design.
Then you may have different and exchangeable color schemes defined on different styles, such as:
_dark_scheme.scss
_light_scheme.scss
_pastels.scss
The idea here, is that you can use the same color variables in your main stylesheets, and do not depend on specific colors.
I like the idea of combining generic to specific naming (good for code completion) and description/functional naming. So you have something like this:
// Descriptive naming
$color-gray-light: #f3f3f3;
$color-gray-dark: #999999;
$color-red: red;
// Functional naming
$link-color: $color-red;
$link-border-color: $color-gray-light;
You can even create a mixin for greys (in the example RGBA is used for transparency, for example black on a red background would be more visible if it is 80% transparent black rather than dark grey).
#mixin grey($intensity: 0.5, $type: color) {
#{$type}: rgba(black, $intensity);
}
.i-am-50-percent-gray {
#include grey(0.5, color);
}
Give the result
.i-am-50-percent-gray {
color: rgba(0, 0, 0, 0.5);
}