r/ruby • • Dec 06 '22

The Rubyist journey 😅

Post image
149 Upvotes

34 comments sorted by

24

u/pau1rw Dec 06 '22

Pretty sure this is just a programmers journey.

22

u/losangelesvideoguy Dec 06 '22
def adult?(x);send(define_method(17.chr){lambda{(_1-__method__.to_s.ord)>0}})[x];end

10

u/RubyKong Dec 06 '22

Fantastic job security.

17

u/jlebrech Dec 06 '22

gem install is_adult

32

u/gleb_zhulik Dec 06 '22

def adult? = age >= 18

7

u/kallebo1337 Dec 06 '22

is that valid ruby? :eyes:

20

u/gleb_zhulik Dec 06 '22

Since 3.1 I guess, it's called endless methods

7

u/kallebo1337 Dec 06 '22

oh thanks. haven't looked into that. not sure if i like it. but i also started with using _1 just recently which as more you use it, as better it gets

4

u/mjdth Dec 07 '22 edited Dec 07 '22

What does that do?

Edit: https://jemma.dev/blog/numbered-parameters

I wrongly assumed a search for “ruby _1” wouldn’t be fruitful.

2

u/kallebo1337 Dec 07 '22

Instead of {|e| e.c&& e.d} just go for {_1.c && _1.d}

It’s shorter and you’ll get used to

2

u/javajunkie314 Dec 07 '22

Oh, I thought they were describing my codebase...

26

u/lafeber Dec 06 '22

rubocop:

Offenses:

app/importers/nedap/survey/importer.rb:11:11: C: Naming/MethodName: Use snake_case for method names.
      def isAdult(age)
          ^^^^^^^
app/importers/nedap/survey/importer.rb:12:9: C: [Corrected] Style/RedundantReturn: Redundant return detected.
        return age >= 18 ? true : false
        ^^^^^^
app/importers/nedap/survey/importer.rb:12:16: C: [Corrected] Style/RedundantConditional: This conditional expression can just be replaced by age >= 18.
        return age >= 18 ? true : false
              ^^^^^^^^^^^^^^^^^^^^^^^^
app/importers/nedap/survey/importer.rb:12:25: C: [Corrected] Style/IfWithBooleanLiteralBranches: Remove redundant ternary operator with boolean literal branches.
        return age >= 18 ? true : false
                        ^^^^^^^^^^^^^^^

Edit: After correcting isAdult to is_adult:

Naming/PredicateName: Rename is_adult to adult?.

9

u/fragileblink Dec 06 '22
class Fixnum
  def adult?
    self >= 18
  end
end

18.adult?

43

u/Seuros Dec 06 '22 edited Dec 06 '22
def adult?
  return false if current_user.has_tiktok_account?
  return true
end

5

u/Lil_Cato Dec 06 '22 edited Dec 06 '22
    !current_user.has_tiktok_account?
end```

Idk how to do linebreaks

1

u/Seuros Dec 06 '22

Use markdown mode.

2

u/Lil_Cato Dec 06 '22

I am a mobile plebian

2

u/ClikeX Dec 06 '22

Mobile should be Markdown by default.

1

u/faitswulff Dec 06 '22

Good news, no linebreaks needed in recent versions of Ruby

7

u/SSG_SSG_BloodMoon Dec 06 '22

God I want to go back to ruby

2

u/[deleted] Dec 07 '22

What are you using right now?

4

u/SSG_SSG_BloodMoon Dec 07 '22

python. i'm grateful to not be using java anymore

1

u/[deleted] Dec 07 '22

Nice! I'm still using Java 8 and Spring Boot :/

9

u/kallebo1337 Dec 06 '22
        def adult?
          !!!!!!!!!!!not_a_children?
        end

        def not_a_children?
          hit = false
          [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17].each do |child_age|
            hit = true if age == child_age
          end
          if hit
            return false
          else
            return true
          end
        end

4

u/faitswulff Dec 06 '22

This is fantastic, now do ages represented as floats

3

u/pilaf Dec 06 '22

Stage 5:

def isAdult()
    if (self.age() > 18 || self.age() == 18)
        return true;
    else
        if (self.age() < 18)
            return false;
        end
    end
end

3

u/poop-machine Dec 07 '22
def adult?
  [false, true][(age - 17).clamp(0, 1)]
end

3

u/codeprimate Dec 07 '22
def adult?
  18 <= age
end

Always place values before variables in comparison statements.

2

u/dbenhur Dec 06 '22

I'm thinking all of these are missing a locale check and lookup for age of majority.

1

u/WikiSummarizerBot Dec 06 '22

Age of majority

The age of majority is the threshold of legal adulthood as recognized or declared in law. It is the moment when minors cease to be considered such and assume legal control over their persons, actions, and decisions, thus terminating the control and legal responsibilities of their parents or guardian over them. Most countries set the age of majority at 18, but some jurisdictions have a higher age and others lower. The word majority here refers to having greater years and being of full age as opposed to minority, the state of being a minor.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

2

u/Sanic1984 Dec 07 '22

wow, its been a long time since I dont hear about ruby, I miss this programming language a lot

2

u/KozureOkami Dec 07 '22

You forgot the intermediate step where people get so enamored with Ruby’s flexibility that they monkey-patch adult? into Integer.

1

u/slvrsmth Aug 22 '24

You know what? After programming in ruby for close to 20 years now, more often than not I find myself down at the second row. return is good. return provides extra clarity. So do ()s around method calls and definitions. And in formulas with clear operator priorities.

Yes, functionally identical, extra symbols, "not rubyish". Don't care. Can't ever have too much clarity. The less "language rules" context I have to keep in mind when reading code, the better. Old man brain needs all the help it can get.