Eternity Async Tutoring

I tried converting between hashes and arrays. I guess because hashes are key-value pairs that it can only convert arrays that consist of two things into hashes? I tried converting my larger arrays into hashes. Didn’t work. When I googled it, I didn’t find an explanation on converting between hashes and arrays. I just found example code where the arrays only had two values. Something like this:

I noticed it didn’t work if I just did [3,4]. It needed double square brackets. So the array has to have stuff already in pairs to make it into a hash?

I’d suggest trying the forum’s built-in bookmarking feature.

If that doesn’t work well for you, try a todo list.

1 Like

I just keep it in my head mostly. Sometimes it goes in the to do list for that day if I can sense it’s getting to much to hold in my head. Recently I’ve been doing that more because I’ve got like 4 active threads going on.

I’m going to look into the bookmark feature that Elliot mentioned.

Do you want to do more programming stuff?

Did you want to do something related to fraud laws?

Yes. I’ve been enjoying it.

That sounds interesting.

If we can work on both at once thats fine. If not, I’d prefer programming for now.

it’s up to you what activities you can manage at once. if you want to pick something that has been posted on the forum recently with some connection to fraud, and post about it, you can.

5.times {|n| puts n * 2 if n != 3}

See if you can predict the output of this ruby code before running it.

You may want to look things up. FYI, the curly braces and their contents are called a “block”, which is a way of providing an anonymous function as an input to a function. A function being anonymous means it hasn’t been saved anywhere permanent with a name, so you give a chunk of code right there instead of naming some pre-existing function.

Without looking anything up:

1.) It runs that block 5 times. What does that block do? Hmm. I don’t know what | | is doing. Hmm. puts n * 2 is multiplying whatever n is by 2 and then outputting it. If n != 3 is saying it will only do it if n does not equal 3. I think != is not equal.

or

2.) That whole blocks result is multiplied by 5.

Let me go through the rest of what you said:

So in

{|n| puts n * 2 if n != 3}

is a block. Ok.

So that block is an input to a function. That block helps put this thing called an anonymous function as an input to a function. An anonymous function is a function that hasn’t been saved, is not permanent, and doesn’t have a name. I assume that if we didn’t have that block anonymous function thing going on there you would need a pre-existing function. So is 5.times a function. Hmm.

After looking things up:

The times function: Ruby Integer times function with example - GeeksforGeeks :

The times function in Ruby returns all the numbers from 0 to one less than the number itself.

Looking at the output examples they gave. I think it gives one less than the number itself because it starts from 0. Seems like | | is necessary for the code but not sure. Googling again got me this: Better Know A Ruby Thing #5: Block Arguments – Noel Rappin Writes Here :

Syntactically, Ruby allows you to define a block at the end of any method call. That is the only place you can define a block. Ruby has two different syntaxes for delimiting a block: do/end and curly braces { } . In both cases you can define arguments to the block and local variables by putting them between pipe characters | var | at the beginning of the block.

So the variable n needs to be in between pipes in the block for it to work. Ok.

I’m pretty sure I know what puts n * 2 is doing. So just checking on n !=3. From Ruby - Operators :

Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

So yeah does not equal is right then.

Putting it all together:

5.times {|n| puts n * 2 if n != 3}

n will have the values 0, 1, 2, 3, 4
it will output a value n*2 if n does not equal 3.
so it outputs:
0, 2, 4, 8

Looks like I was right:

Why’d it output the 5 at the end?

great

Everything in ruby has a value (which can be nil).

Functions like times can say return stuff to specify their output value. If they don’t, the value of the last line of code in the function is the output value. It’s often set to something that could potentially be useful (in this case the number of times) rather than to nil.

In irb, when you enter some code, it evaluates it and prints its value. It still does this even if the code contains instructions to print other stuff. On the other hand, if you put the code in a text file and run it with ruby, it won’t print anything out unless you specifically say to. When code runs in general, lots of outputs just get ignored. But irb prints outputs to help you see what’s going on.

Ruby is an object oriented programming language. That’s a design pattern which is commonly used throughout. An “object” is a complex data type that groups together variables and functions. This helps organize functions in two ways. First, it separates functions into groups instead of there being a single list of all functions that exist. Second, it groups functions together with relevant data.

A “class” is the name for a type or category of object. Then one or more objects of that type can be made. E.g. Integer and String are classes in Ruby, and you can have multiple “instances” of them: 1, 2, 3, “hi”, “bye”, “string”, etc.

Every object instance has some data (in this case, what number it is, or what characters are in the string) and some functions. In object oriented programming, functions are commonly called “methods” and are called with a dot syntax.

Objects also help with reusing code. If you’re writing chess software, you’ll deal with 32 pieces and 64 squares. Instead of defining each one individually, you can make a piece class and a square class and use each class multiple times. You can also have sub-classes (like a knight or queen) which have the code for pieces plus additional customizations. If you’re making poker software, you can have a card class with variables for the suit and the number instead of writing separate code for all 52 cards.

Here’s an example of the dot syntax for calling methods:

3.zero?

“zero?” is a function in the Integer class which can be used with any Integer instance.

Object functions can take inputs. For example:

3.+(4)

Addition in ruby is an object-oriented method call. “+” is a function in the integer class (the same name “+” is also defined separately for some other classes – reusing function names can be confusing but can also be convenient and useful if done well).

You can write 3 + 4 and that works too, but that’s actually just an abbreviation, a convenience shortcut. For certain function names (none of which begin with a letter, I think) you can leave out the dot and ruby knows what you means. Similarly, for lots of function calls you can leave out parentheses and ruby figures it out anyway. Similarly, 3 < 4 works and is a short version equivalent to 3.<(4). “<” is a function defined for integers.

Function calls can be chained together. 3.-(4).+(5) Guess what this will output then run it and see.

All function calls in ruby are actually method calls. puts 3 is actually the same as self.puts(3). Ruby always has a current context with a default object (named “self” if you need to refer to it explicitly) which is used when you don’t specify. All ruby functions are grouped in some object. The “puts” method is defined in the “Kernel” class. When you define a function with “def”, it goes in the current default context unless you specify otherwise.

In irb, run 3.class, 3.class.ancestors, 3.methods and 3.class.methods.

Feeling a tad sick. Still wanted to do something before going to bed. Read over the post, understood most of it I think?

Try writing code to create a deck of cards: an array with 52 2-character strings, one for each card. Don’t get stuck for a long time if you don’t know how.

Am I writing an array with 52 2-character strings? Or am I writing code that creates an array with 52 2-character strings? I don’t know if the second is possible.

This?:

cards = Array [["A","Heart" ],[2,"Heart"],[3,"Heart"],[4, "Heart"],[5, "Heart"],[6, "Heart"],[7, "Heart"],[8, "Heart"],[9, "Heart"],[10, "Heart"],["J", "Heart"],["Q", "Heart"],["K", "Heart"],
["A","Spade" ],[2,"Spade"],[3,"Spade"],[4, "Spade"],[5, "Spade"],[6, "Spade"],[7, "Spade"],[8, "Spade"],[9, "Spade"],[10, "Spade"],["J", "Spade"],["Q", "Spade"],["K", "Spade"],
["A","Clubs" ],[2,"Clubs"],[3,"Clubs"],[4, "Clubs"],[5, "Clubs"],[6, "Clubs"],[7, "Clubs"],[8, "Clubs"],[9, "Clubs"],[10, "Clubs"],["J", "Clubs"],["Q", "Clubs"],["K", "Clubs"],
["A","Diamonds" ],[2,"Diamonds"],[3,"Diamonds"],[4, "Diamonds"],[5, "Diamonds"],[6, "Diamonds"],[7, "Diamonds"],[8, "Diamonds"],[9, "Diamonds"],[10, "Diamonds"],["J", "Diamonds"],["Q", "Diamonds"],["K", "Diamonds"]]

Oh. Character. So if I have the right idea then I should have used H,S,C,D. Ok. If my idea is right I will fix to just 2-characters.

The idea is to get the computer to do repetitive tasks. That way, even if there were a million cards, it would scale up with about the same amount of work for the programmer.

2-character card strings are like “4D” or “KH”.

To simplify, let’s start with only the cards from 2-9, so it’ll be a 32 card deck.

Breaking the problem down into smaller parts can help a lot. For example, you might want to start with just one suit before adding the other suits.

This?:
cards = Array[["2H"],["3H"],["4H"],["5H"],["6H"],["7H"],["8H"],["9H"], ["2S"],["3S"],["4S"],["5S"],["6S"],["7S"],["8S"],["9S"], ["2C"],["3C"],["4C"],["5C"],["6C"],["7C"],["8C"],["9C"], ["2D"],["3D"],["4D"],["5D"],["6D"],["7D"],["8D"],["9D"]]

No, you’re making the cards yourself, one by one. Use a loop.

1 Like

Haven’t started/put much thought into it yet but the thought occurred me: is it fine to look stuff up for this task? or should I try and just do it from what I know? (I’m assuming what we’ve gone over already/what I know is sufficient but I don’t know.)

yes you can look stuff up. you’ll probably need to look up at least a couple small things.

1 Like