Ruby print hash in grid format - ruby

I'm working on coding a chess board. The structure of my board will be like this:
# a b c d e f g h
# 1 * * * * * * * * # <= Black pieces on top
# 2 * * * * * * * *
# 3 * * * * * * * *
# 4 * * * * * * * *
# 5 * * * * * * * *
# 6 * * * * * * * *
# 7 * * * * * * * *
# 8 * * * * * * * * # <= White pieces on bottom
I created an #board hash, which stores the value of any item in the grid (i.e. pieces or blank space).
How can I take my hash, which maps a grid location to a "*" currently (i.e. #board['a8']=> '*', etc), and output that hash in the grid-like format?
Here's my #board variable:
def drawBoard
#board = Hash.new
letter='a'
while letter <= 'h'
i=1
while i<9
#board["#{letter}#{i}"] = "*"
i+=1
end
letter=letter.next
end
#board
end
Current output is just the hash itself. i.e.,
{"a1"=>"*", "a2"=>"*", "a3"=>"*", "a4"=>"*", "a5"=>"*", "a6"=>"*", "a7"=>"*", "a8"=>"*", "b1"=>"*", "b2"=>"*", "b3"=>"*", "b4"=>"*", "b5"=>"*", "b6"=>"*", "b7"=>"*", "b8"=>"*", "c1"=>"*", "c2"=>"*", "c3"=>"*", "c4"=>"*", "c5"=>"*", "c6"=>"*", "c7"=>"*", "c8"=>"*", "d1"=>"*", "d2"=>"*", "d3"=>"*", "d4"=>"*", "d5"=>"*", "d6"=>"*", "d7"=>"*", "d8"=>"*", "e1"=>"*", "e2"=>"*", "e3"=>"*", "e4"=>"*", "e5"=>"*", "e6"=>"*", "e7"=>"*", "e8"=>"*", "f1"=>"*", "f2"=>"*", "f3"=>"*", "f4"=>"*", "f5"=>"*", "f6"=>"*", "f7"=>"*", "f8"=>"*", "g1"=>"*", "g2"=>"*", "g3"=>"*", "g4"=>"*", "g5"=>"*", "g6"=>"*", "g7"=>"*", "g8"=>"*", "h1"=>"*", "h2"=>"*", "h3"=>"*", "h4"=>"*", "h5"=>"*", "h6"=>"*", "h7"=>"*", "h8"=>"*"}
Edit:
Thanks to David's answer, he led me toward a much more compact hash generation method as well. The updated (and working) code:
def drawBoard
#board = Hash.new
('a'..'h').each do |letter|
(1..9).each do |i|
#board["#{letter}#{i}"] = "*"
print #board["#{letter}#{i}"]
end
puts
end
end

Provided that you can use symbols for the keys in #board instead of strings:
#board = {:"a1" => "*", ...}
I think the easiest would be to prepare a fixed template string, and do string format to rewrite the grids.
Template = <<_
a b c d e f g h
1 %{a1} %{b1} %{c1} %{d1} %{e1} %{f1} %{g1} %{h1}
2 %{a2} %{b2} %{c2} %{d2} %{e2} %{f2} %{g2} %{h2}
3 %{a3} %{b3} %{c3} %{d3} %{e3} %{f3} %{g3} %{h3}
4 %{a4} %{b4} %{c4} %{d4} %{e4} %{f4} %{g4} %{h4}
5 %{a5} %{b5} %{c5} %{d5} %{e5} %{f5} %{g5} %{h5}
6 %{a6} %{b6} %{c6} %{d6} %{e6} %{f6} %{g6} %{h6}
7 %{a7} %{b7} %{c7} %{d7} %{e7} %{f7} %{g7} %{h7}
8 %{a8} %{b8} %{c8} %{d8} %{e8} %{f8} %{g8} %{h8}
_
Template % #board
If you let #board be a flat array instead (which can be handled by using modulo operations), then replace the %{..} above with %s, and it will work the same way.

Here is a starting point you can use. You would need to add the row and column labels yourself, and add spaces, but this should get you going in the right direction:
('a'..'h').each do |letter|
(1..8).each do |i|
print #board["#{letter}#{i}"]
end
puts # end the line
end

Related

Array can't be coerced into Float (TypeError) on my Ruby code

I am supposed to make variables that convert standard units into metric units. This is an exercise for a lesson on Learn Ruby the Hard Way. I'm trying to run the following code in PowerShell. The information inside the code is from the author of the book.
name = 'Zed A. Shaw'
age = 35 # not a lie in 2009
height = 74 # inches
weight = 180 # lbs
eyes = 'Blue'
teeth = 'White'
hair = 'Brown'
cm = 2.54
kg_1 = 2
kg_2 = 1/10
puts "Let's talk about #{name}."
puts "He's #{height * cm} inches tall."
puts "He's #{(weight * kg_1) - kg_2} pounds heavy."
puts "Actually that's not too heavy."
puts "He's got #{eyes} and #{hair} hair."
puts "His teeth are usually #{teeth} depending on the coffee."
# this line is tricky, try to get it exactly right
puts "If I add #{age}, #{height * cm}, and #{(weight * kg_1) - kg_2 }"
puts "I get #{age + (height * cm) + [(weight * kg_1) - kg_2]}."
It failed when I had to add up everything at the end. When I try to run it in PowerShell, this comes up:
Traceback (most recent call last):
1: from ex5.rb:20:in `<main>'
ex5.rb:20:in `+': Array can't be coerced into Float (TypeError).
What is my error, and how do I fix it?
The problem lays in this line:
"I get #{age + (height * cm) + [(weight * kg_1) - kg_2]}."
You probably want to see sth like
I get 1234.12.
You use [] brackets to group operations (as you'd do in math class). In Ruby you can only use () to group. [] is a notation for introducing an array. Try this one:
"I get #{age + (height * cm) + ((weight * kg_1) - kg_2)}."
Looks like the error occurs on line:
puts "I get #{age + (height * cm) + [(weight * kg_1) - kg_2]}."
The issue here is using [] indicates the program should be looking for an array object. By using [] here, it is interpreted that you're looking to add an array object to a float object using the + method (operators are methods in Ruby). Ruby spits out that error message. It seems what you're looking to do is to:
add age to (height * cm) to return a float
add that float value to the difference between (weight * kg_1) and kg_2
By replacing:
[(weight * kg_1) - kg_2]
with
((weight * kg_1) - kg_2)`
you should receive the intended interpolated value of:
I get 582.96

Ruby, Convert cron range into digit

Using Ruby
I would like that any ranges that appear in a cron to be expanded to the contained numbers in the range ex:
0,5,7,30-35 1,3-8,20 * * * /script.sh
expanded :
0,5,7,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh
Any help is appreciated
Assuming that 0,5,7,30-35 1,3-8,20 * * * /script.sh is a string, a simple gsub would work:
str = "0,5,7,30-35 1,3-8,20 * * * /script.sh"
str.gsub(/(\d+)-(\d+)/) { ($1..$2).to_a.join(',') }
#=> "0,5,7,30,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh"

rexx reading file data onto panel

can you please suggest me the manual or example codes on how i can read the content of a file to be displayed in a rexx panel.
the number of lines from the file can vary and so cannot use the static manner.
thanks,
Samuel Mathews.
In ZOS to read the file in rexx use the execio command
i.e.
"EXECIO * DISKR myindd (STEM fileContentsVar."
Reads the file into a stem variable (fileContentsVar.0 holds the number of records and
fileContentsVar.1 ... hold the actual data).
You could store the file contents in a ISPF table and display the table using the
TBDispl command
The rexx code will be roughly
address ispexec
'tbcreate myfile names(line)'
do i=1 to fileContentsVar.0
line = fileContentsVar.i
'tbadd myfile'
end
'tbtop myfile'
'tbdispl mypanel'
'tbend myfile'
For an example of a table-panel definition see http://pic.dhe.ibm.com/infocenter/zos/v1r12/index.jsp?topic=%2Fcom.ibm.zos.r12.f54dg00%2Fispzdg8040.htm
A table-panel would look like:
************************************************************
* )Attr *
* # Type(output) Intens(low) Just(asis) Caps(off) *
* )Body *
* -------------------- ????????????????? ----------------- *
* +Command ==>Cmdfld +Scroll ==>_samt+ *
* + *
* This table shows ... *
* *
* Line *
* )Model * ---- The model setion holds the
* #line + * Table display section
* *
* )Init *
* &samt=page *
* )Proc *
* )End *
************************************************************

Symfony 2.1 - Client side validation is only working for required contraint

As the assertions of Entities changed from MinLength and MaxLength to Length, Min and Max to Range, the validator guesser didn't follow these changes and the max_length attribute doesn't get filled, so HTML validation doesn't work for length.
I think there should be a new case for Symfony\Component\Validator\Constraints\Length in guessMaxLengthForConstraint function in Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser.php
Or is it a solved.
I'm using 2.1.6 but I tried 2.1.7 and there is no difference
Im using this:
/**
* #var float $height
*
* #ORM\Column(name="height", type="decimal", nullable=true)
*
*
* #Assert\Range(
* min = "20",
* max = "96",
* minMessage = "You must be at least 20 tall",
* maxMessage = "You cannot taller than 96"
* )
* #Assert\NotBlank(groups={"registration_step_two","profile_measurement"})
* #Assert\Regex(pattern= "/[0-9]/",message="Require number only")
* #Assert\MinLength(10)
*/
I added group validation to them and its finally work

How do I create a .p12 file in Ruby?

I've looked at the Ruby OpenSSL documentation, but I can't quite figure out the pieces I need to put together to make a .p12 file.
There's also this tutorial, but the comments belie an ambivalence about its correctness.
From ossl_pkcs12.c:
/*
* call-seq:
* PKCS12.create(pass, name, key, cert [, ca, [, key_pbe [, cert_pbe [, key_iter [, mac_iter [, keytype]]]]]])
*
* === Parameters
* * +pass+ - string
* * +name+ - A string describing the key.
* * +key+ - Any PKey.
* * +cert+ - A X509::Certificate.
* * * The public_key portion of the certificate must contain a valid public key.
* * * The not_before and not_after fields must be filled in.
* * +ca+ - An optional array of X509::Certificate's.
* * +key_pbe+ - string
* * +cert_pbe+ - string
* * +key_iter+ - integer
* * +mac_iter+ - integer
* * +keytype+ - An integer representing an MSIE specific extension.
*
* Any optional arguments may be supplied as nil to preserve the OpenSSL defaults.
*
* See the OpenSSL documentation for PKCS12_create().
*/
So (untested and probably incorrect - I am not very familiar with Ruby):
p12 = OpenSSL::PKCS12.create("password", "key", pkey, cert)
p12_bytes = p12.to_der

Resources