Program I'm making has a simple configuration file looking something like this.
#overlays = {
:foo => "http://www.bar.com",
:bar => nil,
}
What I need to do is go through this hash and get the following output.
OverlayKey[0]='foo'
OverlayVal[0]='http://www.bar.com'
OverlayKey[1]='bar'
OverlayVal[1]='nil'
In order to keep my configuration like I want it I need some fake index numbers. Would rather not add numbers into the hash, it would make the configuration look a bit ugly. So I been playing around with artificially generating the numbers during output.
This is ugly but I"m just playing around with it currently.
def makenumbers
#numbers = []
length = #overlays.length - 1
(0..length).each do |num|
#numbers.push("#{num}")
end
end
makenumbers
#overlays.each do |key,val|
#numbers.each do |num|
puts "OverlayKey['#{num}']='#{key}'"
puts "OverlayVal['#{num}']='#{val}'"
end
end
Which is giving me something like this.
OverlayKey['0']='foo'
OverlayVal['0']='http://www.bar.com'
OverlayKey['1']='foo'
OverlayVal['1']='http://www.bar.com'
OverlayKey['0']='bar'
OverlayVal['0']=''
OverlayKey['1']='bar'
OverlayVal['1']=''
Understand why this doesn't give me the output I want, although after playing with it for a bit I'm not really sure how to do what I want without adding numbers into the hash during configuration. Sure this is pretty simple I just can't seem to wrap my head around it.
I don't know what the problem is other than Hashes are unsorted by default:
overlays = {
:foo => "http://www.bar.com",
:bar => nil,
}
overlays.each_with_index do |(k,v), i|
puts "OverlayKey['#{i}']=#{k.to_s.inspect}"
puts "OverlayVal['#{i}']=#{v.to_s.inspect}"
end
Output looks like this:
OverlayKey['0']="bar"
OverlayVal['0']=""
OverlayKey['1']="foo"
OverlayVal['1']="http://www.bar.com"
As a note:
# Instead of this:
"#{num}"
# Use this:
num.to_s
Related
I made a simple program with a single method and I'm trying to test it, but I keep getting this weird error, and I have no idea why it keeps happening.
Here's my code for the only method I wrote:
def make_database(lines)
i = 0
foods = hash.new()
while i < lines.length do
lines[i] = lines[i].chomp()
words = lines[i].split(',')
if(words[1].casecmp("b") == 0)
foods[words[0]] = words[3]
end
end
return foods
end
And then here's what I have for calling the method (Inside the same program).
if __FILE__ == $PROGRAM_NAME
lines = []
$stdin.each { |line| lines << line}
foods = make_database(lines).new
puts foods
end
I am painfully confused, especially since it gives me a different random number for each "Undefined method 'new' for (Random number)".
It's a simple mistake. hash calls a method on the current object that returns a number used by the Hash structure for indexing entries, where Hash is the hash class you're probably intending:
foods = Hash.new()
Or more succinctly:
foods = { }
It's ideal to use { } in place of Hash.new unless you need to specify things like defaults, as is the case with:
Hash.new(0)
Where all values are initialized to 0 by default. This can be useful when creating simple counters.
Ruby classes are identified by leading capital letters to avoid confusion like this. Once you get used to the syntax you'll have an easier time spotting mistakes like that.
Note that when writing Ruby code you will almost always omit braces/brackets on empty argument lists. That is x() is expressed simply as x. This keeps code more readable, especially when chaining, like x.y.z instead of x().y().z()
Other things to note include being able to read in all lines with readlines instead of what you have there where you manually compose it. Try:
make_database($stdin.readlines.map(&:chomp))
A more aggressive refactoring of your code looks like this:
def make_database(lines)
# Define a Hash based on key/value pairs in an Array...
Hash[
# ...where these pairs are based on the input lines...
lines.map do |line|
# ...which have comma-separated components.
line.split(',')
end.reject do |key, flag, _, value|
# Pick out only those that have the right flag.
flag.downcase == 'b'
end.map do |key, flag, _, value|
# Convert to a simple key/value pair array
[ key, value ]
end
]
end
That might be a little hard to follow, but once you get the hang of chaining together a series of otherwise simple operations your Ruby code will be a lot more flexible and far easier to read.
So, pretend we have the following three methods that check a grid to determine if there is a winner, and will return true if there is.
def win_diagonal?
# Code here to check for diagonal win.
end
def win_horizontal?
# Code here to check for horizontal win.
end
def win_vertical?
# Code here to check for vertical win.
end
I would like to push the returned values of each method into an Array instead of literally using the method names. Is this possible?
def game_status
check_wins = [win_vertical?, win_diagonal?, win_horizontal?]
if check_wins.uniq.length != 1 # When we don't have only false returns from methods
return :game_over
end
end
What you are looking for will indeed work in ruby.
def hello_world?
"hello world!"
end
a = [hello_world?]
Prints out
=> ["hello world!"]
Hope that helps. IRB is your friend when you wonder if something is possible in Ruby :-)
Simpler way (and very readable) yet:
def game_status
win_vertical? || win_diagonal? || win_horizontal?
end
If, for example, win_vertical? returns true, the other algorithms won't even need to run. You return immediately.
Or, if you need to know in which way the user won, I mean, if you need to preserve the results of all methods after they ran, you can use a hash, like:
{:vertical => win_vertical?, :diagonal => win_diagonal?, :horizontal => win_horizontal?}
This solution, like the array one, is worse than the first one above for it runs all algorithms all the time. If they are complex, you may have a problem. =)
You can do something like this when you really want to store all return values in an array:
def game_status
check_wins = [win_vertical?, win_diagonal?, win_horizontal?]
return :game_over if check_wins.any?
end
For readability I would prefer:
def game_status
return :game_over if win_vertical? || win_diagonal? || win_horizontal?
end
I'm using group_by to return a hash that I would like to order by key first, and then order the values under that key.
Here's the code I've come up with so far. Everything works fine, except I can't figure out how to sort the values after they're grouped.
I have a method, 'pp_accounts_with_properties' which shows my intent and prints out everything in the correct order (thanks to the properties.sort_by! call)... But it seems like I should be able to accomplish this somehow in the 'accounts_with_properties' method.
class ProofList
attr_reader :client
def initialize(client)
#client = client
end
def properties
#client.properties.joins(invoices: [charges: [:charge_credits]])
.where(charge_credits: { approved: false }).uniq
end
def accounts_with_properties
unsorted_accounts_with_properties.sort_by { |account, properties| account[:acct_number] }
end
def unsorted_accounts_with_properties
properties.group_by { |property| property.account }
end
def pp_accounts_with_properties
accounts_with_properties.each do |account, properties|
puts account.name
properties.sort_by! { |p| p[:name] }
properties.each do |property|
puts property.name
end
end
end
end
Are you...
Talking about showing some kind of sorted display of the keys and values of the Hash, or
Actually rearranging the interal structure of the Hash in some way? So that, for example, a call to myhash.keys always returns things in a certain order?
If 2, why? You're barking up the wrong tree. Hashes are just associations of keys with values, and if you're doing anything where the order of the keys matters, (other than trying to make a pretty/readable display), you're going about something the wrong way.
Check out this article for a clearer explanation than mine: http://www.rubyinside.com/how-to/ruby-sort-hash
So I have a class like this:
def Word
end
and im looping thru an array like this
array.each do |value|
end
And inside that loop I want to instantiate an object, with a handle of the var
value = Word.new
Im sure there is an easy way to do this - I just dont know what it is!
Thanks!
To assign things to a dynamic variable name, you need to use something like eval:
array.each do |value|
eval "#{value} = Word.new"
end
but check this is what you want - you should avoid using eval to solve things that really require different data structures, since it's hard to debug errors created with eval, and can easily cause undesired behaviour. For example, what you might really want is a hash of words and associated objects, for example
words = {}
array.each do |value|
words[value] = Word.new
end
which won't pollute your namespace with tons of Word objects.
Depending on the data structure you want to work with, you could also do this:
# will give you an array:
words = array.map { |value| Word.new(value) }
# will give you a hash (as in Peter's example)
words = array.inject({}) { |hash, value| hash.merge value => Word.new }
# same as above, but more efficient, using monkey-lib (gem install monkey-lib)
words = array.construct_hash { |value| [value, Word.new ] }
Let's say I'm doing a simple .each but I still want to keep the position in the loop, I can do:
i = 0
poneys.each do |poney|
#something involving i
#something involving poney
i = i + 1
end
This doesn't look very elegant to me. So I guess I could get rid of the .each:
for i in 0..poneys.size-1 do
#something involving i
end
... or something similar with a different syntax.
The problem is that if I want to access the object I have to do:
for i in 0..poneys.size-1 do
poney = poneys[i]
#something involving i
#something involving poney
end
... and that's not very elegant either.
Is there a nice and clean way of doing this ?
You can use Enumerable#each_with_index
From the official documentation:
Calls block with two arguments, the
item and its index, for each item in
enum.
hash = Hash.new
%w(cat dog wombat).each_with_index do |item, index|
hash[item] = index
end
hash #=> {"cat"=>0, "wombat"=>2, "dog"=>1}
Depends what do you do with poneys :) Enumerable#inject is also a nice one for such things:
poneys.inject(0) do |i, poney|
i += 1; i
end
I learned a lot about inject from http://blog.jayfields.com/2008/03/ruby-inject.html which is great article.