Eternity Async Tutoring

Since I’m a bit stuck/lost I decided to go back and read some of the stuff you posted about Ruby.

Objects group together variables, things that can hold information, and functions, ways to manipulate the information. Objects have both? Hmm. Grouped, not combined?

It being object oriented helps organize functions into groups. So non-object oriented languages wouldn’t group functions.

category/type of object = class. one or more objects of that class can be made. integer and string are classes. instances are objects?

functions are the same as methods

2,upto(4) ← . syntax

we making poker software? ;o

you can make classes? hmm. im getting a bit lost on what classes actually are then

Whats an object function?

Never did this I think. Woops. 3 subtract 4 add 5? so 4:

Hmm. To clarify: are functions and methods the same things when it comes to Ruby?

Am I misunderstanding something here? So here’s my code (I removed the other functions that you told me to make for now because I wasn’t doing anything with them yet):

cards = Array.new
suits = ["S","C","D","H"]
letters = ["A","T","J","Q","K"]

suits.each do |a|
  2.upto(9) do |n|
  cards << "#{n}#{a}"
  end
  letters.each do |l|
  cards << "#{l}#{a}"
  end
end

deck = cards.shuffle

hand = deck.slice!(0..4)

def hand_to_ranks(hand)
  ranks=Array.new

  hand.each do |n|
    ranks<<n[0]
  end
  ranks
end


hand_to_ranks(hand)


So hand_to_ranks(hand) returns value in irb:
image

I just replaced the hand in hand_to_rank(hand) with the array you provided. So
hand_to_ranks(%w[AH AD 5H 3S 2C]).:

image

oh it works, nvm then

You can simplify your hand_to_ranks function using map instead of each.

Sometimes they group functions in other ways like namespaces.

Yes you can make custom classes and also make changes to built-in classes.

A function that’s built into an object.

yes. technically ruby doesn’t have pure functions like regular math. it’s fully object-oriented. you can make some stuff look like regular functions but there’s an implied object.

Ok. So all functions/methods in ruby are object functions. All functions are object functions which are functions that are built into an object. Ruby only has objects. Some stuff may look like a regular function but there’s an implied object. Like puts being self.puts.

def hand_to_ranks(hand)
  ranks = hand.map {|n| n[0]}
end

image

neat

1 Like

Ran into this quote from Objectively Speaking: Ayn Rand Interviewed: Podritske, Marlene, Schwartz, Peter: 9780739131954: Amazon.com: Books while doing reviews:

Nothing but his own intelligence and integrity. The issue here is the same as in science, where man is also not guaranteed against error. Man has to arrive at knowledge by a rational process. And that process is not infallible. Man can make errors, and so he has rules by which he can trace his errors. The science that provides these rules is logic, which is the art of non-contradictory identification. If a man makes an error in his reasoning, sooner or later it will lead him to a contradiction. By that means, he can know that he has made a mistake, can retrace the steps of his reasoning process and can unearth his error.

I think I highlighted it cause of the stuff about being fallible.

1 Like

I’m confused about something:

def hand_to_ranks(hand)
  rank = hand.map {|n| n[0]}
end

def rank_to_number(rank)
  # J = 11, Q = 12, K = 13, A = 14. For now we'll ignore how A can sometimes be 1.
puts rank.class
end

It says rank is undefined. Is it because its used in a definition of a function “hand_to_ranks”. I put the puts command so I could see some output in the IDE, but I also did it in irb (without puts) to see the return value of rank:

Oh yeah. class doesn’t have anything to do with my solution to this part of the programming assignment. I was testing out things I thought maybe would work and I noticed rank kept returning undefined so I just chose to run something simple to double check.

Inputs to functions are only available inside their function.

Ok. I’m a bit confused with how that all works then. Just to check the four functions you gave to me to work out are meant to actually be used, right? They’re not just like different parts I need to figure out?

I kinda am confused with the whole function thing. I did find a way to get to rank_to_numbers but I did it outside of my function.

cards = Array.new
suits = ["S","C","D","H"]
letters = ["A","T","J","Q","K"]

suits.each do |a|
  2.upto(9) do |n|
  cards << "#{n}#{a}"
  end
  letters.each do |l|
  cards << "#{l}#{a}"
  end
end

deck = cards.shuffle

hand = deck.slice!(0..4)

rank = hand.map {|n| n[0]}

rank_to_number = {"A" => 14, "J"=> 11, "Q"=>12, "K"=>13,"T"=>10, "2"=>2, "3"=>3, "4"=>4, "5"=>5 , "6"=>6, "7"=>7, "8"=>8, "9"=>9}

number = Array.new

rank.each do |n|
  number << rank_to_number[n]
end


I know this isn’t exactly what I was supposed to but I had an idea for rank_to_number that I thought could work, wasn’t working with the function stuff, and so I just tried it out of the function last night. I spent a few minutes trying to do it with function but its not working. Hmm. In the assignment you gave for rank_to_number you didn’t say an array for the numbers so there’s that too.

The general concept of a function is it’s a reusable chunk of code which takes some inputs and then calculates an output using only the input data. Functions are important to understand and be able to use. (Most programming languages allow impure functions which can interact with external data. This is widely used but generally only done in specific, organized ways.)

Yes the functions are meant to be used.

If you show your problem using a rank_to_number function I could comment more.

I don’t know what this means.

Ahh when doing rank_to_numbers I don’t know if it should be an array. You didn’t mention it in your comment when giving me the functions to work through:

def rank_to_number(rank)
  # J = 11, Q = 12, K = 13, A = 14. For now we'll ignore how A can sometimes be 1.
  # code
end

You didn’t know if what should be an array?

Hmm. I guess: should the return value of that function be an array.

Or, I guess, what am I trying to do in this section? What I think I’m trying to do is here turn my array that I made earlier that has the ranks into an array that has the number value of those ranks.

OK, I see. def rank_to_number(rank) is self-explanatory to me but you’re not familiar with naming conventions.

X_to_Y indicates a conversion function that turns X (the input) to Y (the output).

If the input or output were an array, I’d use a plural noun instead of a singular noun to indicate that.

It’s often best to make helper functions that do small, individual tasks. If you need to convert multiple things, you can call the function multiple times, e.g. using Array#map.

1 Like

So I did this:

def rank_to_number(rank)
  # J = 11, Q = 12, K = 13, A = 14. For now we'll ignore how A can sometimes be 1.
  rtn = {"A" => 14, "J"=> 11, "Q"=>12, "K"=>13,"T"=>10, "2"=>2, "3"=>3, "4"=>4, "5"=>5 , "6"=>6, "7"=>7, "8"=>8, "9"=>9}
  rtn[rank]
end

Seems to work in irb, if I understand what the goal of this code was,:
image

Oh yeah I made a feature request for the quoting issue:

For

def find_all_pairs(hand)
  # returns an empty array for no pairs, otherwise an array with the ranks of the pairs found. cards can't be reused in multiple pairs, so 3 jacks form one pair and 4 jacks form 2 pairs.
  hand_ranks = hand_to_ranks(hand)
  # more code
end

I can’t find anything that I think would be helpful. My plan is just to use a bunch of if statements. Something like “if hand[0] = hand[1]” and do that to check for every pair. I’m going to try this way tomorrow to see if it even works but any other suggestion?

That will be a hassle and wouldn’t scale. What if a hand had 10 or 1000 cards?

So let’s break the problem down into smaller parts.

Suppose you want to know whether the first card in the hand pairs with any other card in the hand? How could you figure that out using a loop or some other method where the number of lines of code doesn’t go up as the hand gets larger?

I joined in Improving quoting quote accuracy - #9 by curi - Feature - Discourse Meta

1 Like